内核重启运行Ipython + PyMC

时间:2014-10-10 18:56:20

标签: ipython ipython-notebook pymc

当我运行以下代码时,我在Ipython Notebook中遇到错误The kernel appears to have died. It will restart automatically.

from sklearn.datasets import load_boston
import numpy as np
import pymc as pm
import pandas as pd

boston = load_boston()
features = ['INDUS', 'NOX', 'RM', 'TAX', 'PTRATIO', 'LSTAT']
df = pd.DataFrame(boston.data, columns=boston.feature_names)
X = np.array(df.ix[:, features])
y = boston.target

gamma = pm.Binomial('gamma', 1, 0.5, size=len(features))
var = pm.Lambda('var', lambda gamma=gamma: (1-gamma)*0.001 + gamma*10)
prec = pm.Lambda('prec', lambda var=var: 1.0/var)
b = pm.Normal('b', 0, prec)
int_ = pm.Normal('int_', 0, 0.01)
taue = pm.Gamma('taue', 0.1, 0.1)
mu = int_ + X[:,0]*b[0] + X[:,1]*b[1] + X[:,2]*b[2] + X[:,3]*b[3] + X[:,4]*b[4] + X[:,5]*b[5]
observed = pm.Normal('obs', mu, taue, observed=True, value=y)
M = pm.MCMC([observed, mu, int_, b, prec, var, gamma])
M.sample(10000, 500, 5)

pm.Matplot.plot(M)

如果它是相关的,我试图从这个page(WinBUGS代码,第14页)重现贝叶斯变量选择的一个例子。运行M.sample()时,内核有时会失败,但运行pm.Matplot.plot(M)

时大多数时候会出现错误

我也尝试使用ipython qtconsole但结果是一样的。在ipython中,它会导致细分错误。我正在使用带有ipython 2.3.0,matplotlib 1.4.0,pandas 0.14.1,scikit-learn 0.15.2,pymc 2.3.4和python 2.7.8的conda环境。我使用ipython 3.0.0创建了一个新环境,但这个问题仍然存在。

有人可以重现这个问题吗?

更新

我在EC2实例中使用新的Anaconda环境尝试了这个示例,这是我能找到的唯一给出错误的例子。我需要添加的唯一代码是:

import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt

因此,我基本上是在ipython中运行它:

import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
%run model

其中model是一个包含我最初发布的完全相同代码的文件。错误有点改变。例如:

IndexError                                Traceback (most recent call last)
/home/ubuntu/model.py in <module>()
     16 int_ = pm.Normal('int_', 0, 0.01)
     17 taue = pm.Gamma('taue', 0.1, 0.1)
---> 18 mu = int_ + X[:,0]*b[0] + X[:,1]*b[1] + X[:,2]*b[2] + X[:,3]*b[3] + X[:,4]*b[4] + X[:,5]*b[5]
     19 observed = pm.Normal('obs', mu, taue, observed=True, value=y)
     20 M = pm.MCMC([observed, mu, int_, b, prec, var, gamma])

IndexError: index 0 is out of bounds for axis 1 with size -4611686018427387904

*** Error in `/home/ubuntu/anaconda/envs/env3/bin/python': double free or corruption (out): 0x00000000028a0b00 ***
Aborted (core dumped)

另一个:

In [5]: %run model
 [-----------------100%-----------------] 10000 of 10000 complete in 17.7 sec/home/ubuntu/anaconda/envs/env3/lib/python2.7/site-packages/numpy/core/fromnumeric.py:2499: VisibleDeprecationWarning: `rank` is deprecated; use the `ndim` attribute or function instead. To find the rank of a matrix see `numpy.linalg.matrix_rank`.
  VisibleDeprecationWarning)
Plotting int_
Plotting prec_0
Plotting prec_1
Plotting prec_2
Plotting prec_3
Plotting prec_4
Plotting prec_5
Plotting var_0
Plotting var_1
Plotting var_2
Plotting var_3
Plotting var_4
Plotting var_5
Plotting gamma_0
Plotting gamma_1
Plotting gamma_2
Plotting gamma_3
Plotting gamma_4
Plotting gamma_5
Plotting b_0
Plotting b_1
Plotting b_2
Plotting b_3
Plotting b_4
Plotting b_5
*** Error in `/home/ubuntu/anaconda/envs/env3/bin/python': double free or corruption (out): 0x00000000023dc940 ***
Aborted (core dumped)

再次,类似于上面的错误:

In [6]: %run model
 [-----------------100%-----------------] 10000 of 10000 complete in 18.4 secPlotting var_0
*** Error in `/home/ubuntu/anaconda/envs/env3/bin/python': double free or corruption (out): 0x00000000035f0f10 ***
Aborted (core dumped)

这个发生在早些时候:

In [13]: gamma = pm.Binomial('gamma', 1, 0.5, size=len(features))
Segmentation fault (core dumped)

最后:

In [22]: M.sample(10000, 500, 5)
Segmentation fault (core dumped)

有时,代码运行正常并生成多个图。为了进行比较,我还运行了this examplethis one而没有问题。

This是我在conda环境中安装的软件包列表。

1 个答案:

答案 0 :(得分:0)

这里运行得很好,使用Python 2.7.6和IPython 3.0.0-dev。你能尝试升级/改变你的Matplotlib吗?