假设我有一个大小为 n_i x n_o
的矩阵 N
,我想按行对其进行归一化,即,
每行的总和应该是一个。我怎么能在theano中做到这一点?
动机:使用 softmax 会为我返回错误,所以我尝试通过实现我自己的 softmax 版本来回避它。
最佳答案
看看下面的内容是否对你有用:
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)
关于python - 在theano中按行规范化矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28427416/