henrykuo
henrykuo

Reputation: 11

How can I initialize variables with Bernoulli distribution in TensorFlow?

How can I initialize variables in TensorFlow?

I want to associate each weight with a Bernoulli distribution:

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].

Upvotes: 1

Views: 1263

Answers (1)

Maxim
Maxim

Reputation: 53758

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)

Upvotes: 2

Related Questions