Skip to content

Instantly share code, notes, and snippets.

@innat
Created May 3, 2022 03:04
Show Gist options
  • Save innat/e2e06ef399065a555b12701e59896973 to your computer and use it in GitHub Desktop.
Save innat/e2e06ef399065a555b12701e59896973 to your computer and use it in GitHub Desktop.
import tensorflow as tf
# https://github.com/tensorflow/tensorflow/issues/55646
def unique_uniform(num_samples,
minval,
maxval,
seed,
shape,
dtype): # maxval is inclusive
shuffled_index = tf.random.experimental.index_shuffle(
tf.range(num_samples, dtype=tf.int32),
seed=seed, max_index=maxval-minval
) + minval
return tf.cast(
tf.reshape(shuffled_index, shape), dtype=dtype
)
rng = tf.random.Generator.from_seed(1234)
set_seed = tf.reshape(rng.make_seeds(), [-1])
unique_uniform(
num_samples=20,
minval=5,
maxval=30,
seed=set_seed,
shape=[4,5],
dtype=tf.float32
)
<tf.Tensor: shape=(4, 5), dtype=float32, numpy=
array([[10., 27., 23., 9., 26.],
[ 5., 11., 18., 30., 16.],
[21., 15., 25., 13., 17.],
[14., 6., 28., 20., 7.]], dtype=float32)>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment