log_softmax¶
- paddle.nn.functional. log_softmax ( x, axis=- 1, dtype=None, name=None ) [source]
-
This operator implements the log_softmax layer. The calculation process is as follows:
\[\begin{split}\begin{aligned} log\_softmax[i, j] &= log(softmax(x)) \\ &= log(\frac{\exp(X[i, j])}{\sum_j(\exp(X[i, j])}) \end{aligned}\end{split}\]- Parameters
-
x (Tensor) – The input Tensor with data type float32, float64.
axis (int, optional) – The axis along which to perform log_softmax calculations. It should be in range [-D, D), where D is the dimensions of
x
. Ifaxis
< 0, it works the same way as \(axis + D\) . Default is -1.dtype (str|np.dtype|core.VarDesc.VarType, optional) – The desired data type of the output tensor. If dtype is specified,
x
is casted todtype
before the operation is performed. This is useful for preventing data type overflows. Supported dtype: float32, float64. Ifdtype
is None, the output Tensor has the same dtype as x. Default is None.name (str, optional) – For details, please refer to Name. Generally, no setting is required. Default: None.
- Returns
-
A Tensor with the same shape and data type (use
dtype
if it is specified) as x.
Examples
>>> import paddle >>> import paddle.nn.functional as F >>> x = [[[-2.0, 3.0, -4.0, 5.0], ... [3.0, -4.0, 5.0, -6.0], ... [-7.0, -8.0, 8.0, 9.0]], ... [[1.0, -2.0, -3.0, 4.0], ... [-5.0, 6.0, 7.0, -8.0], ... [6.0, 7.0, 8.0, 9.0]]] >>> x = paddle.to_tensor(x) >>> out1 = F.log_softmax(x) >>> out2 = F.log_softmax(x, dtype='float64') >>> #out1's data type is float32; out2's data type is float64 >>> #out1 and out2's value is as follows: >>> print(out1) Tensor(shape=[2, 3, 4], dtype=float32, place=Place(cpu), stop_gradient=True, [[[-7.12783957 , -2.12783957 , -9.12783909 , -0.12783945 ], [-2.12705135 , -9.12705135 , -0.12705141 , -11.12705135], [-16.31326103, -17.31326103, -1.31326187 , -0.31326184 ]], [[-3.05181193 , -6.05181217 , -7.05181217 , -0.05181199 ], [-12.31326675, -1.31326652 , -0.31326646 , -15.31326675], [-3.44018984 , -2.44018984 , -1.44018972 , -0.44018975 ]]]) >>> print(out2) Tensor(shape=[2, 3, 4], dtype=float64, place=Place(cpu), stop_gradient=True, [[[-7.12783948 , -2.12783948 , -9.12783948 , -0.12783948 ], [-2.12705141 , -9.12705141 , -0.12705141 , -11.12705141], [-16.31326180, -17.31326180, -1.31326180 , -0.31326180 ]], [[-3.05181198 , -6.05181198 , -7.05181198 , -0.05181198 ], [-12.31326640, -1.31326640 , -0.31326640 , -15.31326640], [-3.44018970 , -2.44018970 , -1.44018970 , -0.44018970 ]]])