# Inf Average loss ADVI in Correlated Topic Model

**URL:** <https://discourse.pymc.io/t/inf-average-loss-advi-in-correlated-topic-model/1563>\
**Category:** Questions\
**Created:** [July 23, 2018, 11:06pm UTC](https://discourse.pymc.io/t/inf-average-loss-advi-in-correlated-topic-model/1563 "2018-07-23T23:06:11Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![gaddamanil16](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/gaddamanil16/32/927_2.png) [@gaddamanil16](https://discourse.pymc.io/u/gaddamanil16)\
**Post date:** [July 23, 2018, 11:06pm UTC](https://discourse.pymc.io/t/inf-average-loss-advi-in-correlated-topic-model/1563/1 "2018-07-23T23:06:11Z")

</div>

Hi,

While implementing Correlated Topic Model with ADVI I get inf average loss. What does it mean? Now I’m unsure whether I’m implementing it in a correct way or not.

Here is my code:

```
import numpy as np
import pymc3 as pm, theano.tensor as t
import matplotlib.pyplot as plt
K = 4
V = 4 # number of words
D = 10 # number of documents

data = np.random.randint(V,size=(D,4))

alpha = np.ones((1, K))
beta = np.ones((1, V))
model = pm.Model()

mu = np.ones((1,K),dtype = np.float64)
cov = np.random.random_sample(size=(K,K))

Wd = [len(doc) for doc in data]
(D, W) = data.shape

def log_lda(theta,phi):
    def ll_lda(value):  
        dixs, vixs = value.nonzero()
        vfreqs = value[dixs, vixs]
        ll =vfreqs* pm.math.logsumexp(t.log(theta[dixs]) + t.log(phi.T[vixs]), axis = 1).ravel()
        return t.sum(ll) 
    return ll_lda

with model: 
    eta = pm.MvNormal('eta',mu = mu,cov = cov, shape = (D,K))
    theta = t.nnet.softmax(eta)
    phi = pm.Dirichlet("phi", a=beta, shape=(K, V))
    doc = pm.DensityDist('doc', log_lda(theta,phi), observed=data)   
with model:    
    inference = pm.ADVI()
    approx = pm.fit(n=10000,method= inference,callbacks=[pm.callbacks.CheckParametersConvergence(diff='absolute')])
   
#inference    
tr1 = approx.sample(draws=1000)
pm.plots.traceplot(tr1);    
pm.plot_posterior(tr1, color='LightSeaGreen');

plt.plot(approx.hist)

```

Can someone please guide me with this?  
Help much appreciated.  
Thanks

---

<div class="post-metadata">

**Author:** ![gaddamanil16](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/gaddamanil16/32/927_2.png) [@gaddamanil16](https://discourse.pymc.io/u/gaddamanil16)\
**Post date:** [July 23, 2018, 11:07pm UTC](https://discourse.pymc.io/t/inf-average-loss-advi-in-correlated-topic-model/1563/2 "2018-07-23T23:07:00Z")

</div>

Average Loss = inf: 100%|██████████| 10000/10000 [00:06\<00:00, 1497.31it/s]  
Finished [100%]: Average Loss = nan

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [July 24, 2018, 4:22am UTC](https://discourse.pymc.io/t/inf-average-loss-advi-in-correlated-topic-model/1563/3 "2018-07-24T04:22:26Z")

</div>

What is `plt.plot(approx.hist)` looks like?

---

<div class="post-metadata">

**Author:** ![gaddamanil16](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/gaddamanil16/32/927_2.png) [@gaddamanil16](https://discourse.pymc.io/u/gaddamanil16)\
**Post date:** [July 24, 2018, 3:16pm UTC](https://discourse.pymc.io/t/inf-average-loss-advi-in-correlated-topic-model/1563/4 "2018-07-24T15:16:53Z")

</div>

It gives me nothing. Its blank.

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [July 24, 2018, 4:27pm UTC](https://discourse.pymc.io/t/inf-average-loss-advi-in-correlated-topic-model/1563/5 "2018-07-24T16:27:13Z")

</div>

Yeah there is definitely some problems of your model. You should double check the likelihood implementation, print the testpoint and `model.check_test_point`.

---

<div class="post-metadata">

**Author:** ![gaddamanil16](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/gaddamanil16/32/927_2.png) [@gaddamanil16](https://discourse.pymc.io/u/gaddamanil16)\
**Post date:** [July 24, 2018, 4:39pm UTC](https://discourse.pymc.io/t/inf-average-loss-advi-in-correlated-topic-model/1563/6 "2018-07-24T16:39:44Z")

</div>

It gives me

```
eta -inf
phi_stickbreaking__ -15.01367190100603
doc -72.08730677823429

```

For eta :

 ![Screenshot%20from%202018-07-24%2012-37-24](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/1X/be16def93bca514e9fa83bb149521785bdb42739.png)

Using this I have used  
`eta = pm.MvNormal('eta',mu = mu,cov = cov, shape = (D,K))`

Is there anything wrong with this?

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [July 24, 2018, 7:19pm UTC](https://discourse.pymc.io/t/inf-average-loss-advi-in-correlated-topic-model/1563/7 "2018-07-24T19:19:46Z")

</div>

> [@gaddamanil16](#):
>
> eta -inf

This is not right - the reason is that your cov is not a positive definite matrix.

---

<div class="post-metadata">

**Author:** ![gaddamanil16](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/gaddamanil16/32/927_2.png) [@gaddamanil16](https://discourse.pymc.io/u/gaddamanil16)\
**Post date:** [July 24, 2018, 8:18pm UTC](https://discourse.pymc.io/t/inf-average-loss-advi-in-correlated-topic-model/1563/8 "2018-07-24T20:18:49Z")

</div>

In the documentation provided, its not mentioned about the positive definiteness of the covariance matrix?

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [July 24, 2018, 8:48pm UTC](https://discourse.pymc.io/t/inf-average-loss-advi-in-correlated-topic-model/1563/9 "2018-07-24T20:48:34Z")

</div>

I guess the document is not explicit enough on that but the logp of MvNormal (or MvStudentT) only defined on covariance matrix that are positive definiteness.

---

<div class="post-metadata">

**Author:** ![gaddamanil16](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/gaddamanil16/32/927_2.png) [@gaddamanil16](https://discourse.pymc.io/u/gaddamanil16)\
**Post date:** [July 25, 2018, 4:05pm UTC](https://discourse.pymc.io/t/inf-average-loss-advi-in-correlated-topic-model/1563/10 "2018-07-25T16:05:58Z")

</div>

Okay! Thanks. 🙂  
Also do you know any implementation of Stochastic Mixed membership Block Models in pymc3. I want to implement it using ADVI.
