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
returns a fully connected layer and specifies the layer
= fullyconnectedlayer(outputsize
)outputsize
property.
sets the optional parameters and initialization, learning rate and regularization, and
layer
= fullyconnectedlayer(outputsize
,name,value
)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
outputsize
— output size
positive integer
output size for the fully connected layer, specified as a positive integer.
example:
10
inputsize
— input size
'auto'
(default) | positive integer
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
weightsinitializer
— function to initialize weights
'glorot'
(default) |
'he'
|
'orthogonal'
|
'narrow-normal'
|
'zeros'
|
'ones'
| function handle
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 variance2/(inputsize outputsize)
.'he'
– initialize the weights with the he initializer [2]. the he initializer samples from a normal distribution with zero mean and variance2/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)
, wheresz
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
biasinitializer
— function to initialize biases
"zeros"
(default) | "narrow-normal"
| "ones"
| 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)
, wheresz
is the size of the biases.
the layer initializes the biases only when the bias
property is
empty.
data types: char
| string
| function_handle
weights
— layer weights
[]
(default) | matrix
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
bias
— layer biases
[]
(default) | matrix
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
weightlearnratefactor
— learning rate factor for weights
1
(default) | nonnegative scalar
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
biaslearnratefactor
— learning rate factor for biases
1
(default) | nonnegative scalar
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
weightl2factor
— l2 regularization factor for
weights
1 (default) | nonnegative scalar
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
biasl2factor
— l2 regularization factor for biases
0
(default) | nonnegative scalar
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
name
— layer name
""
(default) | character vector | string scalar
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
numinputs
— number of inputs
1
(default)
this property is read-only.
number of inputs to the layer, returned as 1
. this layer accepts a
single input only.
data types: double
inputnames
— input names
{'in'}
(default)
this property is read-only.
input names, returned as {'in'}
. this layer accepts a single input
only.
data types: cell
numoutputs
— number of outputs
1
(default)
this property is read-only.
number of outputs from the layer, returned as 1
. this layer has a
single output only.
data types: double
outputnames
— output names
{'out'}
(default)
this property is read-only.
output names, returned as {'out'}
. this layer has a single output
only.
data types: cell
examples
create fully connected layer
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
specify initial weights and biases in fully connected layer
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
fully connected layer
a fully connected layer multiplies the input by a weight matrix and then adds a bias vector.
as the name suggests, all neurons in a fully connected layer connect to all the neurons in the previous layer. this layer combines all of the features (local information) learned by the previous layers across the image to identify the larger patterns. for classification problems, the last fully connected layer combines the features to classify the images. this is the reason that the outputsize
argument of the last fully connected layer of the network is equal to the number of classes of the data set. for regression problems, the output size must be equal to the number of response variables.
you can also adjust the learning rate and the regularization parameters for this layer using
the related name-value pair arguments when creating the fully connected layer. if you choose
not to adjust them, then the software uses the global training parameters defined by the
trainingoptions
function. for details on global and layer training
options, see .
if the input to the layer is a sequence (for example, in an lstm network), then the fully connected layer acts independently on each time step. for example, if the layer before the fully connected layer outputs an array x of size d-by-n-by-s, then the fully connected layer outputs an array z of size outputsize
-by-n-by-s. at time step t, the corresponding entry of z is , where denotes time step t of x.
fully connected layers flatten the output. they encode the spatial data in the
channel dimension by reshaping the output data. fully connected layers in
seriesnetwork
and dagnetwork
objects output
data with the same number of the spatial dimensions as the input by outputting data
with spatial dimensions of size one. fully connected layers in
dlnetwork
objects remove the spatial dimensions of the
output.
layer input and output formats
layers in a layer array or layer graph pass data to subsequent layers as formatted objects. the format of a dlarray
object is a string of characters, in which each character describes the corresponding dimension of the data. the formats consists of one or more of these characters:
"s"
— spatial"c"
— channel"b"
— batch"t"
— time"u"
— unspecified
for example, 2-d image data represented as a 4-d array, where the first two dimensions correspond to the spatial dimensions of the images, the third dimension corresponds to the channels of the images, and the fourth dimension corresponds to the batch dimension, can be described as having the format "sscb"
(spatial, spatial, channel, batch).
you can interact with these dlarray
objects in automatic differentiation workflows such as developing a custom layer, using a object, or using the and functions with dlnetwork
objects.
this table shows the supported input formats of fullyconnectedlayer
objects and the
corresponding output format. if the software passes the output of the layer to a custom
layer that does not inherit from the nnet.layer.formattable
class, or a
functionlayer
object with the formattable
property
set to 0
(false), then the layer receives an unformatted
dlarray
object with dimensions ordered corresponding to the formats in
this table. the formats listed here are only a subset. the layer may support additional
formats such as formats with additional "s"
(spatial) or
"u"
(unspecified) dimensions.
input format | output format | |
---|---|---|
seriesnetwork and
dagnetwork objects | dlnetwork objects | |
|
|
|
|
| |
|
| |
|
| |
|
|
|
|
|
|
| ||
|
in dlnetwork
objects, fullyconnectedlayer
objects also
support the following input and output format combinations.
input format | output format |
---|---|
|
|
| |
| |
|
|
| |
| |
|
to use these input formats in trainnetwork
workflows, first convert the data to "cbt"
(channel, batch, time)
format using flattenlayer
.
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 r2016ar2019a: default weights initialization is glorot
starting in r2019a, the software, by default, initializes the layer weights of this layer using the glorot initializer. this behavior helps stabilize training and usually reduces the training time of deep networks.
in previous releases, the software, by default, initializes the layer weights by sampling from
a normal distribution with zero mean and variance 0.01. to reproduce this behavior, set the
'weightsinitializer'
option of the layer to
'narrow-normal'
.
打开示例
您曾对此示例进行过修改。是否要打开带有您的编辑的示例?
matlab 命令
您点击的链接对应于以下 matlab 命令:
请在 matlab 命令行窗口中直接输入以执行命令。web 浏览器不支持 matlab 命令。
select a web site
choose a web site to get translated content where available and see local events and offers. based on your location, we recommend that you select: .
you can also select a web site from the following list:
how to get best site performance
select the china site (in chinese or english) for best site performance. other mathworks country sites are not optimized for visits from your location.
americas
- (español)
- (english)
- (english)
europe
- (english)
- (english)
- (deutsch)
- (español)
- (english)
- (français)
- (english)
- (italiano)
- (english)
- (english)
- (english)
- (deutsch)
- (english)
- (english)
- switzerland
- (english)
asia pacific
- (english)
- (english)
- (english)
- 中国
- (日本語)
- (한국어)