I have two numpy matrices a_log2 and b_log2 of log2 elements and I want to perform matrix multiplication between them.
a = np.array([[0.4, 0.4, 0.2],
[0.1, 0.5, 0.4]])
b = np.array([[0.3, 0.7],
[0.5, 0.5],
[0.2, 0.8]])
a_log2 = np.log2(a)
b_log2 = np.log2(b)
I used to perform matrix multiplication of e-based logarithms using scipy.special.logsumexp. Here is the code I use (thanks to Erik Parkinson for his answer in this thread Handling matrix multiplication in log space in Python):
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))
log_sum_exp = logsumexp(Astack+Bstack, axis=0)
return log_sum_exp
Now, I need your help in performing matrix multiplication between a_log2 and b_log2 because scipy.special.logsumexp was not defined for base-2 logarithms.
Note:
I was firstly planning to convert the matrix elements into natural logarithms using a_loge[i, j] = np.log(2**a_log2[i, j]) and b_loge[i, j] = np.log(2**b_log2[i, j]), and then using the aforementioned log_space_product() method to perform the matrix multiplication.
But I resisted myself doing that because the matrices I will eventually work with have > 1000 rows and > 20 columns. (Don't get confused with the numbers of rows and the number of columns here. I do ensure that the matrix multiplication properties are maintained.)