fullyconnect
Sum all weighted input data and apply a bias
Description
The fully connect operation multiplies the input 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
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 unformatted
dlarray
, 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 a
dlarray
.
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 unformatted
dlarray
, 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 formatted
dlarray
, 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 input
X
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 unformatted
dlarray
, 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 of
weights
.
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 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
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
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
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)