切片分配给张量流变量 [英] Slice assignment to tensorflow variable
本文介绍了切片分配给张量流变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图在张量流中为变量的切片分配值,但显示以下错误:'ValueError:仅变量支持切片分配'.为什么即使我尝试对变量进行切片分配,也会显示此错误?我的代码是这样的:
I am trying to assign values to slice of a variable in tensorflow, but the following error is shown: 'ValueError: Sliced assignment is only supported for variables'. Why is this error showing even though I am trying to do slice assignment to a variable. My code is something like this:
var1 = var1[startx:endx,starty:endy].assign(tf.ones([endx-startx,endy-starty],dtype=tf.int32))
其中var1
是张量流变量.
推荐答案
另一个答案是正确的.对tf变量执行任何操作都不会(总是)返回tf变量.因此,如果要链接分配,请使用显式控件依赖项:
The other answer is correct; doing any operation to a tf variable does not (always) return a tf variable. So if you're chaining assignments, use explicit control dependencies:
v = tf.Variable(...)
with tf.control_dependencies([v[...].assign(...)]):
return v[...].assign(...)
这篇关于切片分配给张量流变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文