Created
May 3, 2022 03:04
-
-
Save innat/e2e06ef399065a555b12701e59896973 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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