Normalize the matrix in a row in the anano

Suppose I have an N matrix with size n_i x n_o , and I want to normalize it differently, i.e. the sum of each row must be one. How can I do this in theano?

Motivation: using softmax returns an error back to me, so I try to somehow get around this by implementing my own version of softmax.

+6
source share
2 answers

See if the following suits you:

 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) 
+9
source

or you can also use

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

Source: https://habr.com/ru/post/982247/


All Articles