Neural network models module¶
Some basis of neural network models with PyTorch package.
fynance.models.neural_network.BaseNeuralNet () |
Base object for neural network model with PyTorch. |
fynance.models.neural_network.MultiLayerPerceptron (X, y) |
Neural network with MultiLayer Perceptron architecture. |
-
class
fynance.models.neural_network.
BaseNeuralNet
¶ Bases:
torch.nn.modules.module.Module
Base object for neural network model with PyTorch.
Inherits of torch.nn.Module object with some higher level methods.
See also
Attributes: - criterion : torch.nn.modules.loss
A loss function.
- optimizer : torch.optim
An optimizer algorithm.
- N, M : int
Respectively input and output dimension.
Methods
set_optimizer(criterion, optimizer, **kwargs) Set optimizer object with specified criterion (loss function) and any optional parameters. train_on(X, y) Trains the neural network on X as inputs and y as ouputs. predict(X) Predicts the outputs of neural network model for X as inputs. -
__init__
(self)¶ Initialize.
-
predict
(self, X)¶ Predicts outputs of neural network model.
Parameters: - X : torch.Tensor
Inputs to compute prediction.
Returns: - torch.Tensor
Outputs prediction.
-
set_data
(self, X, y, x_type=None, y_type=None)¶ Set data inputs and outputs.
Parameters: - X, y : array-like
Respectively input and output data.
- x_type, y_type : torch.dtype
Respectively input and ouput data types. Default is None.
-
set_optimizer
(self, criterion, optimizer, **kwargs)¶ Set the optimizer object.
Set optimizer object with specified criterion as loss function and any kwargs as optional parameters.
Parameters: - criterion : torch.nn.modules.loss
A loss function.
- optimizer : torch.optim
An optimizer algorithm.
- kwargs : dict
Keyword arguments of optimizer, cf pytorch documentation [1].
Returns: - NeuralNetwork
Self object model.
References
[1] https://pytorch.org/docs/stable/optim.html
-
train_on
(self, X, y)¶ Trains the neural network model.
Parameters: - X, y : torch.Tensor
Respectively inputs and outputs to train model.
Returns: - torch.nn.modules.loss
Loss outputs.
-
class
fynance.models.neural_network.
MultiLayerPerceptron
(X, y, layers=[], activation=None, drop=None)¶ Bases:
fynance.models.neural_network.BaseNeuralNet
Neural network with MultiLayer Perceptron architecture.
Refered as vanilla neural network model, with n hidden layers s.t n \(\geq\) 1, with each one a specified number of neurons.
See also
Attributes: - criterion : torch.nn.modules.loss
A loss function.
- optimizer : torch.optim
An optimizer algorithm.
- n : int
Number of hidden layers.
- layers : list of int
List with the number of neurons for each hidden layer.
- f : torch.nn.Module
Activation function.
Methods
set_optimizer(criterion, optimizer, **kwargs) Set optimizer object with specified criterion (loss function) and any optional parameters. train_on(X, y) Trains the neural network on X as inputs and y as ouputs. predict(X) Predicts the outputs of neural network model for X as inputs. set_data(X, y) Set respectively input and ouputs data tensor. -
__init__
(self, X, y, layers=[], activation=None, drop=None)¶ Initialize.
Parameters: - X, y : array-like
Respectively inputs and outputs data.
- layers : list of int
List of number of neurons in each hidden layer.
- activation : torch.nn.Module
Activation function of layers.
- drop : float, optional
Probability of an element to be zeroed.
-
forward
(self, x)¶ Forward computation.