2017-07-04 7 views
4

入れ子のwhile_loop()の二次元テンソルを更新しようとしています。私はWHILE_LOOP外の変数を作成し、最初のループでそれを使用する場合TensorFlow while_loopは変数を定数に変換しますか?

ValueError: Sliced assignment is only supported for variables 

はどういうわけか、それが正常に動作します:しかし、第二のループに変数を渡すとき、私はそれがこのエラーがスローされますようtf.assign()を使用して、それを更新することはできません。

2番目のwhileループで2Dのtf変数を変更するにはどうすればよいですか?

私のコード(私は、Python 2.7とTensorFlow 1.2を使用しています):ループ変数が異なって実装されているよう

import tensorflow as tf 
import numpy as np 

tf.reset_default_graph() 

BATCH_SIZE = 10 
LENGTH_MAX_OUTPUT = 31 

it_batch_nr = tf.constant(0) 
it_row_nr = tf.Variable(0, dtype=tf.int32) 
it_col_nr = tf.constant(0) 
cost = tf.constant(0) 

it_batch_end = lambda it_batch_nr, cost: tf.less(it_batch_nr, BATCH_SIZE) 
it_row_end = lambda it_row_nr, cost_matrix: tf.less(it_row_nr, LENGTH_MAX_OUTPUT+1) 

def iterate_batch(it_batch_nr, cost): 
    cost_matrix = tf.Variable(np.ones((LENGTH_MAX_OUTPUT+1, LENGTH_MAX_OUTPUT+1)), dtype=tf.float32) 
    it_rows, cost_matrix = tf.while_loop(it_row_end, iterate_row, [it_row_nr, cost_matrix]) 
    cost = cost_matrix[0,0] # IS 1.0, SHOULD BE 100.0 
    return tf.add(it_batch_nr,1), cost 

def iterate_row(it_row_nr, cost_matrix): 
    # THIS THROWS AN ERROR: 
    cost_matrix[0,0].assign(100.0) 
    return tf.add(it_row_nr,1), cost_matrix 

it_batch = tf.while_loop(it_batch_end, iterate_batch, [it_batch_nr, cost]) 

sess = tf.InteractiveSession() 
sess.run(tf.global_variables_initializer()) 
out = sess.run(it_batch) 
print(out) 
+0

あなたはあなたの解決策を見つけることができ、答えではないの質問に編集としてあなたのソリューションを渡してください[改訂3](https://stackoverflow.com/revisions/44912041/:ループは次のようになります3)、ちょうどコピーしてそれを答えに入れる –

答えて

0

@AlexandrePassosヘルプで、変数をwhile_loopの外側に置くことで、これを動作させることができます。しかし、私はまた、tf.control_dependencies()を使ってコマンドの実行を強制しなければなりませんでした(ループ変数で操作が直接使用されないため)。

cost_matrix = tf.Variable(np.ones((LENGTH_MAX_OUTPUT+1, LENGTH_MAX_OUTPUT+1)), dtype=tf.float32) 

def iterate_batch(it_batch_nr, cost): 
    it_rows = tf.while_loop(it_row_end, iterate_row, [it_row_nr]) 
    with tf.control_dependencies([it_rows]): 
     cost = cost_matrix[0,0] 
     return tf.add(it_batch_nr,1), cost 

def iterate_row(it_row_nr): 
    a = tf.assign(cost_matrix[0,0], 100.0) 
    with tf.control_dependencies([a]): 
     return tf.add(it_row_nr,1) 
1

tf.Variableオブジェクトは、whileループのループ変数として使用することはできません。

したがって、変数をループの外に作成し、ループごとにtf.assignを使用して更新するか、ループ変数と同じように手動で更新を追跡します(ループlambdasから更新された値を返します。外側のループの新しい値として内側のループからの値を使用する)。

+0

ありがとう、それはエラーを取り除く。しかし、ループの外側で 'cost_matrix'変数宣言を移動してループ変数から取り除くと、' cost'は1.0のままです。 – Deruijter

+0

明らかに私は操作が出力変数を実行する。 TFがコードを解釈して最適化する方法と関係があると私は考えています。私はこれを克服するためのハッキーな解決策を使用しましたが、私はこれを行うより良い方法があると仮定します(質問の編集を参照)。これについて新しい質問をします。 – Deruijter

+0

更新:実行する操作をトリガするには、 'tf.control_dependencies()'(問題の解決策が更新されました)を使用します。 – Deruijter

関連する問題