错误:非常量表达式不能从类型'npy_intp'缩小为'int' - python

我正在尝试运行以下模型,但是在编译期间失败:

import numpy as np
import pymc3 as pm


def sample_data(G=1, K=2):
    # mean proportion ([0,1]) for each g
    p_g = np.random.beta(2, 2, size=G)

    # concentration around each p_g
    c_g = np.random.lognormal(mean=0.5, sigma=1, size=G)

    # reparameterization for standard Beta(a,b)
    a_g = c_g * p_g / np.sqrt(p_g**2 + (1.-p_g)**2)
    b_g = c_g*(1.-p_g) / np.sqrt(p_g**2 + (1.-p_g)**2)

    # for each p_g, sample K proportions
    p_gk = np.random.beta(a_g[:, np.newaxis], b_g[:, np.newaxis], size=(G, K))

    return p_gk

# Data size
G = 3
K = 5

# obtain a G x K array of proportions p_gk in [0,1]
data = sample_data(G, K) 

with pm.Model() as m:

    # Parameters
    p_g = pm.Beta('p_g', 1., 1., shape=G)
    sd_g = pm.HalfNormal('sd_g', sd=1., shape=G)

    # Observed proportions
    p_gk = pm.Beta('p_gk', mu=p_g, sd=sd_g, shape=(G, K), observed=data)

    trace = pm.sample(2000)

这些错误:

Exception: ("Compilation failed (return status=1):

/Users/mfansler/.theano/compiledir_Darwin-17.6.0-x86_64-i386-64bit-i386-3.6.3-64/tmpr58gulp2/mod.cpp:400:27: 
  error: non-constant-expression cannot be narrowed from type 'npy_intp' (aka 'long') to 'int' in initializer list [-Wc++11-narrowing].
     int init_totals[2] = {V3_n0, V3_n1};.
                           ^~~~~.

/Users/mfansler/.theano/compiledir_Darwin-17.6.0-x86_64-i386-64bit-i386-3.6.3-64/tmpr58gulp2/mod.cpp:400:27:
  note: insert an explicit cast to silence this issue.
     int init_totals[2] = {V3_n0, V3_n1};.
                           ^~~~~.
                           static_cast<int>( ).

/Users/mfansler/.theano/compiledir_Darwin-17.6.0-x86_64-i386-64bit-i386-3.6.3-64/tmpr58gulp2/mod.cpp:400:34: 
  error: non-constant-expression cannot be narrowed from type 'npy_intp' (aka 'long') to 'int' in initializer list [-Wc++11-narrowing].
     int init_totals[2] = {V3_n0, V3_n1};.
                                  ^~~~~.

/Users/mfansler/.theano/compiledir_Darwin-17.6.0-x86_64-i386-64bit-i386-3.6.3-64/tmpr58gulp2/mod.cpp:400:34: 
  note: insert an explicit cast to silence this issue.
     int init_totals[2] = {V3_n0, V3_n1};.
                                  ^~~~~.
                                  static_cast<int>( ).

/Users/mfansler/.theano/compiledir_Darwin-17.6.0-x86_64-i386-64bit-i386-3.6.3-64/tmpr58gulp2/mod.cpp:412:9: 
  error: non-constant-expression cannot be narrowed from type 'ssize_t' (aka 'long') to 'int' in initializer list [-Wc++11-narrowing].
         V3_stride0, V3_stride1, .
         ^~~~~~~~~~.

/Users/mfansler/.theano/compiledir_Darwin-17.6.0-x86_64-i386-64bit-i386-3.6.3-64/tmpr58gulp2/mod.cpp:412:9: 
  note: insert an explicit cast to silence this issue.
         V3_stride0, V3_stride1, .
         ^~~~~~~~~~.
         static_cast<int>( ).

/Users/mfansler/.theano/compiledir_Darwin-17.6.0-x86_64-i386-64bit-i386-3.6.3-64/tmpr58gulp2/mod.cpp:412:21: 
  error: non-constant-expression cannot be narrowed from type 'ssize_t' (aka 'long') to 'int' in initializer list [-Wc++11-narrowing].
         V3_stride0, V3_stride1, .
                     ^~~~~~~~~~.

/Users/mfansler/.theano/compiledir_Darwin-17.6.0-x86_64-i386-64bit-i386-3.6.3-64/tmpr58gulp2/mod.cpp:412:21:
  note: insert an explicit cast to silence this issue.
         V3_stride0, V3_stride1, .
                     ^~~~~~~~~~.
                     static_cast<int>( ).

/Users/mfansler/.theano/compiledir_Darwin-17.6.0-x86_64-i386-64bit-i386-3.6.3-64/tmpr58gulp2/mod.cpp:413:1: 
  error: non-constant-expression cannot be narrowed from type 'ssize_t' (aka 'long') to 'int' in initializer list [-Wc++11-narrowing].
 V1_stride0, V1_stride1.
 ^~~~~~~~~~.

/Users/mfansler/.theano/compiledir_Darwin-17.6.0-x86_64-i386-64bit-i386-3.6.3-64/tmpr58gulp2/mod.cpp:413:1: 
  note: insert an explicit cast to silence this issue.
 V1_stride0, V1_stride1.
 ^~~~~~~~~~.
 static_cast<int>( ).

/Users/mfansler/.theano/compiledir_Darwin-17.6.0-x86_64-i386-64bit-i386-3.6.3-64/tmpr58gulp2/mod.cpp:413:13:
  error: non-constant-expression cannot be narrowed from type 'ssize_t' (aka 'long') to 'int' in initializer list [-Wc++11-narrowing].
 V1_stride0, V1_stride1.
             ^~~~~~~~~~.

/Users/mfansler/.theano/compiledir_Darwin-17.6.0-x86_64-i386-64bit-i386-3.6.3-64/tmpr58gulp2/mod.cpp:413:13:
  note: insert an explicit cast to silence this issue.
 V1_stride0, V1_stride1.
             ^~~~~~~~~~.
             static_cast<int>( ).

6 errors generated.. ", '[Elemwise{log,no_inplace}(TensorConstant{[[0.297343..76841722]]})]')

我是PyMC3的新手。运行现有的PyMC3示例时,我看不到这些错误。我怀疑看到这些是因为我使用的是多维格式(即(G,K)),因为我还没有看到其他人使用此格式(我可能是对Stan表示熟悉)。

通常,我很难理解如何实现具有多个维度的多级模型。

知道是什么原因导致了我所看到的错误吗?

版本号

python 3.6.3
numpy的1.14.5
Theano 1.0.2
pymc3 3.4.1
Mac OS 10.13.5

更新资料

我在HPC节点(CentOS 7)上安装了相同的软件包版本(通过conda),并且能够运行the modified version of the model suggested by @colcarroll。但是,在OS X机器上,即使更改了模型,我仍然看到上面指出的Theano编译错误。这可能是clang问题吗?可以指定Theano使用的编译器吗?

python大神给出的解决方案

一种解决方法是抑制编译错误:

import theano

theano.config.gcc.cxxflags = "-Wno-c++11-narrowing"

这些错误对程序正确性的影响程度尚不清楚。当我在CentOS 7上编译时,它们不会出现(即使使用-Wc++11-narrowing明确检查它们)。在Mac OS X上具有抑制错误的采样结果与在没有CentOS的情况下具有可比性。

我还是希望看到一个可以解释根本问题的答案。

Python:同时在for循环中添加到列表列表 - python

我想用for循环外的0索引值创建一个新列表,然后使用for循环添加到相同的列表。我的玩具示例是:import random data = ['t1', 't2', 't3'] masterlist = [['col1', 'animal1', 'an…

用大写字母拆分字符串,但忽略AAA Python Regex - python

我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…

在Python中迭代OrderedDict - python

我有以下OrderedDict:OrderedDict([('r', 1), ('s', 1), ('a', 1), ('n', 1), ('y', 1)]) 实际上,这表示单词中字母的出现频率。第一步-我将使用最后两个元素来创建一个这样的联合元组; pair…

如果__name__ =='__main__',则为Python的Powershell等效项: - python

我真的很喜欢python的功能,例如:if __name__ == '__main__': #setup testing code here #or setup a call a function with parameters and human format the output #etc... 很好,因为我可以将Python脚本文件…

Matplotlib表行标签字体的颜色和大小 - python

给出下表:import matplotlib.pyplot as plt table=plt.table(cellText=[' ', ' ', ' ', ' ', ' '], # rows of data values rowLabels=['1&…