GRUCell¶
Defined in fynance.models.gru
- class GRUCell(X, y=None, drop=None, x_type=None, y_type=None, bias=True, hidden_activation=nn.Tanh, hidden_state_size=None, reset_activation=nn.Sigmoid, update_activation=nn.Sigmoid)[source]
Bases:
_GRUCellGRU cell — public composable building block.
Implements the GRU gating logic (reset + update gates) without an output projection layer. Designed to be composed inside larger architectures (TCN, Transformers, encoder-decoders). For a standalone trainable model with output projection, use
GatedRecurrentUnit.- Parameters:
- Xint or array-like
Input dimension (int) or input data. When passing an int,
ymay be omitted.- yarray-like or int, optional
Output data or output dimension. Not required when using the cell as a building block.
- hidden_state_sizeint, optional
Size of the hidden state. Defaults to the input size.
- dropfloat, optional
Dropout probability applied before each gate.
- hidden_activationtorch.nn.Module, optional
Activation for the candidate hidden state (default: Tanh).
- reset_activation, update_activationtorch.nn.Module, optional
Gate activations (default: Sigmoid for both).
See also
GatedRecurrentUnitfull model with output projection and training.
fynance.models.lstm.LSTMCellLSTM variant.
Examples
>>> import torch >>> from fynance.models.gru import GRUCell >>> cell = GRUCell(8, hidden_state_size=16) >>> H = torch.zeros(4, 16) >>> X = torch.randn(4, 8) >>> H_new = cell(X, H) >>> H_new.shape torch.Size([4, 16])
- 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.
- 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
Xandytotorch.Tensorand caches them asself.X/self.y. After the call the attributesself.T(number of observations),self.N(input columns) andself.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.Mwere already set andX/ydo not match, or ifXandyhave different lengths.
- set_lr_scheduler(lr_scheduler, **kwargs)
Set dynamic learning rate.
- Parameters:
- lr_schedulertorch.optim.lr_scheduler._LRScheduler
Method from
torch.optim.lr_schedulerto wrapself.optimizer, cf moduletorch.optim.lr_schedulerin 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.