pythonpymc3

Python PYMC3 matrix multiplication


I have started to learn PYMC3. i am trying to write a simple matrix multiplication using PYMC3. basically would like to learn and understand how the arithmetic operations can be done in PYMC3.

Below is my code,

import numpy as np
import pymc3 as pm 


dimension_N = 3
min = 0
max = 100

Matrix_A = np.random.randint(min,max,(dimension_N,dimension_N)).astype(np.uint8)
Matrix_B = np.random.randint(min,max,(dimension_N,dimension_N)).astype(np.uint8)
Matrix_C = np.zeros((dimension_N,dimension_N))

with pm.Model() as model:
    c = pm.Normal("c", mu=0, sigma=1)
    a = pm.Normal("a", mu=0, sigma=1, observed=Matrix_A)
    b = pm.Normal("b", mu=0, sigma=1, observed=Matrix_B)
    c = a.dot(b)

    gph = pm.fit()

Not sure if this is the correct code. Can you please help me ? from my observation variable c returns 0. Can you please tell me what went wrong ?


Solution

  • Setting aside that the model here is not clear, we can still answer how matrix multiplication should be done when working with PyMC3 RandomVariable objects. Namely, RV's in PyMC3 are theano.tensor.TensorVaribale objects, and therefore, should use the theano.tensor.dot method to matrix multiply them. E.g.,

    import theano.tensor as tt
    
    tt.dot(a,b)
    

    Generally, consult the Theano Tensor Functionality documentation for API to do efficient math with PyMC3.