【问题标题】:Broadcasting mathematical operations with PYMC3/Theano使用 PYMC3/Theano 广播数学运算
【发布时间】:2016-08-26 13:57:08
【问题描述】:

我认为这个问题归结为我对Theano 作品缺乏了解。我处于一种情况,我想创建一个变量,该变量是分布和 numpy 数组之间减法的结果。当我将 shape 参数指定为 1

时,这可以正常工作
import pymc3 as pm
import numpy as np
import theano.tensor as T

X = np.random.randint(low = -10, high = 10, size = 100)

with pm.Model() as model:
    nl = pm.Normal('nl', shape = 1)
    det = pm.Deterministic('det', nl - x)

nl.dshape
(1,)

但是,当我指定 shape > 1 时,这会中断

with pm.Model() as model:
    nl = pm.Normal('nl', shape = 2)
    det = pm.Deterministic('det', nl - X)

ValueError: Input dimension mis-match. (input[0].shape[0] = 2, input[1].shape[0] = 100)

nl.dshape
(2,)

X.shape
(100,)

我尝试转置 X 以使其可广播

X2 = X.reshape(-1, 1).transpose()

X2.shape
(1, 100)

但现在它在 .shape[1] 而不是 .shape[0] 声明不匹配

with pm.Model() as model:
    nl = pm.Normal('nl', shape = 2)
    det = pm.Deterministic('det', nl - X2)

ValueError: Input dimension mis-match. (input[0].shape[1] = 2, input[1].shape[1] = 100)

如果我遍历分布的元素,我可以完成这项工作

distShape = 2
with pm.Model() as model:
    nl = pm.Normal('nl', shape = distShape)

    det = {}
    for i in range(distShape):
        det[i] = pm.Deterministic('det' + str(i), nl[i] - X)

det
{0: det0, 1: det1}

但是,这感觉不雅,并限制我对模型的其余部分使用循环。我想知道是否有一种方法可以指定此操作,以便它可以与分发版一样工作。

distShape = 2
with pm.Model() as model:
    nl0 = pm.Normal('nl1', shape = distShape)
    nl1 = pm.Normal('nl2', shape = 1)

    det = pm.Deterministic('det', nl0 - nl1)

【问题讨论】:

    标签: python theano bayesian pymc3


    【解决方案1】:

    你可以的

    X = np.random.randint(low = -10, high = 10, size = 100)
    X = x[:,None] # or x.reshape(-1, 1)
    

    然后

    with pm.Model() as model:
        nl = pm.Normal('nl', shape = 2)
        det = pm.Deterministic('det', nl - X)
    

    在这种情况下,nl 和 X 的形状将分别为 ((2, 1), (100,)),然后可广播。

    请注意,我们使用两个 NumPy 数组(不仅仅是一个 Theano 张量和一个 NumPy 数组)得到相同的行为

    a0 = np.array([1,2])
    b0 = np.array([1,2,3,5])
    a0 = a0[:,None]  # comment/uncomment this line
    print(a0.shape, b0.shape)
    b0-a0
    

    【讨论】:

    • 谢谢!我将不得不仔细检查这是否会在 pymc3 中产生我想要的行为,但我很乐观地认为这是一个很好的解决方案!
    猜你喜欢
    • 1970-01-01
    • 2014-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-22
    相关资源
    最近更新 更多