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 layerGraph object or Layer array, use the following layer:

example

dlY = fullyconnect(dlX,weights,bias) computes the weighted sum of the spatial, channel, and unspecified data in dlX using the weights specified by weights, and adds a bias. The input dlX must be a formatted dlarray. The output dlY is a formatted dlarray.

dlY = fullyconnect(dlX,weights,bias,'DataFormat',FMT) also specifies the dimension format FMT when dlX is not a formatted dlarray. The output dlY 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);
dlX = 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.

dlY = fullyconnect(dlX,weights,bias);
dlY =
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 dlY 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 dlX is not a formatted dlarray, you must specify the dimension label format using 'DataFormat',FMT. If dlX 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 dlX for each output feature specified by weights. The size of each 'B' or 'T' dimension of dlX 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 dlX 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

Dimension order of unformatted input data, specified as the comma-separated pair consisting of 'DataFormat' and a character array or string FMT that provides a label for each dimension of the data. Each character in FMT must be one of the following:

• 'S' — Spatial

• 'C' — Channel

• 'B' — Batch (for example, samples and observations)

• 'T' — Time (for example, sequences)

• 'U' — Unspecified

You can specify multiple dimensions labeled 'S' or 'U'. You can use the labels 'C', 'B', and 'T' at most once.

You must specify 'DataFormat',FMT when the input data is not a formatted dlarray.

Example: 'DataFormat','SSCB'

Data Types: char | string

Output Arguments

collapse all

Weighted output features, returned as a dlarray. The output dlY has the same underlying data type as the input dlX.

If the input dlX is a formatted dlarray, the output dlY has one dimension labeled 'C' representing the output features, and the same number of 'B' or 'T' dimensions as the input dlX, if either or both are present. If dlX has no 'B' or 'T' dimensions, dlY has the format 'CB', where the 'B' dimension is singleton.

If the input dlX is not a formatted dlarray, output dlY is unformatted. The first dimension of dlY contains the output features. Other dimensions of dlY correspond to the 'B' and 'T' dimensions of dlX, if either or both are present, and are provided in the same order as in FMT. If dlX has no 'B' or 'T' dimensions, the first dimension of dlY contains the output features and the second dimension is singleton.

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.

Extended Capabilities

GPU Code GenerationGenerate CUDA® code for NVIDIA® GPUs using GPU Coder™.

Introduced in R2019b