1

My current function is as follows:

def soft_max(z):
    t = np.exp(z)
    a = np.exp(z) / np.sum(t, axis=1)
    return a

However I get the error: ValueError: operands could not be broadcast together with shapes (20,10) (20,) since np.sum(t, axis=1) isn't a scalar.

I want to have t / the sum of each row but I don't know how to do this.

Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
Wizard
  • 1,533
  • 4
  • 19
  • 32
  • 1
    Use `keepdims` : `np.sum(t, axis=1, keepdims=True)`? – Divakar May 19 '18 at 08:26
  • I tried that and i get a lot of `RuntimeWarning: overflow encountered in exp` – Wizard May 19 '18 at 08:29
  • 1
    https://stackoverflow.com/questions/40726490/overflow-error-in-pythons-numpy-exp-function, https://nolanbconaway.github.io/blog/2017/softmax-numpy, https://stackoverflow.com/questions/9559346/deal-with-overflow-in-exp-using-numpy? – Divakar May 19 '18 at 08:31

3 Answers3

3

You want to do something like (see this post)

def softmax(x, axis=None):
    x = x - x.max(axis=axis, keepdims=True)
    y = np.exp(x)
    return y / y.sum(axis=axis, keepdims=True)
Paul Panzer
  • 51,835
  • 3
  • 54
  • 99
3

As of version 1.2.0, scipy includes softmax as a special function:

https://scipy.github.io/devdocs/generated/scipy.special.softmax.html

Use the axis argument do run it over rows.

from scipy.special import softmax
softmax(arr, axis=0)
Nolan Conaway
  • 2,639
  • 1
  • 26
  • 42
2

suppose your z is a 2 dimensional array, try

def soft_max(z):
    t = np.exp(z)
    a = np.exp(z) / np.sum(t, axis=1).reshape(-1,1)
    return a
lelouchkako
  • 123
  • 1
  • 7
  • sorry, `np.sum(t, axis=1, keepdims=True)` is better than using `.reshape`. they both are doing the same thing. `Overflow` may be caused by your input data I think. – lelouchkako May 19 '18 at 09:23