How can I initialize variables with Bernoulli distribution in TensorFlow?

1.2k Views Asked by At

How can I initialize variables in TensorFlow?

I want to associate each weight with a Bernoulli distribution:

  • with probability of p to get some value x1, and
  • with probability of 1-p to get some value x2.

How should I initialize this matrix?

I wrote this code:

logits_y = tf.get_variable("logits", [n_input*n_hidden,2],
                           initializer=tf.constant_initializer(1.))

The 2 in [n_input*n_hidden, 2] means [p, 1-p].

1

There are 1 best solutions below

1
On

I'm not sure what exactly you plan to do with your matrix, but here's how you can generate Bernoulli distribution in tensorflow:

>>> distrib = tf.contrib.distributions.Bernoulli(probs=[0.3])
>>> sample = distrib.sample([10])
>>> sample
<tf.Tensor 'Bernoulli/sample/Reshape:0' shape=(10, 1) dtype=int32>
>>> sample.eval()
array([[0],
       [0],
       [1],
       [1],
       [0],
       [0],
       [0],
       [1],
       [0],
       [0]], dtype=int32)