2015-07-28 74 views
0
范畴

我试图在以下情况下SOFTMAX选择功能的一个单一的参数来执行参数估计:SOFTMAX选择概率与PyMC3

在每次试验中,三个选项值被给予(例如,[1 ,2,3]),并且主题在选项(0,1或2)之间进行选择。 softmax函数根据温度参数(这里限定在0和10之间)将选项值转换为选择概率(3个概率的向量,总计为1)。

在每个试验中的选择应该被建模为具有从softmax计算的试验选择概率的分类分布。请注意,分类的选择概率取决于选项值,因此在每个试验中都有所不同。

这就是我想出了:

# Generate data 
nTrials = 60 # number of trials (value triplets and choices) 
np.random.seed(42) 
# generate nTrials triplets of values 
values = np.random.choice([1,2,3,4,5], size=(nTrials, 3)) 

choices = values.argmax(axis=1) # choose highest value option 
# add some random variation, so that *not* always the highest value option is chosen 
errors = np.random.rand(nTrials)>0.8 # determine trials with non-optimal choice 
# randomly determine new choices for these trials 
choices[errors] = np.random.choice([0,1,2], size=sum(errors==True)) 

# Model specification & estimation 
import pymc3 as pm 
from theano import tensor as t 
with pm.Model(): 

    # prior over theta 
    theta = pm.Uniform('theta', lower=0, upper=10) 

    # softmax implementation 
    enumerator = pm.exp(theta*values) 
    denominator = t.reshape(pm.sum(pm.exp(theta*values), axis=1), (nTrials, 1)) 
    ps = enumerator/denominator 

    # Likelihood (sampling model for the data) 
    for trial in range(nTrials): 
     yobs = pm.Categorical('yobs{}'.format(trial), p=ps[trial], observed=choices[trial]) 

    # draw 500 samples from posterior 
    trace = pm.sample(500, pm.Metropolis()) 

与极长的警告/错误信息nTrials不是像50大该代码失败:

警告:

INFO (theano.gof.compilelock): Refreshing lock /Users/felixmolter/.theano/compiledir_Darwin-14.4.0-x86_64-i386-64bit-i386-2.7.8-64/lock_dir/lock 
INFO:theano.gof.compilelock:Refreshing lock /Users/felixmolter/.theano/compiledir_Darwin-14.4.0-x86_64-i386-64bit-i386-2.7.8-64/lock_dir/lock 
00001 #include <Python.h> 
00002 #include <iostream> 
00003 #include <math.h> 
00004 #include <numpy/arrayobject.h> 
00005 #include <numpy/arrayscalars.h> 
00006 #include <vector> 
00007 #include <algorithm> 
00008 ////////////////////// 
00009 //// Support Code 
00010 ////////////////////// 
00011 
00012 
00013  namespace { 
00014  struct __struct_compiled_op_65734e56ae54d89bdcf84e36893358e6 { 
00015   PyObject* __ERROR; 
00016 
00017   PyObject* storage_V3; 
00018 PyObject* storage_V5; 
00019 PyObject* storage_V7; 
00020 PyObject* storage_V9; 
00021 PyObject* storage_V11; 
00022 PyObject* storage_V13; 
[...] 

错误:

Exception: ('The following error happened while compiling the node', Elemwise{Composite{((Switch(LE(Abs((i0 + i1)), i2), log(i3), i4) + Switch(LE(Abs((i0 + i5)), i2), log(i6), i4) + Switch(LE(Abs((i0 + i7)), i2), log(i8), i4) + Switch(LE(Abs((i0 + i9)), i2), log(i10), i4) + Switch(LE(Abs((i0 + i11)), [...] 

我对PyMC(和Theano)颇为陌生,我觉得我的实现真的很笨重,并不理想。任何帮助和建议,强烈感谢!

菲利克斯

编辑:我上传的代码作为笔记本电脑,显示出充分的警告和错误消息:http://nbviewer.ipython.org/github/moltaire/softmaxPyMC/blob/master/softmax_stackoverflow.ipynb

+1

为了能够提供帮助,我需要完整的错误信息。 – nouiz

+0

感谢您的回复。我上传了一个带有完整错误信息的笔记本给github:https://github.com/moltaire/softmaxPyMC/blob/master/softmax_stackoverflow.ipynb – Felix

回答

0

我再次发现这种情况。就像跟进一样,现在不能够重现它。所以我认为它已经修复了。我们修复了在某些情况下可能导致此错误的相关问题。

它使用g ++ 4.5.1。如果您遇到此问题,请将Theano更新为开发版本。如果不能解决问题,请尝试使用更新的g ++,这可能与较旧的g ++版本有关。