fullyconnect
Sum all weighted input data and apply a bias
Syntax
Description
The fully connect operation multiplies the input vectors by a weight matrix and then adds a bias vector.
Note
This function applies the fully connect operation to dlarray data. If
you want to apply the fully connect operation within a dlnetwork object, use fullyConnectedLayer.
Examples
Create an array of input data that represents 128 observations with 12 input channels. Arrange the data so that the rows and columns correspond to the observations and channels, respectively.
numObservations = 128; numChannels = 12; X = rand(numObservations,numChannels);
Convert the data to a formatted dlarray object. Specify that the data has format "BC" (batch, channel).
X = dlarray(X,"BC");Initialize the learnable parameters for a fully connected operation with an output size of 10. For example purposes, initialize the learnable parameters with random values.
outputSize = 10; weights = rand(outputSize,numChannels); bias = rand(outputSize,1);
Apply the fully connect operation.
Y = fullyconnect(X,weights,bias);
View the size and format of the output data.
size(Y)
ans = 1×2
10 128
dims(Y)
ans = 'CB'
Input Arguments
Input data, specified as a dlarray object or a numeric array. If
X is a numeric array, then at least one of the
weights or bias arguments must be a
dlarray object.
The fullyconnect operation flattens
the dimensions specified by the dim argument, then multiplies by the
weights matrix and adds the bias vector for each element in remaining dimensions,
independently. (since R2026a)
Before R2026a: The fullyconnect operation
flattens the "S" (spatial), "C" (channel), and
"U" (unspecified) dimensions of the input data, then multiplies by
the weights matrix and adds the bias vector for each element in the "B"
(batch) and "T" (time) dimensions, independently.
If X is not a formatted dlarray object, and
dim is not a positive integer, then you must also specify the
layout of the input data using the FMT argument.
Data Types: single | double
Complex Number Support: Yes
Weights, specified as a dlarray object or a numeric array.
If weights is an unformatted dlarray or a
numeric array, then the size of first dimension of weights must
match the number of output channels. If weights is a formatted
dlarray, the size of the "C" (channel) dimension
must match the number of output channels.
The size of the second dimension of
weights must match the product of the sizes of the sizes of the
dimensions specified by the dim argument. (since R2026a)
Before R2026a: The size of the second dimension of
weights must match the product of the sizes of the sizes of the
"S" (spatial), "C" (channel), and
"U" (unspecified) dimensions of the input
X.
Data Types: single | double
Complex Number Support: Yes
Bias, specified as a dlarray object or a numeric array.
If bias is an unformatted dlarray or a numeric
array, then the size of first dimension of bias must match the
number of output channels. If bias is a formatted
dlarray, the size of the "C" (channel) dimension
must match the number of output channels. The remaining dimensions must be
singleton.
Data Types: single | double
Complex Number Support: Yes
Since R2026a
Operation dimension, specified as one of these values:
"spatial-channel-unspecified"— Flatten the"S"(spatial),"C"(channel), and"U"(unspecified) dimensions of the input data, then multiply by the weights matrix and add the bias vector for each element in the"B"(batch) and"T"(time) dimensions, independently."spatial-channel"— Flatten the"S"(spatial) and"C"(channel) dimensions of the input data, then multiply by the weights matrix and add the bias vector for each element in the"B"(batch),"T"(time), and"U"(unspecified) dimensions, independently.positive integer — Use the specified dimension of
Xas the inner dimension of the matrix multiplicationweights*Xin thefullyconnectoperation, and apply the operation independently for each of the remaining dimensions.
Data Types: single | double | char | string
Description of the data dimensions, specified as a character vector or string scalar.
A data format is a string of characters, where each character describes the type of the corresponding data dimension.
The characters are:
"S"— Spatial"C"— Channel"B"— Batch"T"— Time"U"— Unspecified
For example, consider an array that represents a batch of sequences where the first,
second, and third dimensions correspond to channels, observations, and time steps,
respectively. You can describe the data as having the format "CBT"
(channel, batch, time).
You can specify multiple dimensions labeled "S" or "U".
You can use the labels "C", "B", and
"T" once each, at most. The software ignores singleton trailing
"U" dimensions after the second dimension.
If the input data is not a formatted dlarray object, then you must
specify the FMT option.
For more information, see Deep Learning Data Formats.
Data Types: char | string
Output Arguments
Weighted output features, returned as a dlarray. The output
Y has the same underlying data type as the input
X.
If the input X is a formatted dlarray, the
output Y has one dimension labeled 'C'
representing the output features, and the same number of 'B' or
'T' dimensions as the input X, if either or both
are present. If X has no 'B' or
'T' dimensions, Y has the format
'CB', where the 'B' dimension is
singleton.
If the input X is not a formatted dlarray,
output Y is unformatted. The first dimension of Y
contains the output features. Other dimensions of Y correspond to the
'B' and 'T' dimensions of X,
if either or both are present, and are provided in the same order as in
FMT. If X has no 'B' or
'T' dimensions, the first dimension of Y
contains the output features and the second dimension is singleton.
Algorithms
The fully connect operation multiplies the input vectors by a weight matrix and then adds a bias vector.
The fullyconnect operation flattens
the dimensions specified by the dim argument, then multiplies by the
weights matrix and adds the bias vector for each element in remaining dimensions,
independently. (since R2026a)
Before R2026a: The fullyconnect operation
flattens the "S" (spatial), "C" (channel), and
"U" (unspecified) dimensions of the input data, then multiplies by
the weights matrix and adds the bias vector for each element in the "B"
(batch) and "T" (time) dimensions, independently.
For each of the flattened vectors x of the input, the corresponding output vector y is given by
where W and b denote the weights and bias, respectively, and i and k index over the flattened input operation dimension and the output dimension, respectively.
For example, for an array that represents a batch of sequences where the first, second, and third dimensions correspond to channels, observations, and time steps, respectively, and a fully connect operation that operates over the channel dimension only, the output is given by
where X and Y are the input and output data, respectively, i, n, and t index over the input channels, observations, and time steps, respectively, and k indexes over the output dimension.
Most deep learning networks and functions operate on different dimensions of the input data in different ways.
For example, an LSTM operation iterates over the time dimension of the input data, and a batch normalization operation normalizes over the batch dimension of the input data.
To provide input data with labeled dimensions or input data with additional layout information, you can use data formats.
A data format is a string of characters, where each character describes the type of the corresponding data dimension.
The characters are:
"S"— Spatial"C"— Channel"B"— Batch"T"— Time"U"— Unspecified
For example, consider an array that represents a batch of sequences where the first,
second, and third dimensions correspond to channels, observations, and time steps,
respectively. You can describe the data as having the format "CBT"
(channel, batch, time).
To create formatted input data, create a dlarray object and specify the format using the second
argument.
To provide additional layout information with unformatted data, specify the format
using the FMT argument.
You can also use unformatted data without specifying the layout information by
specifying the operation dimension as a positive integer using the dim argument. In
this case, the software operates on the unformatted data directly.
For more information, see Deep Learning Data Formats.
Extended Capabilities
Usage notes and limitations:
The
dimargument must be"spatial-channel-unspecified".
Refer to the usage notes and limitations in the C/C++ Code Generation section. The same usage notes and limitations apply to GPU code generation.
The fullyconnect function
supports GPU array input with these usage notes and limitations:
When at least one of the following input arguments is a
gpuArrayor adlarraywith underlying data of typegpuArray, this function runs on the GPU:Xweightsbias
For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Version History
Introduced in R2019bThe fullyconnect function supports complex-valued inputs and
outputs. The function applies the same underlying operation to complex-valued input data as
it does to real-valued input data and returns complex-valued data where applicable.
Specify the dimension of the input data that the fullyconnect
function operates over using the dim
argument.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Seleccione un país/idioma
Seleccione un país/idioma para obtener contenido traducido, si está disponible, y ver eventos y ofertas de productos y servicios locales. Según su ubicación geográfica, recomendamos que seleccione: .
También puede seleccionar uno de estos países/idiomas:
Cómo obtener el mejor rendimiento
Seleccione China (en idioma chino o inglés) para obtener el mejor rendimiento. Los sitios web de otros países no están optimizados para ser accedidos desde su ubicación geográfica.
América
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)