Documentation
¶
Overview ¶
Package fnn implements a generic FNN (Feedforward Neural Network) with various configurations. It should suffice for the common cases and can be extended as needed.
It also provides support for various hyperparameter configuration -- so the defaults can be given by the scope parameters.
E.g: A FNN for a multi-class classification model with NumClasses classes.
func MyMode(scope *model.Scope, inputs []*Node) (outputs []*Node) {
x := inputs[0]
logits := fnn.New(scope.In("model"), x, NumClasses).
NumHiddenLayers(3).
Apply("swish").
Dropout(0.3).
UseResidual(true).
Done()
return []*Node{logits}
}
Index ¶
- Constants
- type Config
- func (c *Config) Activation(activation activation.Type) *Config
- func (c *Config) Done() *Node
- func (c *Config) Dropout(ratio float64) *Config
- func (c *Config) Normalization(normalization string) *Config
- func (c *Config) NumHiddenLayers(numLayers, numHiddenNodes int) *Config
- func (c *Config) Regularizer(regularizer regularizer.Regularizer) *Config
- func (c *Config) Residual(useResidual bool) *Config
- func (c *Config) UseBias(useBias bool) *Config
- func (c *Config) WithEnsembleAxis(ensembleAxis int) *Config
- func (c *Config) WithEnsembleSize(ensembleSize int) *Config
- func (c *Config) WithWeightsLayout(layout compute.DenseLayout) *Config
Constants ¶
const ( // ParamNumHiddenLayers is the hyperparameter that defines the default number of hidden layers. // The default is 0 (int), so no hidden layers. ParamNumHiddenLayers = "fnn_num_hidden_layers" // ParamNumHiddenNodes is the hyperparameter that defines the default number of hidden nodes for KAN hidden layers. // The default is 10 (int). ParamNumHiddenNodes = "fnn_num_hidden_nodes" // ParamResidual is the hyperparameter that defines whether to use residual connections between consecutive hidden layers. // If set, and the feature dimension (the last one) is the same between the input, it also adds a residual to the // input. Same with the outputDimensions. // Default is false (bool). ParamResidual = "fnn_residual" // ParamNormalization is the name of the normalization to use in between layers. // It is only applied if there are hidden layers. // See layers.KnownNormalizer: "layer" and "batch" are the most common normalization strategies. // // Defaults to the parameter "normalization" (layers.ParamNormalization) and if that is not set, to "none" // (same as ""), which is no normalization. ParamNormalization = "fnn_normalization" // ParamDropoutRate is the name of the dropout rate to use in between layers. // It is only applied if there are hidden layers. // // Defaults to the parameter "dropout_rate" (layers.ParamDropoutRate) and if that is not set, to 0.0 (no dropout) ParamDropoutRate = "fnn_dropout_rate" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config is created with New and can be configured with its methods, or simply setting the corresponding hyperparameters in the model.
func New ¶
New creates a configuration for a FNN (Feedforward Neural Network). This can be further configured through various methods and when finished, call Done to actually add the FNN computation graph and get the output.
The input is expected to have shape `[<batch dimensions...>, featureDimension]`, the output will have shape `[<batch dimensions...>, <outputDimensions...>]`.
Configuration options have defaults, but can also be configured through hyperparameters set in the model. See corresponding configuration methods for details.
E.g: A FNN for a multi-class classification model with NumClasses classes.
func MyModel(scope *model.Scope, inputs []*Node) (outputs []*Node) {
x := inputs[0]
logits := fnn.New(scope.In("model"), x, NumClasses).
NumHiddenLayers(3, 128).
Activation(activations.TypeSilu).
Dropout(0.3).
Normalization("layer").
Residual(true).
Done()
return []*Node{logits}
}
func (*Config) Activation ¶
func (c *Config) Activation(activation activation.Type) *Config
Activation sets the activation for the FNN, in between each layer. The input and output layers don't get an activation layer.
The default is "relu", but it can be overridden by setting the hyperparameter layers.ParamActivation (="activation") in the model.
func (*Config) Done ¶
func (c *Config) Done() *Node
Done takes the configuration and apply the FNN as configured.
func (*Config) Dropout ¶
Dropout sets the dropout ratio for the FNN, in between each layer. The output layer doesn't get dropout. It uses the normalized form of dropout (see layers.DropoutNormalize).
If set to 0.0, no dropout is used.
The default is 0.0, but it can be overridden by setting the hyperparameter layers.ParamDropoutRate (="dropout_rate") in the model.
func (*Config) Normalization ¶
Normalization sets the normalization type to use in between layers. The input and output layers don't get a normalization layer.
The default is "none", but it can be overridden by setting the hyperparameter ParamNormalization (="fnn_normalization") in the model.
func (*Config) NumHiddenLayers ¶
NumHiddenLayers configure the number of hidden layers between the input and the output. Each layer will have numHiddenNodes nodes.
The default is 0 (no hidden layers), but it will be overridden if the hyperparameter ParamNumHiddenLayers is set in the scope (scope). The value for numHiddenNodes can also be configured with the hyperparameter ParamNumHiddenNodes.
func (*Config) Regularizer ¶
func (c *Config) Regularizer(regularizer regularizer.Regularizer) *Config
Regularizer to be applied to the learned weights (but not the biases). Default is none.
To use more than one type of Regularizer, use regularizers.Combine, and set the returned combined regularizer here.
The default is regularizers.FromScope, which is configured by regularizers.ParamL1 and regularizers.ParamL2.
func (*Config) Residual ¶
Residual configures if residual connections in between layers with the same number of nodes should be used. They are very useful for deep models.
The default is false, and it may be configured with the hyperparameter ParamResidual.
func (*Config) UseBias ¶
UseBias configures whether to add a bias term to each node. Almost always you want this to be true, and that is the default.
func (*Config) WithEnsembleAxis ¶ added in v0.28.0
WithEnsembleAxis defined an axis from the input operand that should be considered as separate per model of the ensemble.
It initializes the ensemble size from the input shape's given axis.
This is used when constructing upper layers of an ensemble model, when the input has already been transformer be per model of the ensemble -- hence it already has an ensemble axis.
See WithEnsembleSize if you want to configure an ensemble for a plain input, which hasn't been transformed per model of the ensemble, and hence doesn't have an ensemble axis.
func (*Config) WithEnsembleSize ¶ added in v0.28.0
WithEnsembleSize configure an ensemble size greater than 1, which adds an extra "ensemble axis" to the variables and intermediary layers, executing the FNN as an ensemble.
See WithEnsembleAxis for an alternative way to configure ensembles, when your input is already split into the ensemble.
func (*Config) WithWeightsLayout ¶ added in v0.28.1
func (c *Config) WithWeightsLayout(layout compute.DenseLayout) *Config
WithWeightsLayout sets the weight layout for linear transformation weights in the FNN layers.
Supported layouts are compute.DenseLayoutInputOutputs (default, 0) where weights shape is `[in_features, out_features...]` or compute.DenseLayoutOutputsInput where weights shape is `[out_features..., in_features]`.