Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling matrix multiplication in log space in Python

Tags:

python

numpy

I am implementing a Hidden Markov Model and thus am dealing with very small probabilities. I am handling the underflow by representing variables in log space (so x → log(x)) which has the side effect that multiplication is now replaced by addition and addition is handled via numpy.logaddexp or similar.

Is there an easy way to handle matrix multiplication in log space?

like image 845
BrandonHoughton Avatar asked Apr 07 '16 04:04

BrandonHoughton


People also ask

How do you accept a matrix multiplication input in Python?

Step1: input two matrix. Step 2: nested for loops to iterate through each row and each column. Step 3: take one resultant matrix which is initially contains all 0. Then we multiply each row elements of first matrix with each elements of second matrix, then add all multiplied value.

Is NP dot () and NP Matmul () the same?

The numpy dot() function returns the dot product of two arrays. The result is the same as the matmul() function for one-dimensional and two-dimensional arrays.

Is matrix multiplication possible in Python?

Matrix multiplication is only possible if the column of the second matrix is equal to rows of the first. In Python, a matrix can be represented in the form of a nested list ( a list inside a list ).


1 Answers

This is the best way I could come up with to do it.

from scipy.special import logsumexp
def log_space_product(A,B):
    Astack = np.stack([A]*A.shape[0]).transpose(2,1,0)
    Bstack = np.stack([B]*B.shape[1]).transpose(1,0,2)
    return logsumexp(Astack+Bstack, axis=0)

The inputs A and B are the logs of the matrices A0 and B0 you want to multiply, and the functions returns the log of A0B0. The idea is that the i,j spot in log(A0B0) is the log of the dot product of the ith row of A0 and the jth column of B0. So it is the logsumexp of the ith row of A plus the jth column of B.

In the code, Astack is built so the i,j spot is a vector containing the ith row of A, and Bstack is built so the i,j spot is a vector containing the jth column of B. Thus Astack + Bstack is a 3D tensor whose i,j spot is the ith row of A plus the jth column of B. Taking logsumexp with axis = 0 then gives the desired result.

like image 183
Erik Parkinson Avatar answered Sep 18 '22 00:09

Erik Parkinson