我刚开始使用theano.scan和theano.scan_module.until。从docs 这里中,我不知道如何在while循环中设置变量,也不确定如何调整这个post 这里来使用theano.scan_module.until。
这是我想要翻译成等价的theano的代码。有人想试试翻译这个吗?(或许还可以解释翻译的代码。)
# Code to perform a random walk using a row stochastic matrix M.
for i in range(100):
r_last = r
r = r.dot(M)
err = np.linalg.norm(r - r_last, ord=1).sum()
if err < N * tol:
break我看到这里有三个任务分配操作,还有一个if语句。但我不知道怎么把这个翻译给提亚诺。
如果您有兴趣,可以将这段代码粘贴到上面来设置变量。
import numpy as np
N = 3
tol = 1.0e-6
M = np.random.rand(N, N)
M = M / M.sum(axis=1).reshape(-1, 1)
r = np.ones(N, dtype=np.float) / N发布于 2016-04-12 00:53:34
给予:
N = 3
tol = 1.0e-6您可以这样定义您的符号函数:
r_init = T.vector()
W = T.matrix()
def step(r):
r_prime = T.dot(r, W)
delta = (r_prime - r).norm(1)
condition = T.lt(delta, N * tol)
return r_prime, theano.scan_module.until(condition)
outputs, updates = theano.scan(step, outputs_info=[r_init], n_steps=1024)
r_final = outputs[-1]
solve = theano.function(inputs=[r_init, W], outputs=r_final)然后像这样使用它:
M = np.random.rand(N, N)
M /= M.sum(axis=1).reshape((-1, 1))
r = np.ones(N, dtype=np.float) / N
print solve(r, M)顺便说一句,你不是在执行“随机游走”。你正在解r,所以rW =r,通常称为马尔可夫链的平稳分布。
https://stackoverflow.com/questions/36481372
复制相似问题