Как определить модель в PyMC3 с одним параметром, ограниченным одним и тем же значением для нескольких условий

Я хочу написать модель, как показано ниже. Основная идея в том, что у меня несколько состояний (или обработок) все параметры оцениваются для каждого состояния независимо, кроме параметра каппа, который одинаков для всех состояний.

with pm.Model() as model:
    trace_per_condition = []
    # define the kappa hyperparameter
    kappa = pm.Gamma('kappa', 1, 0.1)
    for condition in range(0, ncond):
        z_cond = z[condition]
        # define the mu hyperparameter
        mu = pm.Beta('mu', 1, 1)
        # define the prior
        theta = pm.Beta('theta', mu * kappa, (1 - mu) * kappa, shape=len(z_cond))
        # define the likelihood
        y = pm.Binomial('y', p=theta, n=trials, observed=z_cond)
    # Generate a MCMC chain
        start = pm.find_MAP()
        step1 = pm.Metropolis([theta, mu])
        step2 = pm.NUTS([kappa])
        trace = pm.sample(1000, [step1, step2], progressbar=False)
        trace_per_condition.append(trace)

Когда я запускаю модель, я получаю следующее сообщение.

/usr/local/lib/python2.7/dist-packages/Theano-0.6.0-py2.7.egg/theano/gradient.py:513:  UserWarning: grad method was asked to compute the gradient with respect to a variable that is not part of the computational graph of the cost, or is used only by a non-differentiable operator: mu handle_disconnected(elem)
/usr/local/lib/python2.7/dist-packages/Theano-0.6.0-py2.7.egg/theano/gradient.py:533: UserWarning: grad method was asked to compute the gradient with respect to a variable that is not part of the computational graph of the cost, or is used only by a non-differentiable operator: <DisconnectedType>
  handle_disconnected(rval[i])
/usr/local/lib/python2.7/dist-packages/Theano-0.6.0-py2.7.egg/theano/gradient.py:513: UserWarning: grad method was asked to compute the gradient with respect to a variable that is not part of the computational graph of the cost, or is used only by a non-differentiable operator: theta
  handle_disconnected(elem)
Traceback (most recent call last):
  File "<stdin>", line 46, in <module>
  File "/usr/local/lib/python2.7/dist-packages/pymc-3.0-py2.7.egg/pymc/tuning/starting.py", line 80, in find_MAP
    start), fprime=grad_logp_o, disp=disp, *args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/scipy/optimize/optimize.py", line 777, in fmin_bfgs
    res = _minimize_bfgs(f, x0, args, fprime, callback=callback, **opts)
  File "/usr/lib/python2.7/dist-packages/scipy/optimize/optimize.py", line 832, in _minimize_bfgs
    gfk = myfprime(x0)
  File "/usr/lib/python2.7/dist-packages/scipy/optimize/optimize.py", line 281, in function_wrapper
    return function(*(wrapper_args + args))
  File "/usr/local/lib/python2.7/dist-packages/pymc-3.0-py2.7.egg/pymc/tuning/starting.py", line 75, in grad_logp_o
    return nan_to_num(-dlogp(point))
  File "/usr/local/lib/python2.7/dist-packages/pymc-3.0-py2.7.egg/pymc/blocking.py", line 119, in __call__
    return self.fa(self.fb(x))
  File "/usr/local/lib/python2.7/dist-packages/pymc-3.0-py2.7.egg/pymc/model.py", line 284, in __call__
    return self.f(**state)
  File "/usr/local/lib/python2.7/dist-packages/Theano-0.6.0-py2.7.egg/theano/compile/function_module.py", line 516, in __call__
    self[k] = arg
  File "/usr/local/lib/python2.7/dist-packages/Theano-0.6.0-py2.7.egg/theano/compile/function_module.py", line 452, in __setitem__
    self.value[item] = value
  File "/usr/local/lib/python2.7/dist-packages/Theano-0.6.0-py2.7.egg/theano/compile/function_module.py", line 413, in __setitem__
    "of the inputs of your function for duplicates." % str(item)) 
TypeError: Ambiguous name: mu - please check the names of the inputs of your function for duplicates.

Изменить. После ответа Криса Фоннесбека я попробовал следующее:

with pm.Model() as model:
    trace_per_condition = []
    # define the kappa hyperparameter
    kappa = pm.Gamma('kappa', 1, 0.1)
    for condition in range(0, ncond):
        z_cond = z[condition]
        # define the mu hyperparameter
        mu = pm.Beta('mu_%i' % condition, 1, 1)
        # define the prior
        theta = pm.Beta('theta_%i' % condition, mu * kappa, (1 - mu) * kappa, shape=len(z_cond))
        # define the likelihood
        y = pm.Binomial('y_%i' % condition, p=theta, n=trials, observed=z_cond)
    # Generate a MCMC chain
        start = pm.find_MAP()
        step1 = pm.Metropolis([theta, mu])
        step2 = pm.NUTS([kappa])
        trace = pm.sample(10000, [step1, step2], start=start, progressbar=False)
        trace_per_condition.append(trace)

Я получаю сообщение об ошибке:

/usr/local/lib/python2.7/dist-packages/Theano-0.6.0-py2.7.egg/theano/gradient.py:513:
UserWarning: grad method was asked to compute the gradient with respect to a variable  
that  is not part of the computational graph of the cost, or is used only by a  
non-differentiable operator: mu_1
handle_disconnected(elem)

/usr/local/lib/python2.7/dist-packages/Theano-0.6.0-py2.7.egg/theano/gradient.py:533: 
UserWarning: grad method was asked to compute the gradient with respect to a variable 
that is not part of the computational graph of the cost, or is used only by a 
non-differentiable operator: <DisconnectedType>
handle_disconnected(rval[i])

/usr/local/lib/python2.7/dist-packages/Theano-0.6.0-py2.7.egg/theano/gradient.py:513: 
UserWarning: grad method was asked to compute the gradient with respect to a variable 
that is not part of the computational graph of the cost, or is used only by a 
non-differentiable operator: theta_1
handle_disconnected(elem)


Traceback (most recent call last):
  File "<stdin>", line 43, in <module>
  File "/usr/local/lib/python2.7/dist-packages/pymc-3.0-py2.7.egg/pymc/tuning/starting.py", line 80, in find_MAP
    start), fprime=grad_logp_o, disp=disp, *args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/scipy/optimize/optimize.py", line 777, in fmin_bfgs
    res = _minimize_bfgs(f, x0, args, fprime, callback=callback, **opts)
  File "/usr/lib/python2.7/dist-packages/scipy/optimize/optimize.py", line 837, in _minimize_bfgs
    old_fval = f(x0)
  File "/usr/lib/python2.7/dist-packages/scipy/optimize/optimize.py", line 281, in function_wrapper
    return function(*(wrapper_args + args))
  File "/usr/local/lib/python2.7/dist-packages/pymc-3.0-py2.7.egg/pymc/tuning/starting.py", line 72, in logp_o
    return nan_to_high(-logp(point))
  File "/usr/local/lib/python2.7/dist-packages/pymc-3.0-py2.7.egg/pymc/blocking.py", line 119, in __call__
    return self.fa(self.fb(x))
  File "/usr/local/lib/python2.7/dist-packages/pymc-3.0-py2.7.egg/pymc/model.py", line 283, in __call__
    return self.f(**state)
  File "/usr/local/lib/python2.7/dist-packages/Theano-0.6.0-py2.7.egg/theano/compile/function_module.py", line 482, in __call__
    raise TypeError("Too many parameter passed to theano function")
TypeError: Too many parameter passed to theano function

UserWarning связаны с оптимизацией начальной точки и удаляются, если я не использую pm.find_MAP(). В остальном ошибка сохраняется.


person aloctavodia    schedule 24.07.2014    source источник


Ответы (2)


Одна вещь, которую я заметил, это то, что вы делаете выборку каждый раз, когда добавляете условие, я думаю, вы, вероятно, хотите вытащить это из цикла.

Кроме того, вам не нужно определять отдельные переменные для каждого мю, тета, у для каждого условия. Например, если ваши данные находятся в столбцах data, вы сможете сделать что-то вроде

with pm.Model() as model:

    kappa = pm.Gamma('kappa', 1, 0.1)

    mu = pm.Beta('mu', 1, 1, shape=ncond)

    mu_c = mu[data.condition]
    theta = pm.Beta('theta', mu_c * kappa, (1 - mu_c) * kappa, shape=len(data))

    y = pm.Binomial('y', p=theta, n=data.trials, observed=data.z_cond)
person John Salvatier    schedule 31.07.2014

Если вы определяете объекты PyMC в цикле, вы должны давать им разные имена на каждой итерации. Например, вы можете определить:

mu = pm.Beta('mu_%i' % condition, 1, 1)

Это должно устранить ошибку, которую вы получаете.

person Chris Fonnesbeck    schedule 25.07.2014
comment
Спасибо, это имеет большой смысл, но теперь я получаю другую ошибку. Я редактирую вопрос, чтобы добавить новую ошибку. - person aloctavodia; 29.07.2014