Softmax#

  • Domain: ai.onnx

  • Since version: 13

The operator computes the normalized exponential values for the given input. The “axis” attribute indicates the dimension along which Softmax is performed. The output tensor has the same shape as the input tensor.

Inputs

  • input (T): The input tensor of rank >= axis.

Outputs

  • output (T): The output values with the same shape as the input tensor.

Attributes

  • axis (int): Describes the dimension Softmax will be performed on. Negative value means counting dimensions from the back.

Type Constraints

  • T: Constrain input and output types to float tensors. Allowed types: tensor(bfloat16), tensor(double), tensor(float), tensor(float16).

Examples#

test_cc_softmax

Node:
  Softmax(input) -> (output)
  Attributes:
    axis = 1
Inputs:
  input: shape=(2, 3), dtype=float32
    [[1., 2., 3.],
     [1., 2., 3.]]

Outputs:
  output: shape=(2, 3), dtype=float32
    [[0.09003057, 0.24472848, 0.66524094],
     [0.09003057, 0.24472848, 0.66524094]]

test_cc_softmax_axis_0

Node:
  Softmax(input) -> (output)
  Attributes:
    axis = 0
Inputs:
  input: shape=(3, 4, 5), dtype=float32
    [[[-0.77003425, -2.6519022 ,  0.63292354, -0.36012268,  1.752512  ],
      [-0.16575179, -0.4585369 , -1.4947453 ,  2.2959683 ,  1.4827057 ],
      [ 0.23412685, -0.2121899 ,  0.49145597, -0.36653152,  0.05919014],
      [-0.75085527, -0.5321288 , -1.1784475 ,  0.481096  ,  0.3832116 ]],

     [[ 1.6185511 , -0.21927364,  1.0403228 , -0.49840263, -1.0505093 ],
      [ 0.08702205,  0.9696927 , -0.22924021, -0.07851556, -0.35033914],
      [-1.9294221 ,  0.6123335 ,  1.6122967 , -1.027974  ,  0.46176022],
      [ 0.5693595 ,  1.7973237 ,  0.14330323, -0.9876707 ,  0.98740804]],

     [[-0.92656296, -1.8051703 , -0.3287407 , -0.4324619 ,  0.1958068 ],
      [ 0.95511854,  0.07167032, -0.8574417 , -2.2715914 , -3.019159  ],
      [ 1.4794749 , -0.8913689 , -0.29818887,  1.2469095 , -0.475797  ],
      [ 0.19670746,  0.8043982 ,  1.0651342 , -1.3017774 , -0.09921211]]]

Outputs:
  output: shape=(3, 4, 5), dtype=float32
    [[[0.07841185, 0.06793112, 0.3466012 , 0.3570064 , 0.78649974],
      [0.18673736, 0.14554787, 0.1553698 , 0.9062521 , 0.8539607 ],
      [0.21790953, 0.26400363, 0.22116856, 0.15299499, 0.32453406],
      [0.13654411, 0.06632127, 0.0705367 , 0.7151216 , 0.29010174]],

     [[0.85453755, 0.77365255, 0.5209083 , 0.31090075, 0.04768281],
      [0.24044153, 0.6071249 , 0.5507677 , 0.08433809, 0.13657041],
      [0.02504142, 0.6021378 , 0.6784196 , 0.07896169, 0.48539382],
      [0.5112519 , 0.68127304, 0.2645113 , 0.16462761, 0.5308227 ]],

     [[0.06705052, 0.15841629, 0.13249044, 0.33209282, 0.1658174 ],
      [0.5728211 , 0.2473272 , 0.29386243, 0.00940986, 0.00946897],
      [0.7570491 , 0.13385858, 0.10041187, 0.76804334, 0.19007207],
      [0.35220397, 0.25240567, 0.66495204, 0.12025077, 0.17907558]]]

test_cc_softmax_axis_1

Node:
  Softmax(input) -> (output)
  Attributes:
    axis = 1
Inputs:
  input: shape=(3, 4, 5), dtype=float32
    [[[ 0.3916829 , -0.3541392 ,  0.7372205 , -0.05390637,  0.03446517],
      [ 0.29420647, -1.9821024 ,  0.7730614 , -1.8590308 ,  1.0029383 ],
      [ 0.08631501, -0.36509955, -1.4523431 , -0.11940911, -0.9071753 ],
      [ 1.7673455 ,  0.58039784, -1.0156238 ,  0.3451533 ,  1.0078589 ]],

     [[ 0.7516545 ,  0.05013755, -0.47453195, -0.6347338 ,  0.5335269 ],
      [-0.17399625, -1.654322  , -0.9808288 ,  0.9759014 , -0.09168785],
      [ 0.8208916 , -0.4187241 ,  1.2662531 , -0.87888324,  0.12340371],
      [-1.5375786 , -1.6055291 , -0.5015949 ,  1.5512116 ,  0.03221842]],

     [[-0.73663497,  1.0570596 , -1.0541275 , -1.026609  ,  0.16088843],
      [-0.03075927,  0.7164851 ,  0.16134222,  0.17368656, -0.11242106],
      [-0.65411115,  0.4270719 , -2.4425476 , -0.95742875,  2.05589   ],
      [ 0.54944086,  0.87723285,  0.03101575, -0.2751654 , -0.1453079 ]]]

Outputs:
  output: shape=(3, 4, 5), dtype=float32
    [[[0.15147673, 0.21135113, 0.430712  , 0.27843726, 0.14990664],
      [0.13740812, 0.04149436, 0.4464291 , 0.04579012, 0.39484203],
      [0.11161596, 0.20904729, 0.04822493, 0.26078337, 0.05846172],
      [0.59949917, 0.5381072 , 0.07463394, 0.41498917, 0.39678967]],

     [[0.3892099 , 0.5003629 , 0.12080443, 0.06374128, 0.35657585],
      [0.15423355, 0.09100132, 0.07281166, 0.31908825, 0.1908203 ],
      [0.41711247, 0.31308413, 0.6888049 , 0.04993305, 0.2366125 ],
      [0.03944415, 0.09555165, 0.11757896, 0.56723744, 0.21599136]],

     [[0.1293627 , 0.32474127, 0.13190506, 0.13310643, 0.10929369],
      [0.26203963, 0.23100843, 0.4447677 , 0.44205946, 0.08315691],
      [0.14049107, 0.17295666, 0.03290624, 0.14264072, 0.7270828 ],
      [0.46810657, 0.2712937 , 0.390421  , 0.28219333, 0.08046661]]]

test_cc_softmax_axis_2

Node:
  Softmax(input) -> (output)
  Attributes:
    axis = 2
Inputs:
  input: shape=(3, 4, 5), dtype=float32
    [[[ 0.0650013 ,  0.9687623 ,  1.5784289 , -1.4634767 , -1.8611803 ],
      [-0.8757258 ,  0.66140944, -0.15131198, -1.8303125 ,  2.1479135 ],
      [ 0.5050388 ,  0.01897051,  2.4111621 ,  0.02339889, -1.2596691 ],
      [ 1.3501854 ,  1.0494168 ,  0.2704867 , -0.5183443 , -0.79759043]],

     [[-2.0113213 ,  0.6892964 , -0.18279147,  1.0123395 ,  0.04597066],
      [-1.6271967 ,  0.521242  , -0.12936813,  0.84212583,  0.05689837],
      [ 0.6671226 , -0.04827221,  0.9777075 , -0.70817345,  0.08441816],
      [-1.1849133 ,  1.6668009 ,  0.2700868 , -1.2668471 ,  1.0043492 ]],

     [[ 0.65134346, -0.4325288 , -0.3296435 , -0.39662504,  0.09283573],
      [ 0.01498039,  1.6443186 , -0.18755133, -1.5809691 , -1.2471963 ],
      [ 1.5402834 ,  0.9689904 , -0.20490345,  0.32134694, -0.09456601],
      [-2.2057471 , -0.67284495,  0.5497592 , -0.27588364,  0.86582875]]]

Outputs:
  output: shape=(3, 4, 5), dtype=float32
    [[[0.11942133, 0.2948359 , 0.54244435, 0.02589837, 0.01740011],
      [0.03488485, 0.16225803, 0.07198563, 0.01342966, 0.7174418 ],
      [0.10951694, 0.06735728, 0.73671615, 0.06765623, 0.01875333],
      [0.42534348, 0.3148601 , 0.14448828, 0.06565195, 0.0496561 ]],

     [[0.01980008, 0.2948019 , 0.1232501 , 0.40721717, 0.1549307 ],
      [0.03200467, 0.27432686, 0.14312382, 0.37811717, 0.17242746],
      [0.27289802, 0.13344686, 0.3722937 , 0.068979  , 0.15238248],
      [0.03081485, 0.53363436, 0.13202573, 0.02839073, 0.2751343 ]],

     [[0.37937197, 0.1283351 , 0.14224206, 0.13302654, 0.21702433],
      [0.13508199, 0.6889854 , 0.11031613, 0.02738328, 0.03823324],
      [0.44844392, 0.25327873, 0.07830384, 0.13253506, 0.08743837],
      [0.02007023, 0.09295725, 0.31568465, 0.13825515, 0.43303275]]]

test_cc_softmax_default_axis

Node:
  Softmax(input) -> (output)
Inputs:
  input: shape=(2, 3), dtype=float32
    [[ 1.,  2.,  3.],
     [ 4.,  1., -1.]]

Outputs:
  output: shape=(2, 3), dtype=float32
    [[0.09003057, 0.24472848, 0.66524094],
     [0.94649917, 0.04712342, 0.00637746]]

test_cc_softmax_example

Node:
  Softmax(input) -> (output)
Inputs:
  input: shape=(1, 3), dtype=float32
    [[-1.,  0.,  1.]]

Outputs:
  output: shape=(1, 3), dtype=float32
    [[0.09003057, 0.24472848, 0.66524094]]

test_cc_softmax_large_number

Node:
  Softmax(input) -> (output)
Inputs:
  input: shape=(2, 4), dtype=float32
    [[0.0000e+00, 1.0000e+00, 2.0000e+00, 3.0000e+00],
     [1.0000e+04, 1.0001e+04, 1.0002e+04, 1.0003e+04]]

Outputs:
  output: shape=(2, 4), dtype=float32
    [[0.0320586 , 0.08714432, 0.23688284, 0.6439143 ],
     [0.0320586 , 0.08714432, 0.23688284, 0.6439143 ]]

test_cc_softmax_negative_axis

Node:
  Softmax(input) -> (output)
  Attributes:
    axis = -1
Inputs:
  input: shape=(3, 4, 5), dtype=float32
    [[[-1.0283089 ,  0.87021446,  0.85100865, -0.71898204, -0.25055054],
      [ 0.34155968,  1.1869831 ,  0.00441376, -0.5290829 , -1.7380415 ],
      [ 1.5278401 , -1.0319128 ,  0.11292676,  0.23332347, -0.8829553 ],
      [-1.5090932 ,  0.09225651, -0.41412777, -0.78247404,  0.29668635]],

     [[ 0.9531754 ,  2.3279667 ,  2.1695757 ,  0.9496657 , -1.0766346 ],
      [ 1.6332269 ,  0.6144982 ,  0.8777296 ,  1.041466  , -2.2009137 ],
      [-0.10998508,  0.21957855, -2.160124  ,  1.1755424 ,  1.4141262 ],
      [ 1.1015927 , -1.5782163 ,  0.1234045 ,  1.0722983 , -0.3801983 ]],

     [[ 1.3252872 , -0.42411736, -1.1584891 ,  0.48421076, -0.22948906],
      [ 0.17951094,  0.8101771 ,  1.9313664 , -0.70940554, -0.1407327 ],
      [-0.26629317,  0.6170495 ,  1.305193  ,  0.35255212,  0.17388587],
      [-0.86188096, -0.5423794 ,  1.520908  , -1.5987713 , -0.03819034]]]

Outputs:
  output: shape=(3, 4, 5), dtype=float32
    [[[0.05629313, 0.37581453, 0.36866558, 0.07669982, 0.12252692],
      [0.21803445, 0.5077943 , 0.155634  , 0.09128719, 0.02724996],
      [0.5938085 , 0.04591555, 0.14426379, 0.1627215 , 0.05329068],
      [0.0584741 , 0.29001525, 0.17478369, 0.12092891, 0.35579807]],

     [[0.10574038, 0.41812405, 0.35687557, 0.10536993, 0.01389011],
      [0.41566172, 0.15007618, 0.1952683 , 0.23000726, 0.00898657],
      [0.09322519, 0.1296167 , 0.01199967, 0.33715588, 0.42800257],
      [0.3783681 , 0.02594706, 0.14226328, 0.36744484, 0.08597672]],

     [[0.52637464, 0.09152468, 0.04391416, 0.22699697, 0.1111896 ],
      [0.10223669, 0.1920886 , 0.58942413, 0.0420296 , 0.07422099],
      [0.08589194, 0.2077702 , 0.4134663 , 0.15948266, 0.1333889 ],
      [0.06262127, 0.08619452, 0.6785064 , 0.02997053, 0.14270729]]]

Differences with previous version (11)#

SchemaDiff: Softmax (domain 'ai.onnx')

  • old version: 11

  • new version: 13

  • breaking: yes

Breaking reasons:

  • attribute ‘axis’ (changed): default value changed 1 -> -1

Attributes:

  • [BREAKING] changed ‘axis’: default value changed 1 -> -1

Type constraints:

  • changed ‘T’: added types: [‘tensor(bfloat16)’]

Version History#