chainer.functions.softmax

chainer.functions.softmax(x, axis=1)[source]

Softmax function.

This function computes its softmax along an axis. Let \(c = (c_1, c_2, \dots, c_D)\) be the slice of x along with the axis. For each slice \(c\), it computes the function \(f(c)\) defined as \(f(c)={\exp(c) \over \sum_{d} \exp(c_d)}\).

Parameters
  • x (Variable or N-dimensional array) – Input variable. A \(n\)-dimensional (\(n \geq 2\)) float array.

  • axis (int) – The axis along which the softmax is to be computed.

Returns

Output variable. A \(n\)-dimensional (\(n \geq 2\)) float array, which is the same shape with x.

Return type

Variable

Example

>>> x = np.array([[0, 1, 2], [0, 2, 4]], np.float32)
>>> x
array([[0., 1., 2.],
       [0., 2., 4.]], dtype=float32)
>>> y = F.softmax(x, axis=1)
>>> y.array
array([[0.09003057, 0.24472848, 0.66524094],
       [0.01587624, 0.11731043, 0.86681336]], dtype=float32)
>>> F.sum(y, axis=1).array
array([1., 1.], dtype=float32)