矩阵规范化

2024-06-15 08:06:54 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有一个大小为n_i x n_o的矩阵N,我想按行规范化它,即。, 每行之和应为一。我在剧院怎么做?在

动机:使用softmax会给我返回错误,所以我尝试通过实现我自己的softmax版本来避开它。在


Tags: 版本错误矩阵规范化动机剧院softmax
2条回答

或者你也可以用

m/m.norm(1, axis=1).reshape((m.shape[0], 1))

查看以下内容是否对您有用:

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)

相关问题 更多 >