Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

normalize a matrix row-wise in theano

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.

like image 245
TNM Avatar asked Feb 10 '15 08:02

TNM


People also ask

How do you normalize a row of a matrix?

To normalise rows, just divide by the norm. For example, using L₂ normalisation: >>> l2norm = np. sqrt((X * X).

How do you normalize a matrix using Numpy?

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].


1 Answers

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)
like image 191
eickenberg Avatar answered Oct 23 '22 14:10

eickenberg