6

我正在尝试运行以下模型,但在编译过程中失败:

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 的熟悉程度)。

一般来说,我很难理解如何实现具有多个维度的多级模型。

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


版本

  • 蟒蛇3.6.3
  • numpy 1.14.5
  • Theano 1.0.2
  • pymc3 3.4.1
  • Mac OS 10.13.5

更新

我在 HPC 节点(CentOS 7)上安装了相同的软件包版本(通过conda),并且能够运行@colcarroll 建议的模型的修改版本。然而,在我的 OS X 机器上,我仍然看到上面指出的 Theano 编译错误,即使模型发生了变化。这可能是个clang问题吗?可以指定 Theano 使用的编译器吗?

4

2 回答 2

15

一种解决方法是禁止编译警告:

import theano

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

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

我仍然希望看到一个解释潜在问题​​的答案。

于 2018-07-12T19:00:23.907 回答
3

是的 - 对于更高维度的形状,您必须更加明确。该库有点“聪明”,但如果您提供shape参数,它将使用它。

您的示例在语法上是通过设置固定的

with pm.Model() as m:

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

    # Observed proportions
    p_gk = pm.Beta('p_gk', mu=p_g.dot(np.ones((1,K))), sd=sd_g.dot(np.ones((1, K))), shape=(G, K), observed=data)
    trace = pm.sample()

请注意,运行将m.check_test_point()显示p_gk概率为 0。这是因为sd_g太宽了,PyMC3 尝试将其初始化为 0.8,这超出了mu, sd参数化 beta 分布的支持。

设置sd_g = pm.HalfNormal('sd_g', sd=0.1, shape=(G, 1))还允许您从模型中采样,尽管这可能不是您想要的!

于 2018-07-11T17:54:45.887 回答