问题描述
我正在尝试为 tensorflow 中的变量切片赋值,但显示以下错误:ValueError: Sliced assignment is only supported for variables".为什么即使我正在尝试对变量进行切片分配,也会显示此错误.我的代码是这样的:
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
是一个 tensorflow 变量.
where var1
is a tensorflow variable.
推荐答案
另一个答案是正确的;对 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(...)
这篇关于切片分配给 tensorflow 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!