3

私のトレーニング機能:

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 は実行ごとにランダムな値に初期化されますが、実行するたびに同じ出力が得られます。

4

1 に答える 1