Sunday, October 1, 2017

Using scatter_nd_add op inside a while loop?

Leave a Comment

I'm trying to do a tf.scatter_nd_add inside a tf.while loop. It gives me an error message though.

TypeError: 'ScatterNdAdd' Op requires that input 'ref' be a mutable tensor (e.g.: a tf.Variable) 

It seems like the way that while loops work is that it changes the input variable from being of type tf.Variable to having some other type. Is it possible to do a scatter_nd_add op inside a while loop?

Here's a code excerpt:

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) 

This is the body of the loop:

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 

0 Answers

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment