Using theano.scan within PyMC3 gives TypeError: slice indices must be integers or None or have an __index__ method
Using theano.scan within PyMC3 gives TypeError: slice indices must be integers or None or have an __index__ method
我想在 pymc3 中使用 theano.scan。当我将两个以上的变量添加为 sequences
时,我 运行 遇到了问题。这是一个简单的例子:
import numpy as np
import pymc3 as pm
import theano
import theano.tensor as T
a = np.ones(5)
b = np.ones(5)
basic_model = pm.Model()
with basic_model:
a_plus_b, _ = theano.scan(fn=lambda a, b: a + b, sequences=[a, b])
导致以下错误:
Traceback (most recent call last):
File "WhosebugExample.py", line 23, in <module>
sequences=[a, b])
File "\Anaconda3\lib\site-packages\theano\scan_module\scan.py", line 586, in scan
scan_seqs = [seq[:actual_n_steps] for seq in scan_seqs]
File "\Anaconda3\lib\site-packages\theano\scan_module\scan.py", line 586, in <listcomp>
scan_seqs = [seq[:actual_n_steps] for seq in scan_seqs]
TypeError: slice indices must be integers or None or have an __index__ method
但是,当我在 pymc 模型块外 运行 相同 theano.scan 时,一切正常:
a = T.vector('a')
b = T.vector('b')
a_plus_b, update = theano.scan(fn=lambda a, b: a + b, sequences=[a, b])
a_plus_b_function = theano.function(inputs=[a, b], outputs=a_plus_b, updates=update)
a = np.ones(5)
b = np.ones(5)
print(a_plus_b_function(a, b))
打印 [2. 2. 2. 2. 2.]
,就像它应该的那样。
此外,问题似乎是针对添加多个 sequences
。当 sequences
中有一个变量,non-sequences
中有一个变量时,一切正常。以下代码有效:
a = np.ones(5)
c = 2
basic_model = pm.Model()
with basic_model:
a_plus_c, _ = theano.scan(fn=lambda a, c: a + c, sequences=[a], non_sequences=[c])
a_plus_c_print = T.printing.Print('a_plus_c')(a_plus_c)
按预期打印 a_plus_c __str__ = [ 3. 3. 3. 3. 3.]
。
注意:我不能只用a+b代替theano.scan,因为我的实际功能比较复杂。我实际上想要这样的东西:
rewards = np.array([1, 1, 1, 1]) # reward (1) or no reward (0)
choices = np.array([1, 0, 1, 0]) # action left (1) or right (0)
Q_old = 0 # initial Q-value
alpha = 0.1 # learning rate
def update_Q(reward, choice, Q_old, alpha):
return Q_old + choice * alpha * (reward - Q_old)
Q_left, _ = theano.scan(fn=update_Q,
sequences=[rewards, choices],
outputs_info=[Q_old],
non_sequences=[alpha])
原来这是一个简单的错误!只要我将 a
和 b
定义为张量变量,一切都会正常进行。添加这两行就完成了工作:
a = T.as_tensor_variable(np.ones(5))
b = T.as_tensor_variable(np.ones(5))
我想在 pymc3 中使用 theano.scan。当我将两个以上的变量添加为 sequences
时,我 运行 遇到了问题。这是一个简单的例子:
import numpy as np
import pymc3 as pm
import theano
import theano.tensor as T
a = np.ones(5)
b = np.ones(5)
basic_model = pm.Model()
with basic_model:
a_plus_b, _ = theano.scan(fn=lambda a, b: a + b, sequences=[a, b])
导致以下错误:
Traceback (most recent call last):
File "WhosebugExample.py", line 23, in <module>
sequences=[a, b])
File "\Anaconda3\lib\site-packages\theano\scan_module\scan.py", line 586, in scan
scan_seqs = [seq[:actual_n_steps] for seq in scan_seqs]
File "\Anaconda3\lib\site-packages\theano\scan_module\scan.py", line 586, in <listcomp>
scan_seqs = [seq[:actual_n_steps] for seq in scan_seqs]
TypeError: slice indices must be integers or None or have an __index__ method
但是,当我在 pymc 模型块外 运行 相同 theano.scan 时,一切正常:
a = T.vector('a')
b = T.vector('b')
a_plus_b, update = theano.scan(fn=lambda a, b: a + b, sequences=[a, b])
a_plus_b_function = theano.function(inputs=[a, b], outputs=a_plus_b, updates=update)
a = np.ones(5)
b = np.ones(5)
print(a_plus_b_function(a, b))
打印 [2. 2. 2. 2. 2.]
,就像它应该的那样。
此外,问题似乎是针对添加多个 sequences
。当 sequences
中有一个变量,non-sequences
中有一个变量时,一切正常。以下代码有效:
a = np.ones(5)
c = 2
basic_model = pm.Model()
with basic_model:
a_plus_c, _ = theano.scan(fn=lambda a, c: a + c, sequences=[a], non_sequences=[c])
a_plus_c_print = T.printing.Print('a_plus_c')(a_plus_c)
按预期打印 a_plus_c __str__ = [ 3. 3. 3. 3. 3.]
。
注意:我不能只用a+b代替theano.scan,因为我的实际功能比较复杂。我实际上想要这样的东西:
rewards = np.array([1, 1, 1, 1]) # reward (1) or no reward (0)
choices = np.array([1, 0, 1, 0]) # action left (1) or right (0)
Q_old = 0 # initial Q-value
alpha = 0.1 # learning rate
def update_Q(reward, choice, Q_old, alpha):
return Q_old + choice * alpha * (reward - Q_old)
Q_left, _ = theano.scan(fn=update_Q,
sequences=[rewards, choices],
outputs_info=[Q_old],
non_sequences=[alpha])
原来这是一个简单的错误!只要我将 a
和 b
定义为张量变量,一切都会正常进行。添加这两行就完成了工作:
a = T.as_tensor_variable(np.ones(5))
b = T.as_tensor_variable(np.ones(5))