1

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

Maxim
  • 52,561
  • 27
  • 155
  • 209
henrykuo
  • 11
  • 2
  • How are you planning on using `logits_y`? In the loss function or for producing summaries? – musically_ut Dec 21 '17 at 10:26
  • @musically_ut I want to draw samples using logits_y, the weights now are from different Bernoulli distributions. And I want to optimize p for each weight in some way. – henrykuo Dec 21 '17 at 18:04

1 Answers1

2

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)
Maxim
  • 52,561
  • 27
  • 155
  • 209
  • Thanks for replying. The problem I have is that I want to optimize the Bernoulli distribution for each weight by drawing samples. So I must make each p as a variable and optimize it using SGD or some similar algorithms. – henrykuo Dec 21 '17 at 19:07