簡體   English   中英

theano定義了重復調用另一個函數的函數?

[英]theano define function which repeatedly calls another function?

我的訓練功能:

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 <TensorType(int64, scalar)> is used to compute other new_v's, but it is scheduled to be replaced by <TensorType(int64, scalar)>.

更新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被初始化為每次運行的隨機值。

我認識的每個人都在python中循環使用minibatch。 這可以通過掃描完成,但您在此處的所有嘗試都沒有使用掃描。 所以他們沒有工作是正常的。 您需要在某處調用掃描功能才能使用它(或者更高級別的界面,如地圖)。 事實上,在你的情況下,我認為你可以使用theano.scan(fn, theano.tensor.arange(N))

我不能在這篇文章中回答你的所有問題,因為代碼片段不完整,但這里有一些信息:

return theano.function(
    inputs=[], outputs=[self.cost] * n_batches,

這里: [self.cost] * n_batches是純Python代碼。 這將創建一個n_batches元素列表,其中每個元素都是self.cos t。 因此,如果n_batches為3,則outputs=[self.cost, self.cost, self.cost] 這就是為什么你多次輸出相同的值。

我不能告訴你為什么你總是添加相同的答案,因為我需要未提供的信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM