Search code examples
pythonmatrixnormalizationtheano

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.


Solution

  • 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)