gpt4 book ai didi

python - 使用 PYMC3/Theano 广播数学运算

转载 作者:行者123 更新时间:2023-11-30 22:51:01 26 4
gpt4 key购买 nike

我认为这个问题归结为我对 Theano 工作缺乏理解。我现在的情况是,我想创建一个变量,该变量是分布和 numpy 数组之间相减的结果。当我将形状参数指定为 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,)

但是,当我指定形状 > 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)

最佳答案

你可以做到

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

关于python - 使用 PYMC3/Theano 广播数学运算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39151247/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com