Contenido principal

fullyconnect

Sum all weighted input data and apply a bias

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.

Y = fullyconnect(X,weights,bias) applies the fully connect operation to the formatted dlarray object X using the specified weight matrix and bias vector.

example

Y = fullyconnect(X,weights,bias,dim) applies the fully connect operation to the numeric array or dlarray object X over the specified operation dimensions

Y = fullyconnect(___,DataFormat=FMT) also specifies the layout of the input data using the data format FMT.

Examples

collapse all

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

collapse all

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 X as the inner dimension of the matrix multiplication weights*X in the fullyconnect operation, 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

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

Extended Capabilities

expand all

Version History

Introduced in R2019b

expand all