emil
emil

Reputation: 331

How can I randomly set elements to zero in TF?

The pure numpy solution is:

import numpy as np

data = np.random.rand(5,5) #data is of shape (5,5) with floats
masking_prob = 0.5 #probability of an element to get masked
indices = np.random.choice(np.prod(data.shape), replace=False, size=int(np.prod(data.shape)*masking_prob))
data[np.unravel_index(indices, data)] = 0. #set to zero

How can I achieve this in TensorFlow?

Upvotes: 0

Views: 428

Answers (2)

Nicolas Gervais
Nicolas Gervais

Reputation: 36714

Use tf.nn.dropout:

import tensorflow as tf
import numpy as np

data = np.random.rand(5,5)
array([[0.38658212, 0.6896139 , 0.92139911, 0.45646086, 0.23185075],
       [0.03461688, 0.22073962, 0.21254995, 0.20046708, 0.43419155],
       [0.49012903, 0.45495968, 0.83753471, 0.58815975, 0.90212244],
       [0.04071416, 0.44375078, 0.55758641, 0.31893155, 0.67403431],
       [0.52348073, 0.69354454, 0.2808658 , 0.6628248 , 0.82305081]])
tf.nn.dropout(data, rate=prob).numpy()*(1-prob)
array([[0.38658212, 0.6896139 , 0.92139911, 0.        , 0.        ],
       [0.03461688, 0.        , 0.        , 0.20046708, 0.        ],
       [0.49012903, 0.45495968, 0.        , 0.        , 0.        ],
       [0.        , 0.44375078, 0.55758641, 0.31893155, 0.        ],
       [0.52348073, 0.69354454, 0.2808658 , 0.6628248 , 0.        ]])

Dropout multiplies remaining values so I counter this by multiplying by (1-prob)

Upvotes: 2

emil
emil

Reputation: 331

For further users looking for a TF 2.x compatible answer, this is what I came up with:

import tensorflow as tf
import numpy as np

input_tensor = np.random.rand(5,5).astype(np.float32)

def my_numpy_func(x):
  # x will be a numpy array with the contents of the input to the
  # tf.function
  p = 0.5
  indices = np.random.choice(np.prod(x.shape), replace=False, size=int(np.prod(x.shape)*p))
  x[np.unravel_index(indices, x.shape)] = 0.

  return x


@tf.function(input_signature=[tf.TensorSpec((None, None), tf.float32)])
def tf_function(input):
  y = tf.numpy_function(my_numpy_func, [input], tf.float32)
  return y

tf_function(tf.constant(input_tensor))

You can also use this is code in the context of a Dataset by using the map() operation.

Upvotes: 0

Related Questions