MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / Softmax

Class Softmax

imperative/python/megengine/module/activation.py:10–46  ·  view source on GitHub ↗

r"""Applies a softmax function. Softmax is defined as: .. math:: \text{Softmax}(x_{i}) = \frac{exp(x_i)}{\sum_j exp(x_j)} It is applied to all elements along axis, and rescales elements so that they stay in the range `[0, 1]` and sum to 1. Args: axis: Along whi

Source from the content-addressed store, hash-verified

8
9
10class Softmax(Module):
11 r"""Applies a softmax function. Softmax is defined as:
12
13 .. math::
14 \text{Softmax}(x_{i}) = \frac{exp(x_i)}{\sum_j exp(x_j)}
15
16 It is applied to all elements along axis, and rescales elements so that
17 they stay in the range `[0, 1]` and sum to 1.
18
19 Args:
20 axis: Along which axis softmax will be applied. By default,
21 softmax will be applyed along the highest ranked axis.
22
23 Shape:
24 - Input: :math:`(*)` where `*` means, any number of additional
25 dimensions
26 - Output: :math:`(*)`, same shape as the input
27
28 Examples:
29 >>> import numpy as np
30 >>> data = mge.tensor(np.array([-2,-1,0,1,2]).astype(np.float32))
31 >>> softmax = M.Softmax()
32 >>> output = softmax(data)
33 >>> with np.printoptions(precision=6):
34 ... print(output.numpy())
35 [0.011656 0.031685 0.086129 0.234122 0.636409]
36 """
37
38 def __init__(self, axis=None, **kwargs):
39 super().__init__(**kwargs)
40 self.axis = axis
41
42 def forward(self, inputs):
43 return softmax(inputs, self.axis)
44
45 def _module_info_string(self) -> str:
46 return "axis={axis}".format(axis=self.axis)
47
48
49class Sigmoid(Module):

Callers 3

__init__Method · 0.90
__init__Method · 0.90
getFunction · 0.85

Calls

no outgoing calls

Tested by 2

__init__Method · 0.72
__init__Method · 0.72