fullyconnect - Sum all weighted input data and apply a bias - MATLAB (original) (raw)
Sum all weighted input data and apply a bias
Syntax
Description
The fully connect operation multiplies the input by a weight matrix and then adds a bias vector.
[Y](#mw%5F9d4a370d-a499-4445-8261-4a4e9b182bcd) = fullyconnect([X](#mw%5Fa3ffc0a1-e206-4741-baad-900c6be1f93f),[weights](#mw%5F964cad8f-e244-4aee-8b7f-22efbcdee264),[bias](#mw%5Ff4002b92-a657-4e00-a36a-d788bdf34fce))
computes the weighted sum of the spatial, channel, and unspecified data inX
using the weights specified by weights
, and adds a bias. The input X
must be a formatted dlarray
. The output Y
is a formatted dlarray
.
[Y](#mw%5F9d4a370d-a499-4445-8261-4a4e9b182bcd) = fullyconnect([X](#mw%5Fa3ffc0a1-e206-4741-baad-900c6be1f93f),[weights](#mw%5F964cad8f-e244-4aee-8b7f-22efbcdee264),[bias](#mw%5Ff4002b92-a657-4e00-a36a-d788bdf34fce),'DataFormat',[FMT](#mw%5Ff2f4cfbe-893d-4381-b494-9d4856aa61de%5Fsep%5Fmw%5F30d6528f-469b-47fb-a774-e80032eca842))
also specifies the dimension format FMT
when X
is not a formatted dlarray
. The output Y
is an unformatteddlarray
.
Examples
Fully Connect All Input Data to Output Features
The fullyconnect
function uses the weighted sum to connect all inputs of an observation to each output feature.
Create the input data as a single observation of random values with a height and width of 12 and 32 channels.
height = 12; width = 12; channels = 32; observations = 1;
X = rand(height,width,channels,observations); X = dlarray(X,'SSCB');
Create the learnable parameters. For this operation there are ten output features.
outputFeatures = 10;
weights = ones(outputFeatures,height,width,channels); bias = ones(outputFeatures,1);
Apply the fullyconnect
operation.
Y = fullyconnect(X,weights,bias);
Y = 10(C) × 1(B) dlarray
1.0e+03 *
2.3266
2.3266
2.3266
2.3266
2.3266
2.3266
2.3266
2.3266
2.3266
2.3266
The output Y
is a 2-D dlarray
with one channel dimension of size ten and one singleton batch dimension.
Input Arguments
X
— Input data
dlarray
| numeric array
Input data, specified as a formatted dlarray
, an unformatteddlarray
, or a numeric array. When X
is not a formatted dlarray
, you must specify the dimension label format using'DataFormat',FMT
. If X
is a numeric array, at least one of weights
or bias
must be adlarray
.
The fullyconnect
operation sums over the 'S'
,'C'
, and 'U'
dimensions of X
for each output feature specified by weights
. The size of each'B'
or 'T'
dimension of X
is preserved.
Data Types: single
| double
weights
— Weights
dlarray
| numeric array
Weights, specified as a formatted dlarray
, an unformatteddlarray
, or a numeric array.
If weights
is an unformatted dlarray
or a numeric array, the first dimension of weights
must match the number of output features. If weights
is a formatteddlarray
, the size of the 'C'
dimension must match the number of output features. weights
must contain the same number of elements as the combined size of the 'S'
,'C'
, and 'U'
dimensions of inputX
multiplied by the number of output features.
Data Types: single
| double
bias
— Bias constant
dlarray
vector | numeric vector
Bias constant, specified as a formatted dlarray
, an unformatteddlarray
, or a numeric array.
Each element of bias
is the bias applied to the corresponding feature output. The number of elements of bias
must match the number of output features specified by the first dimension ofweights
.
If bias
is a formatted dlarray
, the nonsingleton dimension must be a channel dimension labeled 'C'
.
Data Types: single
| double
FMT
— Description of data dimensions
character vector | string scalar
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 containing a batch of sequences where the first, second, and third dimensions correspond to channels, observations, and time steps, respectively. You can specify that this array has 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
Y
— Weighted output features
dlarray
Weighted output features, returned as a dlarray
. The outputY
has the same underlying data type as the inputX
.
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 inFMT
. If X
has no 'B'
or'T'
dimensions, the first dimension of Y
contains the output features and the second dimension is singleton.
Algorithms
Fully Connect Operation
The fullyconnect
function connects all outputs of the previous operation to the outputs of the fullyconnect
function. For more information, see the definition of Fully Connected Layer on the fullyConnectedLayer reference page.
Deep Learning Array Formats
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 containing a batch of sequences where the first, second, and third dimensions correspond to channels, observations, and time steps, respectively. You can specify that this array has 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.
For more information, see Deep Learning Data Formats.
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™.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
The fullyconnect
function supports GPU array input with these usage notes and limitations:
- When at least one of the following input arguments is a
gpuArray
or adlarray
with underlying data of typegpuArray
, this function runs on the GPU:X
weights
bias
For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Version History
Introduced in R2019b