# Variational inference in brms

**URL:** <https://discourse.mc-stan.org/t/variational-inference-in-brms/20616>\
**Category:** brms\
**Tags:** brms\
**Created:** [February 4, 2021, 4:49pm UTC](https://discourse.mc-stan.org/t/variational-inference-in-brms/20616 "2021-02-04T16:49:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![YidaZhang0628](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/yidazhang0628/32/10105_2.png) [@YidaZhang0628](https://discourse.mc-stan.org/u/YidaZhang0628)\
**Post date:** [February 4, 2021, 4:49pm UTC](https://discourse.mc-stan.org/t/variational-inference-in-brms/20616/1 "2021-02-04T16:49:25Z")

</div>

Hi, I am trying to use variational inference to fit a hierarchical bayes model through brms. The main reason for doing this is to increase the speed (especially during early testing of different models). In the brms document, it says that I can change the “algorithm” parameter from “sampling” to “meanfield” to use variational inference. However, this is all the information I can find online about how to use variational inference through brms. I tried a simple example and received a warning message suggesting me to decrease tol\_rel\_obj. But where can I specify this parameter? More generally, what parameters do I have when using variational inference and how to specify them? Is there a tutorial on how to use variational inference and diagnose the result?

- Operating System:  
Linux

- brms Version:  
brms\_2.14.4

---

<div class="post-metadata">

**Author:** ![storopoli](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/storopoli/32/6455_2.png) [@storopoli](https://discourse.mc-stan.org/u/storopoli)\
**Post date:** [February 4, 2021, 8:22pm UTC](https://discourse.mc-stan.org/t/variational-inference-in-brms/20616/2 "2021-02-04T20:22:04Z")

</div>

`brms::brm()` can accept further parameters that are passed to either `rstan::sampling()` or `rstan::vb()`. You can find more about `tol_rel_obj` and other parameters in the [`rstan::vb()` function help](https://mc-stan.org/rstan/reference/stanmodel-method-vb.html).

---

<div class="post-metadata">

**Author:** ![YidaZhang0628](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/yidazhang0628/32/10105_2.png) [@YidaZhang0628](https://discourse.mc-stan.org/u/YidaZhang0628)\
**Post date:** [February 4, 2021, 11:46pm UTC](https://discourse.mc-stan.org/t/variational-inference-in-brms/20616/3 "2021-02-04T23:46:32Z")

</div>

Thank you for the reply! I am using rstan as backend so I assume that when I use variational inference, I am passing the further parameters to rstan::vb(). Here is an example code showing where I put the additional parameter (tol\_rel\_obj):

test ← brm(  
bf(response ~ 0 + Intercept + X + (1 | group),  
data = data2fit,  
family = zero\_inflated\_negbinomial(),  
algorithm = “meanfield”,  
tol\_rel\_obj = 0.00000001,  
iter = 2000, chains = 4, cores = 4  
)

Is this correct? I tried this on a toy example. When I decrease tol\_rel\_obj, the Pareto k diagnostic value is sometimes decreasing and sometimes increasing. Does this mean that the tol\_rel\_obj is working or not? I thought the Pareto k diagnostic value will keep decreasing when I decrease tol\_rel\_obj.

---

<div class="post-metadata">

**Author:** ![storopoli](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/storopoli/32/6455_2.png) [@storopoli](https://discourse.mc-stan.org/u/storopoli)\
**Post date:** [February 5, 2021, 7:50am UTC](https://discourse.mc-stan.org/t/variational-inference-in-brms/20616/4 "2021-02-05T07:50:12Z")

</div>

@YidaZhang0628 sorry but I cannot help you. I don’t have experience with VI. But I can point you towards the following literature that can help you:

Blei, D. M., Kucukelbir, A., & McAuliffe, J. D. (2016). Variational Inference: A Review for Statisticians. _Journal of the American Statistical Association_ , _112_ (518), 859–877. [https://doi.org/10.1080/01621459.2017.1285773](https://doi.org/10.1080/01621459.2017.1285773)

Kucukelbir, A., Ranganath, R., Gelman, A., & Blei, D. M. (2015). Automatic Variational Inference in Stan. _ArXiv:1506.03431 [Stat]_ . [http://arxiv.org/abs/1506.03431](http://arxiv.org/abs/1506.03431)

Yao, Y., Vehtari, A., Simpson, D., & Gelman, A. (2018). Yes, but Did It Work?: Evaluating Variational Inference. _35th International Conference on Machine Learning, ICML 2018_ . [http://arxiv.org/abs/1802.02538](http://arxiv.org/abs/1802.02538)

Also don’t forget to check the [section 18 of Variational Inference in the Stan Reference Manual](https://mc-stan.org/docs/2_26/reference-manual/vi-algorithms-chapter.html).

---

<div class="post-metadata">

**Author:** ![YidaZhang0628](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/yidazhang0628/32/10105_2.png) [@YidaZhang0628](https://discourse.mc-stan.org/u/YidaZhang0628)\
**Post date:** [February 17, 2021, 2:08pm UTC](https://discourse.mc-stan.org/t/variational-inference-in-brms/20616/5 "2021-02-17T14:08:29Z")

</div>

Thank you! I will take a look at these materials!
