Sunday, June 24, 2018

BUG: tensorflow tf.scatter_nd will accumulate (or undermined) values when indices have duaplicates

Problem:
WARNING: The order in which updates are applied is nondeterministic, so the output will be nondeterministic if indicescontains duplicates.

Solution:
https://github.com/tensorflow/tensorflow/issues/8102

If you are in the unpooling business:
@teramototoya what I did as a hack: with https://www.tensorflow.org/api_docs/python/tf/unique_with_counts i counted the multiplication in the indices and i divided the tensor which was holding the values (aka tensor named 'updates' in the first comment) with this counter
so when add() comes it will undo what the division made
If not, so your problem is general, then you have to somehow flatten your indices, and then tf.unique, see this post:
https://stackoverflow.com/questions/44117430/how-to-use-tf-scatter-nd-without-accumulation


2. Solution
Use a count auxilliary tensor.

cntOnes = tf.ones_like(dmVis, tf.float32)
vals = tf.stack([dmVis, mlVis, cntOnes], axis=1)
scatter_shape = tf.constant([bs, h*upsample, w*upsample, 3])
dmc = tf.scatter_nd(locVis, vals, scatter_shape)
dm, ml, cnt = tf.unstack(dmc, axis=-1)
cnt = cnt + epsilon
dm = dm / cnt

No comments:

Post a Comment