我的训练功能:
def fit(self, X, y):
batch_size = 20
index = T.lscalar() # index to a [mini]batch
updates = {}
return theano.function(
inputs=[index], outputs=self.cost, updates=updates,
givens={
self.sym_X: X[index * batch_size:(index + 1) * batch_size],
self.sym_y: y[index * batch_size:(index + 1) * batch_size]})
然后从其他地方:
fn = obj.fit(X, y)
for i in range(10):
fn(i)
所以我想要的是这样的
fn = obj.fit(X, y)
fn()
我真的不确定如何开始这个,因为theano对我来说仍然非常令人折服.我能够做到这一点,但循环是非常具有挑战性的.
我有一个模糊的概念,如果我可以将theano.function转换为theano.scan,然后在它周围放置一个外部theano.function – 这可能会起作用.然而,theano.scan对我来说仍然是神奇的(尽管我付出了最大的努力).
我怎样才能将循环的小型数据集合并到单个函数调用中?
更新:
我以为我拥有它!我懂了:
def fit(self, X, y):
batch_size = 20
n_batches = 5
index = theano.shared(0)
## index to a [mini]batch
updates = {
index: index + batch_size
}
return theano.function(
inputs=[], outputs=[self.cost] * n_batches, updates=updates,
givens={
index: 0,
self.sym_X: X[index * batch_size:(index + 1) * batch_size],
self.sym_y: y[index * batch_size:(index + 1) * batch_size]})
但不幸的是,似乎因为我使用索引来计算批次中的批次,我也无法更新它:
Traceback (most recent call last):
File "skdeeplearn/classifiers/test/test_classifiers.py", line 79, in test_logistic_sgd
fn = clf.fit(self.shared_X, self.shared_y)
File "skdeeplearn/classifiers/logistic_sgd.py", line 139, in fit
self.sym_y: y[index * batch_size:(index + 1) * batch_size]})
File "/Users/aelaguiz/workspace/pyvotune/venv/lib/python2.7/site- packages/theano/compile/function.py", line 206, in function
profile=profile)
File "/Users/aelaguiz/workspace/pyvotune/venv/lib/python2.7/site-packages/theano/compile/pfunc.py", line 461, in pfunc
no_default_updates=no_default_updates)
File "/Users/aelaguiz/workspace/pyvotune/venv/lib/python2.7/site-packages/theano/compile/pfunc.py", line 162, in rebuild_collect_shared
"to be replaced by %s." % (v_orig, v_repl))
AssertionError: When using 'givens' or 'replace' with several (old_v, new_v) replacement pairs, you can not have a new_v variable depend on an old_v one. For instance, givens = {a:b, b:(a+1)} is not allowed. Here, the old_v is used to compute other new_v's, but it is scheduled to be replaced by .
更新2:
def fit(self, X, y):
batch_size = 20
n_batches = 5
index = theano.shared(0)
## index to a [mini]batch
updates = {
index: index + batch_size
}
return theano.function(
inputs=[], outputs=[self.cost] * n_batches, updates=updates,
givens={
self.sym_X: X[index * batch_size:(index + 1) * batch_size],
self.sym_y: y[index * batch_size:(index + 1) * batch_size]})
这实际上运行,但它的输出很奇怪:
[array(0.6931471824645996, dtype=float32), array(0.6931471824645996, dtype=float32), array(0.6931471824645996, dtype=float32), array(0.6931471824645996, dtype=float32), array(0.6931471824645996, dtype=float32)]
我每次运行它都得到相同的输出,即使X& y被初始化为每次运行的随机值.