# Model Not Recovering Parameters after Minibatching

**URL:** <https://discourse.pymc.io/t/model-not-recovering-parameters-after-minibatching/15047>\
**Category:** v5\
**Tags:** modeling\
**Created:** [September 8, 2024, 8:27pm UTC](https://discourse.pymc.io/t/model-not-recovering-parameters-after-minibatching/15047 "2024-09-08T20:27:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kylekatzen](https://avatars.discourse-cdn.com/v4/letter/k/7c8e57/32.png) [@kylekatzen](https://discourse.pymc.io/u/kylekatzen)\
**Post date:** [September 8, 2024, 8:27pm UTC](https://discourse.pymc.io/t/model-not-recovering-parameters-after-minibatching/15047/1 "2024-09-08T20:27:15Z")

</div>

I’m trying to understand why minibatching isn’t working for my problem. I have some clicks and transactions for each entity. I’m using a Binomial likelihood distribution to model the conversion rate (transactions / clicks). When I don’t use minibatching (or NUTS sampling) I get results pretty close to the generated conversion rates, but when I use batching I get results for each entity that are right around the mean conversion rate.

One thing that I thought might solve the problem, even if I didn’t think it would speed up the fitting, was using a batch size of 1. For reasons that I don’t understand, that was actually significantly worse, and it appeared the model didn’t learn anything from the data and just used the prior mean of 0. As it trained the loss only seemed to go up. Obviously there is something that I’m completely misunderstanding about minibatching.

This is how I’m generating the data

> import numpy as np  
> def sigmoid(x):  
> return 1 / (1 + np.exp(-x))  
> n\_ent = 1000  
> entity\_ids = np.arange(n\_ent)  
> cvrs = sigmoid(np.random.normal(0, 1, size=(n\_ent)) - 4)  
> clicks = np.round(np.random.lognormal(7, 1, size = n\_ent)).astype(int)  
> transactions = np.random.binomial(clicks, cvrs)  
> X = pd.DataFrame({“entity\_id”: entity\_ids, “cvrs”: cvrs, “clicks”: clicks, “transactions”: transactions})

This is the model I’m using:

> coords = {“entity\_ids”: entity\_ids}  
> with pm.Model(coords = coords) as model:
> 
> ```
> clicks = pm.Data("clicks", X.clicks.astype(np.int32))
> transactions = pm.Data("transactions", X.transactions.astype(np.int32))
> entity_id = pm.Data("entity_id", X.entity_id.astype(np.int32))
> 
> mu = pm.Normal("beta", mu = 0, sigma = 10, dims = "entity_ids")
> cvr = pm.Deterministic("cvr", pm.math.invlogit(mu[entity_id]))
> y_hat = pm.Binomial("y_hat", p = cvr, n = clicks, observed = transactions)
> 
> ```

This is the optimization code that works

> with model:
> 
> ```
> advi = pm.ADVI()
> approx = advi.fit(100000,
> callbacks=[pm.variational.callbacks.CheckParametersConvergence(tolerance = .01)])
> 
> trace = approx.sample(1000)
> 
> ```

And this is the code that doesn’t

> with model:  
> minibatch\_data\_clicks, minibatch\_data\_transactions, minibatch\_data\_entity\_id = pm.Minibatch(X.clicks.astype(np.int32), X.transactions.astype(np.int32), X.entity\_id.astype(np.int32), batch\_size = 128)
> 
> ```
> advi = pm.ADVI()
> approx = advi.fit(100000,more_replacements = {model.clicks: minibatch_data_clicks, model.transactions: minibatch_data_transactions, model.entity_id: minibatch_data_entity_id},
> callbacks=[pm.variational.callbacks.CheckParametersConvergence(tolerance = .01)])
> 
> trace = approx.sample(1000)
> 
> ```

---

<div class="post-metadata">

**Author:** ![ricardoV94](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ricardov94/32/5775_2.png) [@ricardoV94](https://discourse.pymc.io/u/ricardoV94)\
**Post date:** [September 8, 2024, 9:46pm UTC](https://discourse.pymc.io/t/model-not-recovering-parameters-after-minibatching/15047/2 "2024-09-08T21:46:36Z")

</div>

Do you get any warnings when fit starts?

---

<div class="post-metadata">

**Author:** ![kylekatzen](https://avatars.discourse-cdn.com/v4/letter/k/7c8e57/32.png) [@kylekatzen](https://discourse.pymc.io/u/kylekatzen)\
**Post date:** [September 8, 2024, 11:01pm UTC](https://discourse.pymc.io/t/model-not-recovering-parameters-after-minibatching/15047/3 "2024-09-08T23:01:16Z")

</div>

Thank you for your prompt response! There are no warnings.

---

<div class="post-metadata">

**Author:** ![kylekatzen](https://avatars.discourse-cdn.com/v4/letter/k/7c8e57/32.png) [@kylekatzen](https://discourse.pymc.io/u/kylekatzen)\
**Post date:** [September 9, 2024, 5:11pm UTC](https://discourse.pymc.io/t/model-not-recovering-parameters-after-minibatching/15047/4 "2024-09-09T17:11:15Z")

</div>

I think I may have found the issue. I didn’t use the total\_size parameter in my likelihood function. When I include that, I get results much closer to what I would expect.
