# Using VI & minibatches with Bambi

**URL:** <https://discourse.pymc.io/t/using-vi-minibatches-with-bambi/11943>\
**Category:** version agnostic\
**Tags:** bambi\
**Created:** [April 20, 2023, 10:44am UTC](https://discourse.pymc.io/t/using-vi-minibatches-with-bambi/11943 "2023-04-20T10:44:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zweli](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/zweli/32/4089_2.png) [@zweli](https://discourse.pymc.io/u/zweli)\
**Post date:** [April 20, 2023, 10:44am UTC](https://discourse.pymc.io/t/using-vi-minibatches-with-bambi/11943/1 "2023-04-20T10:44:00Z")

</div>

@tcapretto

I have a dataset that looks like this:

![Screenshot 2023-04-20 at 6.34.21 AM](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/2X/e/ea202a741adbc05a6aa909dfba71094b079fde13.png)

I build the model with this formula:

 ![Screenshot 2023-04-20 at 6.34.14 AM](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/2X/d/d3f32bedb47768e9ca5f7b340877758462718cf6.png)

And I call a fit using Variational Inference using this:

fit = model.fit(inference\_method=‘vi’, n\_init=100\_000, obj\_optimizer=pm.adagrad\_window(learning\_rate=1e-2)).sample(1\_000)

Everything works fine (tested it on a much smaller row count) but is slow at 164k observations. I want to use the pm.minibatch function but struggling with how to get it to work.

I thought of doing the model.build() then \>\>\>\> with model.build() as xxx: then setting pm.MutableData but getting stuck.

Any ideas would be appreciated but wanted to pin this here and if I figure out a solution share it.

---

<div class="post-metadata">

**Author:** ![tcapretto](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/tcapretto/32/4902_2.png) [@tcapretto](https://discourse.pymc.io/u/tcapretto)\
**Post date:** [April 20, 2023, 11:36am UTC](https://discourse.pymc.io/t/using-vi-minibatches-with-bambi/11943/2 "2023-04-20T11:36:07Z")

</div>

Hi!

A couple of comments

- Why are you using both `(1|striker)` and `(striker|ballnumber)`? You’re using `striker` both as a group and as a predictor. I guess you’re trying to use nested effects, but I’m afraid this is not the correct way. If you tell me a little more I can help 🙂
- I’m not very familiar with minibatch. But as far as I understand you need to modify how the model is written (not just adding things to it) and that’s not possible.
- If you’re using the default priors as they appear there, I recommend you change to something like Normal(0, 1) or Normal(0, 2). Those standard deviations together with the log-link function (which exponentiates the linear predictor) will make the computation very unstable.

---

<div class="post-metadata">

**Author:** ![zweli](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/zweli/32/4089_2.png) [@zweli](https://discourse.pymc.io/u/zweli)\
**Post date:** [April 20, 2023, 12:09pm UTC](https://discourse.pymc.io/t/using-vi-minibatches-with-bambi/11943/3 "2023-04-20T12:09:10Z")

</div>

Whoops, I meant ‘cumruns ~ 1 + (1|striker) + (1|striker:ballnumber)’ - wich nests the balls faced. This is some cricket (the sport) data that I am playing with to test growth modeling and vi, so I can use it with intensive longitudinal data. I wanted to look at the trajectories of batters of the South African team over a couple of seasons as a test case.

You can check out the notebook and data here:

> **[ruminations/cricket\_bambi\_test at main · zwelitunyiswa/ruminations](https://github.com/zwelitunyiswa/ruminations/tree/main/cricket_bambi_test)**
>
> main/cricket\_bambi\_test

---

<div class="post-metadata">

**Author:** ![tcapretto](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/tcapretto/32/4902_2.png) [@tcapretto](https://discourse.pymc.io/u/tcapretto)\
**Post date:** [April 20, 2023, 12:23pm UTC](https://discourse.pymc.io/t/using-vi-minibatches-with-bambi/11943/4 "2023-04-20T12:23:24Z")

</div>

Looks really nice! I love you use both polars and plotnine, we need more of that in the Python world!

If this is not proprietary, are you interested in contributing this example or a simplified version to our docs? 😃

---

<div class="post-metadata">

**Author:** ![zweli](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/zweli/32/4089_2.png) [@zweli](https://discourse.pymc.io/u/zweli)\
**Post date:** [April 20, 2023, 4:55pm UTC](https://discourse.pymc.io/t/using-vi-minibatches-with-bambi/11943/5 "2023-04-20T16:55:42Z")

</div>

Yes! It’s time I contributed. I will ask for help as it’s my first time. But this cricket example can show longitudinal modeling + VI, which will be fun. Thanks for asking.
