Main Content

fullyconnect

Sum all weighted input data and apply a bias

Since R2019b

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.

example

Y = fullyconnect(X,weights,bias) computes the weighted sum of the spatial, channel, and unspecified data in X 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 = fullyconnect(X,weights,bias,'DataFormat',FMT) also specifies the dimension format FMT when X is not a formatted dlarray. The output Y is an unformatted dlarray.

Examples

collapse all

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

collapse all

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, 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 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

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" at most once. 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

collapse all

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

collapse all

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™.

Version History

Introduced in R2019b