Manipulating matrix elements in tensorflow

ぐ巨炮叔叔 提交于 2019-11-28 08:37:04
dga

You can't change a tensor - but, as you noted, you can change a variable.

There are three patterns you could use to accomplish what you want:

(a) Use tf.scatter_update to directly poke to the part of the variable you want to change.

import tensorflow as tf

a = tf.Variable(initial_value=[2, 5, -4, 0])
b = tf.scatter_update(a, [1], [9])
init = tf.initialize_all_variables()

with tf.Session() as s:
  s.run(init)
  print s.run(a)
  print s.run(b)
  print s.run(a)

[ 2 5 -4 0]

[ 2 9 -4 0]

[ 2 9 -4 0]

(b) Create two tf.slice()s of the tensor, excluding the item you want to change, and then tf.concat(0, [a, 0, b]) them back together.

(c) Create b = tf.zeros_like(a), and then use tf.select() to choose which items from a you want, and which zeros from b that you want.

I've included (b) and (c) because they work with normal tensors, not just variables.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!