假设我有一个大小为
动机:使用softmax函数返回错误,因此我尝试通过实现自己版本的softmax函数来解决问题。
n_i x n_o
的矩阵N
,我想逐行对其进行归一化,即每一行的和应该为1。我该如何在Theano中实现这个操作?动机:使用softmax函数返回错误,因此我尝试通过实现自己版本的softmax函数来解决问题。
n_i x n_o
的矩阵N
,我想逐行对其进行归一化,即每一行的和应该为1。我该如何在Theano中实现这个操作?import theano
import theano.tensor as T
m = T.matrix(dtype=theano.config.floatX)
m_normalized = m / m.sum(axis=1).reshape((m.shape[0], 1))
f = theano.function([m], m_normalized)
import numpy as np
a = np.exp(np.random.randn(5, 10)).astype(theano.config.floatX)
b = f(a)
c = a / a.sum(axis=1)[:, np.newaxis]
from numpy.testing import assert_array_equal
assert_array_equal(b, c)
sum
后面使用 reshape
,我认为使用 keepdims=True
会更清晰。 - AlbertT.setsubtensor
来实际实现决策机制。 - eickenbergnumpy.testing.assert_array_almost_equal
来使比较更加宽松。请随意编辑答案! - eickenberg或者您也可以使用
m/m.norm(1, axis=1).reshape((m.shape[0], 1))