Categories &

Functions List

Function Reference: fcnntrain

statistics: Mdl = fcnntrain (X, Y, LayerSizes, Activations, OutputLayerActivation, NumThreads, LearningRate, Epochs, DisplayInfo)
statistics: Mdl = fcnntrain (…, LossFunction)

Train a fully connected Neural Network.

Mdl = fcnntrain (…) requires the following input arguments.

  • X : An NxM matrix containing the data set to be trained upon. Rows N correspond to individual samples and columns M correspond to features (dimensions). Type of X must be double.
  • Y : An Nx1 column vector containing the labels of the training dataset. The labels must be natural numbers (positive integers) starting from 1 up to the number of classes, similarly as returned by the ‘grp2idx‘ function. Type of Y must be double. Under regression, selected by LossFunction 2, Y is instead an NxR matrix of response values, which may take any finite value, and the output layer is sized to its R columns rather than to a number of classes.
  • LayerSizes : A numeric row vector of integer values defining the size of the hidden layers of the network. Input and output layers are automatically determined by the training data and their labels.
  • Activations : The activation function of the hidden layers, named as a character vector applying to all of them or as a cellstring naming them one by one, in which case it must have one name per hidden layer. The supported names are:
    • 'linear' or 'none' : the identity
    • 'sigmoid'
    • 'relu' : rectified linear unit
    • 'tanh' : hyperbolic tangent
    • 'softmax'
    • 'lrelu' or 'prelu' : leaky rectified linear unit, whose negative slope is a constant 0.01
    • 'elu' : exponential linear unit, saturating at -1
    • 'gelu' : Gaussian error linear unit
  • OutputLayerActivation : The activation function of the output layer, named as a character vector from the same list.
  • NumThreads : A positive scalar integer value defining the number of threads used for computing the activation layers. For layers with less than 1000 neurons, NumThreads always defaults to 1.
  • LearningRate : A positive scalar value defining the learning rate used by the gradient descend algorithm during training.
  • Epochs : A positive scalar value defining the number of epochs for training the model.
  • DisplayInfo : A boolean scalar indicating whether to print information during training.

Mdl = fcnntrain (…, LossFunction) also selects the loss the network is trained against. LossFunction is a scalar: 0 for mean squared error over a one-hot target, which is the default, 1 for cross entropy, and 2 for mean squared error over a continuous response. Cross entropy expects the output layer to report a probability over the classes, so it belongs with a softmax output; paired that way the two gradients compose to y - t. Its loss is undefined where the predicted probability of the true class is zero, so both the logarithm and its derivative are floored. Code 2 is regression: Y holds response values rather than labels, the output layer belongs with the identity activation, and the returned model carries no Accuracy field, there being no labels to count.

fcnntrain returns the trained model, Mdl, as a structure containing the following fields:

  • LayerWeights : A cell array with each element containing a matrix with the Weights and Biases of each layer including the output layer.
  • Activations : A numeric row vector of integer values defining the activation functions to be used at each layer including the output layer.
  • Accuracy : The prediction accuracy at each iteration during the neural network model’s training process. Absent under regression.
  • Loss : The loss value recorded at each iteration during the neural network model’s training process.

Installation Note: in order to support parallel processing on MacOS, users have to manually add support for OpenMP by adding the following flags to CFLAGS and CXXFLAGS prior to installing the statistics package:

setenv ("CPPFLAGS", "-I/opt/homebrew/opt/libomp/include -Xclang -fopenmp")

See also: fcnnpredict, fitcnet, ClassificationNeuralNetwork

Source Code: fcnntrain