RecurrentNeuralNetwork

Defined in fynance.models.rnn

class RecurrentNeuralNetwork(X, y, drop=None, x_type=None, y_type=None, bias=True, forward_activation=nn.Softmax, hidden_activation=nn.Tanh, hidden_state_size=None)[source]

Bases: _OutputLayerMixin, _RecurrentBase

Neural network with vanilla Elman recurrent architecture.

A single recurrent linear layer followed by a forward output layer. Each call to forward updates the hidden state H and emits a prediction Y. Suitable as a baseline for short-horizon sequence prediction; for longer dependencies, use GatedRecurrentUnit or LongShortTermMemory to mitigate vanishing gradients.

Parameters:
X, yarray-like or int
  • If it’s an array-like, respectively inputs and outputs data.

  • If it’s an integer, respectively dimension of inputs and outputs.

dropfloat, optional

Probability of an element to be zeroed.

forward_activation, hidden_activationtorch.nn.Module, optional

Activation functions, default is respectively Softmax and Tanh function.

hidden_state_sizeint, optional

Size of hidden states, default is the same size than input.

Attributes:
criteriontorch.nn.modules.loss

A loss function.

optimizertorch.optim

An optimizer algorithm.

W_h, W_ytorch.nn.Linear

Respectively recurrent and forward weights.

f_y, f_htorch.nn.Module

Respectively forward and hidden activation functions.

See also

fynance.models.gru.GatedRecurrentUnit
fynance.models.lstm.LongShortTermMemory
forward(X, H)[source]

Forward method.

Parameters:
X, Htorch.Tensor

Respectively input data and hidden state.

Returns:
torch.Tensor

Output data.

torch.Tensor

Hidden state.

load_model(path, load_optimizer=False)

Save the model with this weights and parameters.

Parameters:
pathstr or os.PathLike object

Path to load the model.

load_optimizerbool, optional

If True, then load also the optimizer.

predict(X, H)

Predicts outputs of neural network model.

Parameters:
Xtorch.Tensor

Inputs to compute prediction.

Htorch.Tensor

States of the model.

Returns:
torch.Tensor

Outputs prediction.

torch.Tensor

Updated states of the model.

save_model(path, save_optimizer=False)

Save the model with this weights and parameters.

Parameters:
pathstr or os.PathLike object

Path to save the model.

save_optimizerbool, optional

If True, then save also the optimizer.

set_data(X, y, x_type=None, y_type=None)

Set data inputs and outputs.

Coerces X and y to torch.Tensor and caches them as self.X / self.y. After the call the attributes self.T (number of observations), self.N (input columns) and self.M (output columns) are set.

Parameters:
X, yarray-like

Respectively input and output data. Accepted types: numpy.ndarray, torch.Tensor, pandas.DataFrame. Shapes must be (T, N) and (T, M) respectively.

x_type, y_typetorch.dtype, optional

Target dtypes for the resulting tensors. Default is None, which preserves the input dtype.

Returns:
BaseNeuralNet

self, to allow chaining.

Raises:
ValueError

If self.N / self.M were already set and X / y do not match, or if X and y have different lengths.

set_lr_scheduler(lr_scheduler, **kwargs)

Set dynamic learning rate.

Parameters:
lr_schedulertorch.optim.lr_scheduler._LRScheduler

Method from torch.optim.lr_scheduler to wrap self.optimizer, cf module torch.optim.lr_scheduler in PyTorch documentation [2].

**kwargs

Keyword arguments to pass to the learning rate scheduler.

References

set_optimizer(criterion, optimizer, params=None, **kwargs)

Set the optimizer object.

Set optimizer object with specified criterion as loss function and any kwargs as optional parameters.

Parameters:
criterionCallabletorch.nn.modules.loss

A loss function.

optimizertorch.optim.Optimizer

An optimizer algorithm.

paramsobject or iterable object

Layer of parameters to optimize or dicts defining parameter groups. If set to None then all parameters of model will be optimized. Default is None.

**kwargs

Keyword arguments of optimizer, cf PyTorch documentation [1].

Returns:
BaseNeuralNet

Self object model.

References

set_seed(seed_torch=None, seed_numpy=None)

Set seed for PyTorch and NumPy random number generator.

Parameters:
seed_torch, seed_numpybool or int, optional

If seed is an int \(0 < seed < 2^32\) set respectively PyTorch and NumPy seed with the number. Otherwise if is True then choose a random number, else doesn’t set seed.

train_on(X, y, H)

Trains the neural network model.

Parameters:
X, y, Htorch.Tensor

Respectively inputs, outputs and states to train model.

Returns:
torch.nn.modules.loss

Loss outputs.

torch.Tensor

Updated states of the model.