Lets say I have a Matrix N
with size n_i x n_o
and I want to normalize it row-wise,i.e.,
the sum of each row should be one. How can I do this in theano?
Motivation: using softmax returns back error for me, so I try to kind of sidestep it by implementing my own version of softmax.
To normalise rows, just divide by the norm. For example, using L₂ normalisation: >>> l2norm = np. sqrt((X * X).
Normalization of 1D-Array Suppose, we have an array = [1,2,3] and to normalize it in range [0,1] means that it will convert array [1,2,3] to [0, 0.5, 1] as 1, 2 and 3 are equidistant. This can also be done in a Range i.e. instead of [0,1], we will use [3,7].
See if the following is useful for 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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With