deep-river
deep-river copied to clipboard
Multifeature Output
Request: Allow multi-feature output. e.g. allow the final layer to output a list or dictionary with one feature per element.
Here is a (probably incorrect) example of a way to output a list in the case of a rolling regressor, but at least it works for a dynamical system, which is simply to predict features based on a time window of prior features.
It is probably better to use a dictionary template (ie: list of keys) corresponding to the names of the various features output.
class LstmModule(nn.Module):
def __init__(self, n_features, hidden_size=1):
super().__init__()
self.n_features=n_features
self.hidden_size = hidden_size
self.lstm = nn.LSTM(input_size=n_features, hidden_size=hidden_size, num_layers=1, bidirectional=False)
self.fc = nn.Linear(in_features=hidden_size,out_features=n_features)
def forward(self, X, **kwargs):
output, (hn, cn) = self.lstm(X) # lstm with input, hidden, and internal state
return self.fc(output[-1, 0])
metric = metrics.RMSE()
model_pipeline = preprocessing.StandardScaler()
model_pipeline |= RollingRegressor(
module=LstmModule,
loss_fn=torch.nn.functional.mse_loss, #'mse',
optimizer_fn='sgd',
window_size=window_size,
lr=lr, #1e-2,
hidden_size=32, # parameters of MyModule can be overwritten
append_predict=True,
device='cuda',
)