Implement softmax function with numpy array

2019-08-22 08:31发布

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.

3条回答
再贱就再见
2楼-- · 2019-08-22 08:50

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
查看更多
\"骚年 ilove
3楼-- · 2019-08-22 08:58

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)
查看更多
我只想做你的唯一
4楼-- · 2019-08-22 08:58

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)
查看更多
登录 后发表回答