# About VAE.\_regular\_inference()

**URL:** <https://discourse.scverse.org/t/about-vae-regular-inference/1012>\
**Category:** scvi-tools\
**Created:** [December 19, 2022, 11:19pm UTC](https://discourse.scverse.org/t/about-vae-regular-inference/1012 "2022-12-19T23:19:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nrclaudio](https://avatars.discourse-cdn.com/v4/letter/n/34f0e0/32.png) [@nrclaudio](https://discourse.scverse.org/u/nrclaudio)\
**Post date:** [December 19, 2022, 11:19pm UTC](https://discourse.scverse.org/t/about-vae-regular-inference/1012/1 "2022-12-19T23:19:29Z")

</div>

Hi all,

I’m having trouble following the data flow in the simplest scVI case. As defined in `BaseModuleClass._generic_forward()`, the model will go first through an inference face (encoder) to then go through a generative process (decoder).

However, what I don’t seem to understand is that in `VAE._regular_inference()`, which I understand is the first step during training, `batch_index` is passed to the different encoders. The same applies to `VAE.generative()` and corresponding decoder(s). `batch_index` is usually obtained by slicing the tensors as `tensor[REGISTRY_FIELDS.BATCH_KEY]`.

- My first question is: what does `batch_index` contain exactly?

- Then my second question would be: why is this variable passed to the different encoders in `VAE._regular_inference()`? I understand why we are passing `x` and `*categorical_input`, but I get lost trying to understand where `batch_index` comes into play in the codebase. Maybe a pointer can be helpful?

Let me know if something is not clear!

---

<div class="post-metadata">

**Author:** ![adamgayoso](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/adamgayoso/32/100_2.png) [@adamgayoso](https://discourse.scverse.org/u/adamgayoso)\
**Post date:** [December 19, 2022, 11:35pm UTC](https://discourse.scverse.org/t/about-vae-regular-inference/1012/2 "2022-12-19T23:35:32Z")

</div>

First, let’s look at `setup_anndata`

> <https://github.com/scverse/scvi-tools/blob/1a389d2cdd054441500ad747171f9a532a29ec32/scvi/model/_scvi.py#L205-L226>

We see that the `batch_key` in .obs will be available via the `REGISTRY_KEYS.BATCH_KEY` key.

Now let’s look at where the data gets processed:

> <https://github.com/scverse/scvi-tools/blob/1a389d2cdd054441500ad747171f9a532a29ec32/scvi/module/_vae.py#L217-L221>

Thus, `batch_index` corresponds to the batch information (for integration purposes, nothing to do with minibatching).

Now let’s look where it’s used in `_regular_inference`:

> <https://github.com/scverse/scvi-tools/blob/1a389d2cdd054441500ad747171f9a532a29ec32/scvi/module/_vae.py#L314>

It’s passed to the `z_encoder`, which was initialized as such:

> <https://github.com/scverse/scvi-tools/blob/1a389d2cdd054441500ad747171f9a532a29ec32/scvi/module/_vae.py#L170-L184>

Notice that `encoder_cat_list` is None by default.

By continuing to follow the code, you’ll find that when this is None, no categorical covariates passed in are actually used.

This is not the case for the decoder, which does use the batch information.

The other categorical inputs are also one-hotted and concatenated, but this depends on the initial n cat list passed in.

Finally, I know this is confusing, and this is legacy code we had to keep in to keep old saved models compatible. We are refactoring some parts of the codebase with jax, and those implementations will be a lot cleaner.

---

<div class="post-metadata">

**Author:** ![adamgayoso](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/adamgayoso/32/100_2.png) [@adamgayoso](https://discourse.scverse.org/u/adamgayoso)\
**Post date:** [December 19, 2022, 11:37pm UTC](https://discourse.scverse.org/t/about-vae-regular-inference/1012/3 "2022-12-19T23:37:03Z")

</div>

We also keep a basic scvi implementation here:

> <https://github.com/scverse/scvi-tools-skeleton/blob/main/mypackage/_mymodule.py>

---

<div class="post-metadata">

**Author:** ![nrclaudio](https://avatars.discourse-cdn.com/v4/letter/n/34f0e0/32.png) [@nrclaudio](https://discourse.scverse.org/u/nrclaudio)\
**Post date:** [December 19, 2022, 11:47pm UTC](https://discourse.scverse.org/t/about-vae-regular-inference/1012/4 "2022-12-19T23:47:23Z")

</div>

Hi Adam,

Thanks for the prompt response.

Maybe not the best example since no batch information is concatenated by default in the encoder, but the same would apply to the decoder.

From what I understand:

In `qz, z = self.z_encoder(encoder_input, batch_index, *categorical_input)` :

- `*categorical_input` will be unpacked into `VAE. __init__ (n_cats_per_cov)`
- `batch_index` will be passed into `VAE. __init__ (n_batch)`, which will default to 0 if no `REGISTRY_KEYS.BATCH_KEY` have been initiated

Again, thanks for the prompt response!
