本文介绍了用numpy数组实现softmax函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我当前的功能如下:
def soft_max(z):
t = np.exp(z)
a = np.exp(z) / np.sum(t, axis=1)
return a
但是我收到错误:ValueError: operands could not be broadcast together with shapes (20,10) (20,)
,因为np.sum(t,axis = 1)不是标量.
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.
我想拥有t / the sum of each row
,但是我不知道该怎么做.
I want to have t / the sum of each row
but I don't know how to do this.
推荐答案
从1.2.0版开始,scipy包含softmax作为特殊功能:
As of version 1.2.0, scipy includes softmax as a special function:
https://scipy.github.io/devdocs/genic /scipy.special.softmax.html
使用axis
参数可以在行上运行它.
Use the axis
argument do run it over rows.
from scipy.special import softmax
softmax(arr, axis=0)
这篇关于用numpy数组实现softmax函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!