0

In Tensorflow, I would like to create a mask of all ones, except a single element, where the element location is chosen randomly. Specifically, I would like to create a random mask of either [0,1,1,1] or [1,1,0,1]

I tried:

import tensorflow as tf
import numpy as np

# randomly draw the column to select (either column 0 or 2)
COLselect = tf.to_int64(tf.greater(tf.random_uniform((1,)), 0.5))
column_num = COLselect*2

ones_mask = tf.Variable(numpy.ones((4,)))

And I am stuck on how to create a new mask that nulls column_num column of ones_mask.

I tried following Manipulating matrix elements in tensorflow, but failed because the column is a tensor.

Thanks

Community
  • 1
  • 1
Yuval Atzmon
  • 5,645
  • 3
  • 41
  • 74

2 Answers2

1

Another option would be to use tf.one_hot with a custom on_value and off_value, and use a random integer as the index. You can use tf.reshape to get the output in the right shape.

Peter Hawkins
  • 3,201
  • 19
  • 17
0

With Tensorflow r0.10 :

import tensorflow as tf
import numpy as np

# randomly draw the column to select (either column 0 or 2)
COLselect = tf.to_int64(tf.greater(tf.random_uniform((1,)), 0.5))
column_num = COLselect*2

ones_mask = tf.Variable(np.ones((4,)))
mask_op = tf.scatter_update(ones_mask,column_num,[0])

sess = tf.Session()
sess.run(tf.initialize_all_variables())
sess.run(mask_op)

give me :

array([ 0., 1., 1., 1.])

or

array([ 1., 1., 0., 1.])

The only problem is that tf.scatter_update modify the variable. It's normal, but when you want call mask_op a second time, you should get :

array([ 0., 1., 0., 1.])

So initialize ones_mask before each mask_op call.

Corentin
  • 201
  • 2
  • 5