は、ticket #206で議論されています。 まだ利用できません。
更新:これが実装されました。あなたのようなあなたが更新する所望の値に沿って変更したくない値を含む目的の行のブロックを構築することができ、利用可能であるassign_slice_update
(または)までhttps://stackoverflow.com/a/43139565/6531137
:jdehesaの回答を参照してください。したがって:
import tensorflow as tf
a = tf.Variable(tf.ones([10,36,36]))
i = 3
j = 5
# Gather values inside the a[i,...] block that are not on column j
idx_before = tf.concat(1, [tf.reshape(tf.tile(tf.Variable([i]), [j]), [-1, 1]), tf.reshape(tf.range(j), [-1, 1])])
values_before = tf.gather_nd(a, idx_before)
idx_after = tf.concat(1, [tf.reshape(tf.tile(tf.Variable([i]), [36-j-1]), [-1, 1]), tf.reshape(tf.range(j+1, 36), [-1, 1])])
values_after = tf.gather_nd(a, idx_after)
# Build a subset of tensor `a` with the values that should not be touched and the values to update
block = tf.concat(0, [values_before, 5*tf.ones([1, 36]), values_after])
d = tf.scatter_update(a, i, block)
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
sess.run(d)
print(a.eval()[3,4:7,:]) # Print a subset of the tensor to verify
この例では、1のテンソルを生成し、a[i,j,:] = 5
を実行します。複雑さの大部分は、変更したくない値を取得することにあります。a[i,~j,:]
(そうでない場合はscatter_update()
がそれらの値を置き換えます)。
質問したとおりにT[i,k,:] = a[1,1,:]
を実行する場合は、前の例の5*tf.ones([1, 36])
をtf.gather_nd(a, [[1, 1]])
に置き換える必要があります。
別のアプローチは、それからtf.select()
所望の要素にマスクを作成し、そのようなものとして、バック変数に代入することであろう。
import tensorflow as tf
a = tf.Variable(tf.zeros([10,36,36]))
i = tf.Variable([3])
j = tf.Variable([5])
# Build a mask using indices to perform [i,j,:]
atleast_2d = lambda x: tf.reshape(x, [-1, 1])
indices = tf.concat(1, [atleast_2d(tf.tile(i, [36])), atleast_2d(tf.tile(j, [36])), atleast_2d(tf.range(36))])
mask = tf.cast(tf.sparse_to_dense(indices, [10, 36, 36], 1), tf.bool)
to_update = 5*tf.ones_like(a)
out = a.assign(tf.select(mask, to_update, a))
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
sess.run(out)
print(a.eval()[2:5,5,:])
それが二回必要とするので、これは潜在的にあまり効率的なメモリの点ですと同じようにto_update
の変数を扱うメモリがありますが、この最後の例を簡単に変更して、tf.select(...)
ノードからグラディエント保存操作を得ることができます。また、この他のStackOverflowに関する質問に興味があるかもしれません:Conditional assignment of tensor values in TensorFlow。
これらのエレガントでない曲げは、使用可能になると、適切なTensorFlow関数への呼び出しに置き換えてください。
詳細な例をありがとうございます!あなたが言ったように、現在のやり方はやや控えめです。すぐに 'assign_slice_update'関数が利用できることを願っています。 – user270700
tf.selectがテンソルフロー1のtf.whereに置き換えられました。 –