誰も私のコードで何が間違っているのか教えてください。pymc3にpymc2を適合させることができません
私は一般的に物理式を解くためのpymc2のカジュアルユーザーです。私はpymc3に適合させるのに苦労しています。ドキュメントは私には不明瞭です。また、私は...私は私の問題が何であるかを知らないためか、フォーラムで私は当てはめ値の最初の推測を取得するためにfind_MAPメソッドを使用
を私の問題を認識しなかったが、この最初の推測であっても内部(completly間違っていません物理的な限界)、警告には離散変数(間違っている)があり、勾配が利用できないことを意味します。
目的は拡散方程式にいくつかのパラメータを当てはめることです:ここではα0、α1およびεは連続的で事前に均一に分布しています。長いデバッグ時間の間、私はコードを反復最適化したので、コード自体は面白いとは思わない。ちょうどpymc2バージョンが正常で動作していることを知っています。私は問題がどこにあるのかわからないので、私は 'simul_DifferentialEq'関数の内部も与えますが、pymc3のものは対応するコメントの下にあります。
C0=Cowl0 = 10 # in mmol/l: concentration at the surface (at t=0), sometimes noted C0
Dsw = 1.6 # in cm^2.d-1
Cdefault = 1e-10 # concentration at t=0, depth>0
# maximum depth and time in the simulation for solving the ED (assuming it begins at x=t=0)
maxdepth = 17 # in cm
maxtime = 1 # in day
#steps in depth and time in the simulation for solving the ED
Deltax = 0.05# in cm
Deltat = 0.02# in day
##############################################
# internal cooking
from numpy import arange, empty, zeros
from solve_ED_crank import sph_2Dfunct
depth = (arange(maxdepth/Deltax +1))*Deltax # in cm
time = (arange(maxtime/Deltat +1))*Deltat # in day
beta = Dsw * Deltat/(2 * Deltax)
matA = zeros([len(depth),len(depth)])
matB = zeros([len(depth),len(depth)])
matC = empty([len(depth),len(depth)])
concentration_t0 = empty(len(depth))
concentration_t0[1:] = Cdefault
concentration_t0[0] = Cowl0
concentration = sph_2Dfunct(sizex=len(depth),
sizet=len(time),
firstline=concentration_t0)
コメント火曜日〜12時7月2日:30
import numpy as np
from scipy.interpolate import interp1d
import pymc3 as pm3
import theano.tensor as tt
import theano.compile
import config
@theano.compile.ops.as_op(itypes=[tt.dscalar,tt.dscalar,tt.dscalar],
otypes=[tt.dvector])
def simul_DifferentialEq(alpha0,alpha1,epsilon):
observed_depth = np.array([0.5,1.5,3,5,7,9,13,17])# in cm
observed_values = np.array([6.25,2.75,1.25,1.25,1.5,1.75,1.5,1])# mmol.l-1
#useful ?
observed_values = observed_values[np.argsort(-observed_depth)]
observed_depth = -np.sort(-observed_depth)
depth = config.depth
matA = config.matA
matB = config.matB
matC = config.matC
concentration = config.concentration
alpha = alpha0 * np.exp(-alpha1*depth) # in day^-1
# simplification for a Constant phi
phi = np.empty(len(depth))
phi[:]=0.6
#########################
beta = config.beta * phi * epsilon # dimensionless
delta = beta * phi/config.Deltax
eta = alpha * config.Deltat
f1 = f2 = delta
f3 = 1 - 2*delta + eta/2
matB[0,0] = f1[0] - eta[0]/2 +1
matB[0,1] = matA[0,1] = -2*f1[0]
matB[0,2] = matA[0,2] = delta[0]
matB[-1,-1] = f2[-1] + eta[-1]/2 +1
matB[-1,-2] = matA[-1,-2] = -2*f2[-1]
matB[-1,-3] = matA[-1,-3] = delta[-1]
matB[range(1,concentration.sizex-1),
range(1,concentration.sizex-1)] = \
f3[1:concentration.sizex-1]
matA[range(1,concentration.sizex-1),
range(concentration.sizex-2)] = \
matB[range(1,concentration.sizex-1),
range(concentration.sizex-2)] = \
f1[1:concentration.sizex-1]
matA[range(1,concentration.sizex-1),
range(2,concentration.sizex)] = \
matB[range(1,concentration.sizex-1),
range(2,concentration.sizex)] = \
f2[1:concentration.sizex-1]
matB[range(1,concentration.sizex),0] = -eta[1:]
matA[range(concentration.sizex),
range(concentration.sizex)] = \
matB[range(concentration.sizex),
range(concentration.sizex)] -2
matA[0,0] += eta[0]
matC = np.dot(np.linalg.matrix_power(-matA,-1),matB)
for tcount in range(concentration.sizet-1):
#the variable 'temp' has no interest (just convenient for debugging)
temp = np.dot(matC,concentration.values[:,tcount])
# condition limit
temp[0] = config.C0
# a priori useless (but convenient for debugging))
temp[np.where(temp>config.C0)] = config.C0
# everything for that...
concentration.values[:,tcount+1] = temp
interpolated_concentration = interp1d(depth,concentration.values[:,-1])
return interpolated_concentration(observed_depth)
# the pymc3 stuff is below
model = pm3.Model()
with model:
alpha0 = pm3.Uniform("alpha0",-2,0)
alpha1 = pm3.Uniform("alpha1",-1,2)
epsilon = pm3.Uniform("epsilon",0.1,15)
DifferentialEq = simul_DifferentialEq(alpha0,alpha1,epsilon)
# it is awkward to repeat observed values
#some previous tries made me think it could solve the problem but it didn't
observed_depth = np.array([0.5,1.5,3,5,7,9,13,17])# in cm
observed_values = np.array([6.25,2.75,1.25,1.25,1.5,1.75,1.5,1])# mmol.l-1
# useful ?
observed_values = observed_values[np.argsort(-observed_depth)]
observed_depth = -np.sort(-observed_depth)
obs = pm3.Normal('obs', mu=DifferentialEq, sd=0.1, observed=observed_values)
print('running test170127, find_MAP...')
testfindmap = pm3.find_MAP()
はconfig.pyのコンテンツがあり、ご清聴ありがとうございました。
私は最後の行(すなわちfind_MAPのものを。)に置き換え:私は追加することになり
Auto-assigning NUTS sampler...
INFO:pymc3:Auto-assigning NUTS sampler...
Initializing NUTS using advi...
INFO:pymc3:Initializing NUTS using advi...
Traceback (most recent call last):
File "<ipython-input-1-8395e07601b2>", line 1, in <module>
runfile('/Users/steph/work/profiles/profiles-pymc/test170127.py', wdir='/Users/steph/work/profiles/profiles-pymc')
File "/Users/steph/anaconda/lib/python3.5/site-packages/spyder/utils/site/sitecustomize.py", line 866, in runfile
execfile(filename, namespace)
File "/Users/steph/anaconda/lib/python3.5/site-packages/spyder/utils/site/sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "/Users/steph/work/profiles/profiles-pymc/test170127.py", line 84, in <module>
pm3.sample(500)
File "/Users/steph/anaconda/lib/python3.5/site-packages/pymc3/sampling.py", line 149, in sample
start_, step = init_nuts(init=init, n_init=n_init, model=model)
File "/Users/steph/anaconda/lib/python3.5/site-packages/pymc3/sampling.py", line 434, in init_nuts
v_params = pm.variational.advi(n=n_init)
File "/Users/steph/anaconda/lib/python3.5/site-packages/pymc3/variational/advi.py", line 139, in advi
updates = optimizer(loss=-1 * elbo, param=[uw_shared])
File "/Users/steph/anaconda/lib/python3.5/site-packages/pymc3/variational/advi.py", line 259, in optimizer
grad = tt.grad(loss, param_)
File "/Users/steph/anaconda/lib/python3.5/site-packages/theano/gradient.py", line 561, in grad
grad_dict, wrt, cost_name)
File "/Users/steph/anaconda/lib/python3.5/site-packages/theano/gradient.py", line 1324, in _populate_grad_dict
rval = [access_grad_cache(elem) for elem in wrt]
File "/Users/steph/anaconda/lib/python3.5/site-packages/theano/gradient.py", line 1324, in <listcomp>
rval = [access_grad_cache(elem) for elem in wrt]
File "/Users/steph/anaconda/lib/python3.5/site-packages/theano/gradient.py", line 1279, in access_grad_cache
term = access_term_cache(node)[idx]
File "/Users/steph/anaconda/lib/python3.5/site-packages/theano/gradient.py", line 973, in access_term_cache
output_grads = [access_grad_cache(var) for var in node.outputs]
File "/Users/steph/anaconda/lib/python3.5/site-packages/theano/gradient.py", line 973, in <listcomp>
output_grads = [access_grad_cache(var) for var in node.outputs]
File "/Users/steph/anaconda/lib/python3.5/site-packages/theano/gradient.py", line 1279, in access_grad_cache
term = access_term_cache(node)[idx]
File "/Users/steph/anaconda/lib/python3.5/site-packages/theano/gradient.py", line 973, in access_term_cache
output_grads = [access_grad_cache(var) for var in node.outputs]
File "/Users/steph/anaconda/lib/python3.5/site-packages/theano/gradient.py", line 973, in <listcomp>
output_grads = [access_grad_cache(var) for var in node.outputs]
File "/Users/steph/anaconda/lib/python3.5/site-packages/theano/gradient.py", line 1279, in access_grad_cache
term = access_term_cache(node)[idx]
File "/Users/steph/anaconda/lib/python3.5/site-packages/theano/gradient.py", line 973, in access_term_cache
output_grads = [access_grad_cache(var) for var in node.outputs]
File "/Users/steph/anaconda/lib/python3.5/site-packages/theano/gradient.py", line 973, in <listcomp>
output_grads = [access_grad_cache(var) for var in node.outputs]
File "/Users/steph/anaconda/lib/python3.5/site-packages/theano/gradient.py", line 1279, in access_grad_cache
term = access_term_cache(node)[idx]
File "/Users/steph/anaconda/lib/python3.5/site-packages/theano/gradient.py", line 973, in access_term_cache
output_grads = [access_grad_cache(var) for var in node.outputs]
File "/Users/steph/anaconda/lib/python3.5/site-packages/theano/gradient.py", line 973, in <listcomp>
output_grads = [access_grad_cache(var) for var in node.outputs]
File "/Users/steph/anaconda/lib/python3.5/site-packages/theano/gradient.py", line 1279, in access_grad_cache
term = access_term_cache(node)[idx]
File "/Users/steph/anaconda/lib/python3.5/site-packages/theano/gradient.py", line 973, in access_term_cache
output_grads = [access_grad_cache(var) for var in node.outputs]
File "/Users/steph/anaconda/lib/python3.5/site-packages/theano/gradient.py", line 973, in <listcomp>
output_grads = [access_grad_cache(var) for var in node.outputs]
File "/Users/steph/anaconda/lib/python3.5/site-packages/theano/gradient.py", line 1279, in access_grad_cache
term = access_term_cache(node)[idx]
File "/Users/steph/anaconda/lib/python3.5/site-packages/theano/gradient.py", line 973, in access_term_cache
output_grads = [access_grad_cache(var) for var in node.outputs]
File "/Users/steph/anaconda/lib/python3.5/site-packages/theano/gradient.py", line 973, in <listcomp>
output_grads = [access_grad_cache(var) for var in node.outputs]
File "/Users/steph/anaconda/lib/python3.5/site-packages/theano/gradient.py", line 1279, in access_grad_cache
term = access_term_cache(node)[idx]
File "/Users/steph/anaconda/lib/python3.5/site-packages/theano/gradient.py", line 973, in access_term_cache
output_grads = [access_grad_cache(var) for var in node.outputs]
File "/Users/steph/anaconda/lib/python3.5/site-packages/theano/gradient.py", line 973, in <listcomp>
output_grads = [access_grad_cache(var) for var in node.outputs]
File "/Users/steph/anaconda/lib/python3.5/site-packages/theano/gradient.py", line 1279, in access_grad_cache
term = access_term_cache(node)[idx]
File "/Users/steph/anaconda/lib/python3.5/site-packages/theano/gradient.py", line 973, in access_term_cache
output_grads = [access_grad_cache(var) for var in node.outputs]
File "/Users/steph/anaconda/lib/python3.5/site-packages/theano/gradient.py", line 973, in <listcomp>
output_grads = [access_grad_cache(var) for var in node.outputs]
File "/Users/steph/anaconda/lib/python3.5/site-packages/theano/gradient.py", line 1279, in access_grad_cache
term = access_term_cache(node)[idx]
File "/Users/steph/anaconda/lib/python3.5/site-packages/theano/gradient.py", line 973, in access_term_cache
output_grads = [access_grad_cache(var) for var in node.outputs]
File "/Users/steph/anaconda/lib/python3.5/site-packages/theano/gradient.py", line 973, in <listcomp>
output_grads = [access_grad_cache(var) for var in node.outputs]
File "/Users/steph/anaconda/lib/python3.5/site-packages/theano/gradient.py", line 1279, in access_grad_cache
term = access_term_cache(node)[idx]
File "/Users/steph/anaconda/lib/python3.5/site-packages/theano/gradient.py", line 973, in access_term_cache
output_grads = [access_grad_cache(var) for var in node.outputs]
File "/Users/steph/anaconda/lib/python3.5/site-packages/theano/gradient.py", line 973, in <listcomp>
output_grads = [access_grad_cache(var) for var in node.outputs]
File "/Users/steph/anaconda/lib/python3.5/site-packages/theano/gradient.py", line 1279, in access_grad_cache
term = access_term_cache(node)[idx]
File "/Users/steph/anaconda/lib/python3.5/site-packages/theano/gradient.py", line 973, in access_term_cache
output_grads = [access_grad_cache(var) for var in node.outputs]
File "/Users/steph/anaconda/lib/python3.5/site-packages/theano/gradient.py", line 973, in <listcomp>
output_grads = [access_grad_cache(var) for var in node.outputs]
File "/Users/steph/anaconda/lib/python3.5/site-packages/theano/gradient.py", line 1279, in access_grad_cache
term = access_term_cache(node)[idx]
File "/Users/steph/anaconda/lib/python3.5/site-packages/theano/gradient.py", line 973, in access_term_cache
output_grads = [access_grad_cache(var) for var in node.outputs]
File "/Users/steph/anaconda/lib/python3.5/site-packages/theano/gradient.py", line 973, in <listcomp>
output_grads = [access_grad_cache(var) for var in node.outputs]
File "/Users/steph/anaconda/lib/python3.5/site-packages/theano/gradient.py", line 1279, in access_grad_cache
term = access_term_cache(node)[idx]
File "/Users/steph/anaconda/lib/python3.5/site-packages/theano/gradient.py", line 1113, in access_term_cache
input_grads = node.op.grad(inputs, new_output_grads)
AttributeError: 'FromFunctionOp' object has no attribute 'grad'
::私は続けるならば、私はメインのコードを実行したときに、私が取得
pm3.sample(500)
find_MAPを呼び出すと、コードはエラーなく実行されますが、結果の値はばかげて見えますが、この二重警告が表示されます。
Warning: gradient not available.(E.g. vars contains discrete variables). MAP estimates may not be accurate for the default parameters. Defaulting to non-gradient minimization fmin_powell.WARNING:pymc3:Warning: gradient not available.(E.g. vars contains discrete variables). MAP estimates may not be accurate for the default parameters. Defaulting to non-gradient minimization fmin_powell.
Optimization terminated successfully.
Current function value: 36.569283
Iterations: 10
Function evaluations: 415
"config"のものを実際の値に置き換えることはできますか? '' 'find_MAP'''はscipy minimizerのラッパーです。(NUTSやMetropolisを使って)サンプルすればエラーが出ますか? – aloctavodia
1. NUTSとサンプルの両方が私に 'AttributeError:' FromFunctionOp 'オブジェクトには、一連のメッセージの後に' grad '属性がありません。 –
再現可能な例を投稿できますか? – aloctavodia