main content

fully connected layer -凯发k8网页登录

fully connected layer

description

a fully connected layer multiplies the input by a weight matrix and then adds a bias vector.

creation

description

layer = fullyconnectedlayer(outputsize) returns a fully connected layer and specifies the outputsize property.

example

layer = fullyconnectedlayer(outputsize,name,value) sets the optional parameters and initialization, learning rate and regularization, and name properties using name-value pairs. for example, fullyconnectedlayer(10,'name','fc1') creates a fully connected layer with an output size of 10 and the name 'fc1'. you can specify multiple name-value pairs. enclose each property name in single quotes.

properties

fully connected

output size for the fully connected layer, specified as a positive integer.

example: 10

input size for the fully connected layer, specified as a positive integer or 'auto'. if inputsize is 'auto', then the software automatically determines the input size during training.

parameters and initialization

function to initialize the weights, specified as one of the following:

  • 'glorot' – initialize the weights with the glorot initializer [1] (also known as xavier initializer). the glorot initializer independently samples from a uniform distribution with zero mean and variance 2/(inputsize outputsize).

  • 'he' – initialize the weights with the he initializer [2]. the he initializer samples from a normal distribution with zero mean and variance 2/inputsize.

  • 'orthogonal' – initialize the input weights with q, the orthogonal matrix given by the qr decomposition of z = qr for a random matrix z sampled from a unit normal distribution. [3]

  • 'narrow-normal' – initialize the weights by independently sampling from a normal distribution with zero mean and standard deviation 0.01.

  • 'zeros' – initialize the weights with zeros.

  • 'ones' – initialize the weights with ones.

  • function handle – initialize the weights with a custom function. if you specify a function handle, then the function must be of the form weights = func(sz), where sz is the size of the weights. for an example, see .

the layer only initializes the weights when the weights property is empty.

data types: char | string | function_handle

function to initialize the biases, specified as one of these values:

  • "zeros" — initialize the biases with zeros.

  • "ones" — initialize the biases with ones.

  • "narrow-normal" — initialize the biases by independently sampling from a normal distribution with a mean of zero and a standard deviation of 0.01.

  • function handle — initialize the biases with a custom function. if you specify a function handle, then the function must have the form bias = func(sz), where sz is the size of the biases.

the layer initializes the biases only when the bias property is empty.

data types: char | string | function_handle

layer weights, specified as a matrix.

the layer weights are learnable parameters. you can specify the initial value of the weights directly using the weights property of the layer. when you train a network, if the weights property of the layer is nonempty, then the and functions use the weights property as the initial value. if the weights property is empty, then the software uses the initializer specified by the weightsinitializer property of the layer.

at training time, weights is an outputsize-by-inputsize matrix.

data types: single | double

layer biases, specified as a matrix.

the layer biases are learnable parameters. when you train a neural network, if bias is nonempty, then the and functions use the bias property as the initial value. if bias is empty, then software uses the initializer specified by biasinitializer.

at training time, bias is an outputsize-by-1 matrix.

data types: single | double

learning rate and regularization

learning rate factor for the weights, specified as a nonnegative scalar.

the software multiplies this factor by the global learning rate to determine the learning rate for the weights in this layer. for example, if weightlearnratefactor is 2, then the learning rate for the weights in this layer is twice the current global learning rate. the software determines the global learning rate based on the settings you specify using the function.

data types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

learning rate factor for the biases, specified as a nonnegative scalar.

the software multiplies this factor by the global learning rate to determine the learning rate for the biases in this layer. for example, if biaslearnratefactor is 2, then the learning rate for the biases in the layer is twice the current global learning rate. the software determines the global learning rate based on the settings you specify using the function.

data types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

l2 regularization factor for the weights, specified as a nonnegative scalar.

the software multiplies this factor by the global l2 regularization factor to determine the l2 regularization for the weights in this layer. for example, if weightl2factor is 2, then the l2 regularization for the weights in this layer is twice the global l2 regularization factor. you can specify the global l2 regularization factor using the function.

data types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

l2 regularization factor for the biases, specified as a nonnegative scalar.

the software multiplies this factor by the global l2 regularization factor to determine the l2 regularization for the biases in this layer. for example, if biasl2factor is 2, then the l2 regularization for the biases in this layer is twice the global l2 regularization factor. the software determines the global l2 regularization factor based on the settings you specify using the function.

data types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

layer

layer name, specified as a character vector or a string scalar. for layer array input, the , , , , and functions automatically assign names to layers with the name "".

the fullyconnectedlayer object stores this property as a character vector.

data types: char | string

this property is read-only.

number of inputs to the layer, returned as 1. this layer accepts a single input only.

data types: double

this property is read-only.

input names, returned as {'in'}. this layer accepts a single input only.

data types: cell

this property is read-only.

number of outputs from the layer, returned as 1. this layer has a single output only.

data types: double

this property is read-only.

output names, returned as {'out'}. this layer has a single output only.

data types: cell

examples

create a fully connected layer with an output size of 10 and the name 'fc1'.

layer = fullyconnectedlayer(10,'name','fc1')
layer = 
  fullyconnectedlayer with properties:
          name: 'fc1'
   hyperparameters
     inputsize: 'auto'
    outputsize: 10
   learnable parameters
       weights: []
          bias: []
use properties method to see a list of all properties.

include a fully connected layer in a layer array.

layers = [ ...
    imageinputlayer([28 28 1])
    convolution2dlayer(5,20)
    relulayer
    maxpooling2dlayer(2,'stride',2)
    fullyconnectedlayer(10)
    softmaxlayer
    classificationlayer]
layers = 
  7x1 layer array with layers:
     1   ''   image input             28x28x1 images with 'zerocenter' normalization
     2   ''   2-d convolution         20 5x5 convolutions with stride [1  1] and padding [0  0  0  0]
     3   ''   relu                    relu
     4   ''   2-d max pooling         2x2 max pooling with stride [2  2] and padding [0  0  0  0]
     5   ''   fully connected         10 fully connected layer
     6   ''   softmax                 softmax
     7   ''   classification output   crossentropyex

to specify the weights and bias initializer functions, use the weightsinitializer and biasinitializer properties respectively. to specify the weights and biases directly, use the weights and bias properties respectively.

specify initialization function

create a fully connected layer with an output size of 10 and specify the weights initializer to be the he initializer.

outputsize = 10;
layer = fullyconnectedlayer(outputsize,'weightsinitializer','he')
layer = 
  fullyconnectedlayer with properties:
          name: ''
   hyperparameters
     inputsize: 'auto'
    outputsize: 10
   learnable parameters
       weights: []
          bias: []
use properties method to see a list of all properties.

note that the weights and bias properties are empty. at training time, the software initializes these properties using the specified initialization functions.

specify custom initialization function

to specify your own initialization function for the weights and biases, set the weightsinitializer and biasinitializer properties to a function handle. for these properties, specify function handles that take the size of the weights and biases as input and output the initialized value.

create a fully connected layer with output size 10 and specify initializers that sample the weights and biases from a gaussian distribution with a standard deviation of 0.0001.

outputsize = 10;
weightsinitializationfcn = @(sz) rand(sz) * 0.0001;
biasinitializationfcn = @(sz) rand(sz) * 0.0001;
layer = fullyconnectedlayer(outputsize, ...
    'weightsinitializer',@(sz) rand(sz) * 0.0001, ...
    'biasinitializer',@(sz) rand(sz) * 0.0001)
layer = 
  fullyconnectedlayer with properties:
          name: ''
   hyperparameters
     inputsize: 'auto'
    outputsize: 10
   learnable parameters
       weights: []
          bias: []
use properties method to see a list of all properties.

again, the weights and bias properties are empty. at training time, the software initializes these properties using the specified initialization functions.

specify weights and bias directly

create a fully connected layer with an output size of 10 and set the weights and bias to w and b in the mat file fcweights.mat respectively.

outputsize = 10;
load fcweights
layer = fullyconnectedlayer(outputsize, ...
    'weights',w, ...
    'bias',b)
layer = 
  fullyconnectedlayer with properties:
          name: ''
   hyperparameters
     inputsize: 720
    outputsize: 10
   learnable parameters
       weights: [10x720 double]
          bias: [10x1 double]
use properties method to see a list of all properties.

here, the weights and bias properties contain the specified values. at training time, if these properties are non-empty, then the software uses the specified values as the initial weights and biases. in this case, the software does not use the initializer functions.

algorithms

references

[1] glorot, xavier, and yoshua bengio. "understanding the difficulty of training deep feedforward neural networks." in proceedings of the thirteenth international conference on artificial intelligence and statistics, 249–356. sardinia, italy: aistats, 2010.

[2] he, kaiming, xiangyu zhang, shaoqing ren, and jian sun. "delving deep into rectifiers: surpassing human-level performance on imagenet classification." in 2015 ieee international conference on computer vision (iccv), 1026–34. santiago, chile: ieee, 2015.

[3] saxe, andrew m., james l. mcclelland, and surya ganguli. "exact solutions to the nonlinear dynamics of learning in deep linear neural networks.” preprint, submitted february 19, 2014. .

extended capabilities

c/c code generation
generate c and c code using matlab® coder™.

gpu code generation
generate cuda® code for nvidia® gpus using gpu coder™.

version history

introduced in r2016a
网站地图