Tensorflow 在while循环中使用scatter\u和\u add op?
我试图在Tensorflow 在while循环中使用scatter\u和\u add op?,tensorflow,Tensorflow,我试图在tf.while循环中执行tf.scatter\u\u添加。但它给了我一个错误消息 TypeError: 'ScatterNdAdd' Op requires that input 'ref' be a mutable tensor (e.g.: a tf.Variable) 循环工作的方式似乎是将输入变量从tf.variable类型更改为其他类型。可以在while循环中执行分散和添加操作吗 下面是一段代码摘录: self.hough = tf.Variable(tf.zeros((n
tf.while
循环中执行tf.scatter\u\u添加
。但它给了我一个错误消息
TypeError: 'ScatterNdAdd' Op requires that input 'ref' be a mutable tensor (e.g.: a tf.Variable)
循环工作的方式似乎是将输入变量从tf.variable
类型更改为其他类型。可以在while循环中执行分散和添加操作吗
下面是一段代码摘录:
self.hough = tf.Variable(tf.zeros((num_points, num_points), dtype=tf.float32),
name='hough')
self.i = tf.constant(0)
c = lambda i, hough: tf.less(i, tf.squeeze(tf.shape(self.img_x)))
b = lambda i, hough: self.ProcessPixel(i, hough)
self.r = tf.while_loop(c, b, [self.i, self.hough], back_prop=False)
这是循环的主体:
def ProcessPixel(self, i, hough):
pixel_x, pixel_y = self.img_x[self.i], self.img_y[self.i]
result = self.GetLinesThroughPixel(pixel_x, pixel_y)
idx = tf.stack([tf.range(num_points, dtype=tf.int64), result])
pixel_val = self.img[tf.to_int32(pixel_x), tf.to_int32(pixel_y)]
print type(hough)
updated_hough = tf.scatter_nd_add(hough, tf.transpose(idx),
updates=pixel_val * tf.ones(num_points))
return tf.add(i, 1), updated_hough
你能再发一些你的代码吗?特别是while_循环和传入的输入。@AvishkarBhoopchand我添加了一个代码示例。我不确定它是否有助于解决这个问题。与其在循环中向变量添加散点,为什么不直接累积结果并在最后应用它呢?所以像
updated\u hough=hough+tf.scatter\u nd(…)
这样的东西,在while循环结束时执行tf.assign(self.hough,self.r[1])?我不确定我是否理解您的建议或它是否有效。你能再解释一下吗?