# Loss are NaN when using KLqp or Bayesian by Backpropagation

**URL:** <https://discourse.edwardlib.org/t/loss-are-nan-when-using-klqp-or-bayesian-by-backpropagation/706>\
**Category:** General\
**Created:** [March 31, 2018, 9:14am UTC](https://discourse.edwardlib.org/t/loss-are-nan-when-using-klqp-or-bayesian-by-backpropagation/706 "2018-03-31T09:14:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sejabs](https://avatars.discourse-cdn.com/v4/letter/s/db5fbb/32.png) [@sejabs](https://discourse.edwardlib.org/u/sejabs)\
**Post date:** [March 31, 2018, 9:14am UTC](https://discourse.edwardlib.org/t/loss-are-nan-when-using-klqp-or-bayesian-by-backpropagation/706/1 "2018-03-31T09:14:56Z")

</div>

I am a newbie with Tensorflow. I used pymc3 for my project and heard Theano will stop developing, so I have to swing to Tensorflow and edward to implement Bayesian Deep learning.  
When I read [Weight Uncertainty in Neural Networks](https://arxiv.org/abs/1505.05424), I planed to implement the Bayesian by Backpropagation algorithm they proposed based on edward’s KLqp, and I was lucky to find there is a tutorial with Mxnet [Bayes by Backprop from scratch (NN, classification)](http://gluon.mxnet.io/chapter18_variational-methods-and-uncertainty/bayes-by-backprop.html). After studying the code, I have found that only the `loss function` need to modify according to the paper and delete the scale option in the KLqp class.  
However, in the paper the writer also proposed a mixture scale prior like a spike-and-slab. I implement this with `Mixtrue`

> sigma\_p1 = 0.75  
> sigma\_p2 = 0.1  
> pi = 0.25  
> probs = [pi, 1.-pi]

> n\_hidden\_1 = 400  
> cat\_W\_1 = cat\_batch\_shape(dim=[num\_inputs, n\_hidden\_1, len(probs)])  
> W\_1 = Mixture(cat=Categorical(probs=tf.convert\_to\_tensor(cat\_W\_1, dtype=tf.float32)),  
> components=[Normal(loc=tf.zeros([num\_inputs, n\_hidden\_1]),  
> scale=tf.constant(sigma\_p1,shape=[num\_inputs, n\_hidden\_1])),  
> Normal(loc=tf.zeros([num\_inputs, n\_hidden\_1]),  
> scale=tf.constant(sigma\_p2, shape=[num\_inputs, n\_hidden\_1]))])

`cat_batch_shape` is a function to keep shape of cat property of Mixture same with components. But when I run the code with KLqp or BBB, the same nan errors show:  
`tensorflow.python.framework.errors_impl.InvalidArgumentError: Nan in summary histogram for: gradient/qW_3/mu/0 [[Node: gradient/qW_3/mu/0 = HistogramSummary[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"](gradient/qW_3/mu/0/tag, gradients/AddN_11/_197)]] [[Node: norm_10/Squeeze/_194 = _Send[T=DT_FLOAT, client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_1702_norm_10/Squeeze", _device="/job:localhost/replica:0/task:0/device:GPU:0"](norm_10/Squeeze)]]`  
Dose the mixture scale priors produce these errors? Because I have reproduce the code according to [MNIST FOR ML BEGINNERS: THE BAYESIAN WAY](https://www.alpha-i.co/blog/MNIST-for-ML-beginners-The-Bayesian-Way.html), both algorithm are successful.

---

<div class="post-metadata">

**Author:** ![sejabs](https://avatars.discourse-cdn.com/v4/letter/s/db5fbb/32.png) [@sejabs](https://discourse.edwardlib.org/u/sejabs)\
**Post date:** [April 2, 2018, 3:37am UTC](https://discourse.edwardlib.org/t/loss-are-nan-when-using-klqp-or-bayesian-by-backpropagation/706/2 "2018-04-02T03:37:57Z")

</div>

I can answer myself ^-^!  
First, I used tfdbg with filter `has_inf_or_nan` to debug the nan source. If the OS is Windows, there may be some problems.  
Second, I found the problem was stem from learning rate. So I modified the VI and KLqp file to enable directly setting learning rate. With smaller learning rate 0…001, finally I got the comparable results with [Bayes by Backprop from scratch](http://gluon.mxnet.io/chapter18_variational-methods-and-uncertainty/bayes-by-backprop.html).

---

<div class="post-metadata">

**Author:** ![YSanchezAraujo](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.edwardlib.org/ysanchezaraujo/32/312_2.png) [@YSanchezAraujo](https://discourse.edwardlib.org/u/YSanchezAraujo)\
**Post date:** [May 5, 2018, 8:29pm UTC](https://discourse.edwardlib.org/t/loss-are-nan-when-using-klqp-or-bayesian-by-backpropagation/706/3 "2018-05-05T20:29:54Z")

</div>

hi @sejabs, could you provide information for how you reduced the learning rate through the edward api?
