TemporalConvNet¶
Defined in fynance.models.tcn
- class TemporalConvNet(X, y, channels=None, kernel_size=2, drop=0., x_type=None, y_type=None)[source]
Bases:
BaseNeuralNetCausal dilated Temporal Convolutional Network.
A stack of residual blocks (
_TemporalBlock) with dilation doubling at each level (1, 2, 4, …) followed by a linear read-out to the output dimension. Padding is causal (left-only, trimmed by_Chomp1d), so the prediction at timetnever seest + 1— preserving the library’s no-lookahead invariant.Configure the optimizer with
BaseNeuralNet.set_optimizer(e.g. withfynance.models.loss.SharpeLoss).- Parameters:
- X, yarray-like or int
If array-like, respectively the input and output data.
If an integer, respectively the input and output dimension.
- channelslist of int, optional
Number of channels of each residual block (the depth is
len(channels); dilation of blockiis2 ** i). Default[16, 16].- kernel_sizeint, optional
Convolution kernel size. Default
2.- dropfloat, optional
Dropout probability inside each block. Default
0..
- Attributes:
- tcntorch.nn.Sequential
The stack of temporal blocks.
- lineartorch.nn.Linear
Final read-out from the last channel size to
Moutputs.
See also
fynance.models._base.BaseNeuralNet,fynance.models.lstm.LongShortTermMemory
Examples
>>> import torch >>> from fynance.models.tcn import TemporalConvNet >>> _ = torch.manual_seed(0) >>> X = torch.randn(50, 3) >>> y = torch.randn(50, 1) >>> model = TemporalConvNet(X, y, channels=[8, 8], kernel_size=2) >>> out = model(X) >>> out.shape torch.Size([50, 1])
- forward(x)[source]
Forward pass.
- Parameters:
- xtorch.Tensor
Input window, shape
(L, N)(time steps × features).
- Returns:
- torch.Tensor
Per-step output, shape
(L, M).
- 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)
Predicts outputs of neural network model.
Runs
self.forward(X)undertorch.no_grad, so no autograd graph is built. The returned tensor is detached and lives on the same device as the model parameters.- Parameters:
- Xtorch.Tensor
Inputs to compute prediction. Same shape and dtype contract as
train_on.
- Returns:
- torch.Tensor
Outputs prediction (detached, gradient-free).
- 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,polars.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.
- train_on(X, y)
Trains the neural network model on a single batch.
Runs one forward / backward / optimizer-step cycle on the batch
(X, y). As a side effect, gradients of all parameters are zeroed before the forward pass and the optimizer state is advanced afterwards. If a learning-rate scheduler has been registered viaset_lr_scheduler, itsstepis also called.- Parameters:
- X, ytorch.Tensor
Respectively inputs and outputs to train model. Shapes must match what
self.forwardexpects (see the class-level “Public API contract” section).
- Returns:
- torch.Tensor
The loss tensor produced by
self.criterion(self(X), y), with gradient already consumed byloss.backward().
- Raises:
- AttributeError
If
set_optimizerhas not been called yet.