コード例 #1
0
ファイル: test_nn.py プロジェクト: zhaoy99/deepdow
    def test_basic(self, Xy_dummy):
        eps = 1e-4

        X, _, _, _ = Xy_dummy
        n_samples, n_channels, lookback, n_assets = X.shape
        dtype = X.dtype
        device = X.device

        network = LinearNet(n_channels, lookback, n_assets)
        network.to(device=device, dtype=dtype)

        with pytest.raises(ValueError):
            network(
                torch.ones(n_samples,
                           n_channels + 1,
                           lookback,
                           n_assets,
                           device=device,
                           dtype=dtype))

        weights = network(X)

        assert isinstance(network.hparams, dict)
        assert network.hparams
        assert torch.is_tensor(weights)
        assert weights.shape == (n_samples, n_assets)
        assert X.device == weights.device
        assert X.dtype == weights.dtype
        assert torch.allclose(weights.sum(dim=1),
                              torch.ones(n_samples).to(dtype=dtype,
                                                       device=device),
                              atol=eps)
コード例 #2
0
    def test_n_params(self, n_channels, lookback, n_assets):
        network = LinearNet(n_channels, lookback, n_assets)

        n_features = n_channels * lookback * n_assets
        expected = 0
        expected += n_features * 2  # batch norm
        expected += n_features * n_assets + n_assets  # dense
        expected += 1  # temperature

        actual = sum(p.numel() for p in network.parameters() if p.requires_grad)

        assert expected == actual
コード例 #3
0
n_timesteps = len(data)

# Create features and targets
X_list, y_list = [], []

for i in range(lookback, n_timesteps - horizon - gap + 1):
    X_list.append(data[i - lookback:i, :])
    y_list.append(data[i + gap:i + gap + horizon, :])

X = np.stack(X_list, axis=0)[:, None, ...]
y = np.stack(y_list, axis=0)[:, None, ...]

# Setup deepdow framework
dataset = InRAMDataset(X, y)

network = LinearNet(1, lookback, n_assets, p=0.5)
dataloader = RigidDataLoader(dataset,
                             indices=list(range(5000)),
                             batch_size=batch_size,
                             lookback=lookback)
val_dataloaders = {
    'train':
    dataloader,
    'val':
    RigidDataLoader(dataset,
                    indices=list(range(5020, 9800)),
                    batch_size=batch_size,
                    lookback=lookback)
}

run = Run(network,