You can use Bernoulli distribution from Tensorflow probability library which is an extension built on Tensorflow:
import tensorflow_probability as tfp
x = tfp.distributions.Bernoulli(probs=0.7).sample(sample_shape=(10, 10))
x
This will output
<tf.Tensor: shape=(10, 10), dtype=int32, numpy=
array([[1, 1, 1, 1, 0, 0, 0, 1, 1, 0],
[1, 0, 1, 0, 1, 1, 0, 1, 0, 0],
[1, 1, 1, 1, 0, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 0, 1, 1, 1, 1, 1],
[1, 1, 0, 1, 1, 1, 1, 0, 1, 1],
[1, 0, 1, 1, 1, 0, 0, 1, 0, 1],
[0, 1, 0, 1, 0, 1, 1, 1, 1, 1],
[1, 0, 0, 1, 1, 1, 1, 1, 1, 0],
[1, 1, 1, 1, 1, 0, 1, 1, 1, 1],
[1, 0, 1, 1, 1, 1, 1, 1, 1, 1]], dtype=int32)>
The other method is to use a similar class from tf.compat
:
import tensorflow as tf
x = tf.compat.v1.distributions.Bernoulli(probs=0.7).sample(sample_shape=(10, 10))
x
You will get x
as you want but also a bunch of deprecation warnings will fall. So, I would recommend to use the 1st variant.
Also take into account that when I tested this code the latest version of tensorflow_probability
library required tensoflow>=2.3 installed.