"Opano引数に不正な引数がある"というカスタムOpを実装しようとしました。ここにコードがあります。私が理解している問題は、PyMC3変数をtheano理解可能な型に変換する方法です。Pyamer3のTheanoカスタムOp
import numpy as np
import theano
import theano.tensor as t
from theano import config
config.compute_test_value = 'off'
#true_Data = [1,2]
#values=[]
class trial_Op(theano.Op):
__props__ =()
itypes = [t.dmatrix, t.dmatrix, t.dmatrix]
otypes = [t.dmatrix]
def perform(self,node,inputs,output_storage):
x0 = inputs[0]
x1 = inputs[1]
x2 = inputs[2]
z = output_storage[0]
z[0] = np.add(x0,x1)
z[0] = np.add(z[0],x2)
def grad(self,inputs,output_grads):
return output_grads[0]
Trial_Op = trial_Op()
x1 = t.dmatrix()
x2 = t.dmatrix()
x3 = t.dmatrix()
f = theano.function([x1,x2,x3], trial_Op()(x1,x2,x3))
# the Op works for the
#inp1 = np.random.rand(3,1) # a 2d matrix
#inp2 = np.random.rand(3,1) # a 2d matrix
#inp3 = np.array([[-40]]) # a constant
#print("Op application gives = ", f(inp1,inp2,inp3))
import pymc3 as pm
true_Data = [[1]]
with pm.Model() as model:
x1 = pm.Normal('x1', mu = 0, sd = 0.1)
x2 = pm.Normal('x2', mu = 3, sd = 0.5)
x3 = np.asarray([[4]], dtype='float64')
# x1 = x1.reshape(1,1)
# x2 = x2.reshape(1,1)
sum_of_x1_x2_x3 = f(x1,x2,x3)
z = pm.Normal('z', sum_of_x1_x2_x3, observed = true_Data)
start = {'x1':[[0.1]], 'x2':[[0.1]]}
step = pm.Metropolis()
trace = pm.sample(100, step, start)
pm.traceplot(trace)