Home > Enterprise >  How to use tf.random.uniform and not generate duplicates
How to use tf.random.uniform and not generate duplicates

Time:11-04

I need to creare a random list of integer values that rappresent indices for other operation, the problem is that tf.random.uniform can generate the same value multiple times, and i don't want this behavious. Is there a way to change it?

indices = tf.random.uniform(shape=[size_for_layer_submodel[index], ], minval=0,
                                maxval=size_for_layer[index], dtype=tf.dtypes.int64, seed=seed, name=None)

CodePudding user response:

What you want is sampling without replacement.

Try something like this.

sorted_indices = tf.range(size_for_layer_submodel[index])
shuffled_indices = tf.shuffle(sorted_indices)

CodePudding user response:

I know you've asked specifically for tf but Numpy also has a function for this, np.random.choice, which might make things cleaner.

  • Related