Main Content

softmax

Apply softmax activation to channel dimension

Description

The softmax activation operation applies the softmax function to the channel dimension of the input data.

The softmax function normalizes the value of the input data across the channel dimension such that it sums to one. You can regard the output of the softmax function as a probability distribution.

Note

This function applies the softmax operation to dlarray data. If you want to apply softmax within a dlnetwork object, use softmaxLayer.

Y = softmax(X) computes the softmax activation of the input X by applying the softmax transfer function to the channel dimension of the input data. All values in Y are between 0 and 1, and sum to 1. The input X must be a formatted dlarray. The output Y is a formatted dlarray with the same dimension format as X.

example

Y = softmax(X,'DataFormat',FMT) also specifies dimension format FMT when X is not a formatted dlarray. The output Y is an unformatted dlarray with the same dimension order as X.

Examples

collapse all

Use the softmax function to set all values in the input data to values between 0 and 1 that sum to 1 over all channels.

Create the input classification data as two observations of random variables. The data can be in any of 10 categories.

numCategories = 10;
observations = 2;

X = rand(numCategories,observations);
X = dlarray(X,'CB');

Compute the softmax activation.

Y = softmax(X)
totalProb = sum(Y,1)
Y =

  10(C) x 2(B) dlarray

    0.1151    0.0578
    0.1261    0.1303
    0.0579    0.1285
    0.1270    0.0802
    0.0959    0.1099
    0.0562    0.0569
    0.0673    0.0753
    0.0880    0.1233
    0.1328    0.1090
    0.1337    0.1288
totalProb =

  1(C) x 2(B) dlarray

    1.0000    1.0000

All values in Y range between 0 and 1. The values over all channels sum to 1 for each observation.

Input Arguments

collapse all

Input data, specified as a formatted dlarray or an unformatted dlarray. When X is not a formatted dlarray, you must specify the dimension label format using 'DataFormat',FMT.

X must contain a 'C' channel dimension.

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

Softmax activations, returned as a dlarray. All values in Y are between 0 and 1. The output Y has the same underlying data type as the input X.

If the input data X is a formatted dlarray, Y has the same dimension format as X. If the input data is not a formatted dlarray, Y is an unformatted dlarray with the same dimension order as the input data.

Algorithms

collapse all

Extended Capabilities

expand all

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