# Train scVI on a sampled dataset

**URL:** <https://discourse.scverse.org/t/train-scvi-on-a-sampled-dataset/3302>\
**Category:** scvi-tools\
**Created:** [October 17, 2024, 2:54am UTC](https://discourse.scverse.org/t/train-scvi-on-a-sampled-dataset/3302 "2024-10-17T02:54:58Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![abs51295](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/abs51295/32/994_2.png) [@abs51295](https://discourse.scverse.org/u/abs51295)\
**Post date:** [October 17, 2024, 2:54am UTC](https://discourse.scverse.org/t/train-scvi-on-a-sampled-dataset/3302/1 "2024-10-17T02:54:58Z")

</div>

Hey,

We recently started using Leverage score sampling from Seurat that samples large datasets in a way that preserves rare cell types. Assuming I sampled 10% of cells from every scVI batch that I have and trained an scVI model using it, how do I project the remaining 90% of the cells? Since the scVI model has already learned “batch effects” from my sampled data, is it possible to do a forward pass of the trained model to get latent space for the remaining 90% of cells per batch?

Thanks

---

<div class="post-metadata">

**Author:** ![cane11](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/cane11/32/241_2.png) [@cane11](https://discourse.scverse.org/u/cane11)\
**Post date:** [October 17, 2024, 3:31am UTC](https://discourse.scverse.org/t/train-scvi-on-a-sampled-dataset/3302/2 "2024-10-17T03:31:52Z")

</div>

We support now passing train and validation indices. We would be very excited to see some benchmarking with passing stratified indices and model performance. I would assume one could speed up training quite significantly. You can enable it by defining validation\_indices to datasplitter\_kwargs in model.train.  
Please update about the results as we might be interested to highlight it as a tutorial.

---

<div class="post-metadata">

**Author:** ![abs51295](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/abs51295/32/994_2.png) [@abs51295](https://discourse.scverse.org/u/abs51295)\
**Post date:** [October 17, 2024, 4:07am UTC](https://discourse.scverse.org/t/train-scvi-on-a-sampled-dataset/3302/3 "2024-10-17T04:07:05Z")

</div>

So, just to be clear I understand, we are passing 10% of the sampled data for training and the remaining as a validation set?

---

<div class="post-metadata">

**Author:** ![cane11](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/cane11/32/241_2.png) [@cane11](https://discourse.scverse.org/u/cane11)\
**Post date:** [October 17, 2024, 4:19am UTC](https://discourse.scverse.org/t/train-scvi-on-a-sampled-dataset/3302/4 "2024-10-17T04:19:40Z")

</div>

You can also add cells to the test\_set. The validation set is evaluated once per epoch. Adding them to the test\_set means they are not used during the full training run. The second one would be more efficient. I would still keep 10% validation cells - maybe using the same splitting technique.

---

<div class="post-metadata">

**Author:** ![abs51295](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/abs51295/32/994_2.png) [@abs51295](https://discourse.scverse.org/u/abs51295)\
**Post date:** [October 17, 2024, 5:27am UTC](https://discourse.scverse.org/t/train-scvi-on-a-sampled-dataset/3302/5 "2024-10-17T05:27:07Z")

</div>

Okay, I would try with 10% training, 10% validation and 80% test. I am curious though is it the same as [Train + Prediction Split](https://discourse.scverse.org/t/train-prediction-split/338) ?

---

<div class="post-metadata">

**Author:** ![cane11](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/cane11/32/241_2.png) [@cane11](https://discourse.scverse.org/u/cane11)\
**Post date:** [October 17, 2024, 6:02am UTC](https://discourse.scverse.org/t/train-scvi-on-a-sampled-dataset/3302/6 "2024-10-17T06:02:54Z")

</div>

Yes, until recently the split was always random. Now you can provide a custom split to the dataloader.

---

<div class="post-metadata">

**Author:** ![abs51295](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/abs51295/32/994_2.png) [@abs51295](https://discourse.scverse.org/u/abs51295)\
**Post date:** [December 5, 2024, 6:55pm UTC](https://discourse.scverse.org/t/train-scvi-on-a-sampled-dataset/3302/7 "2024-12-05T18:55:50Z")

</div>

Hey Cane,

Passing stratified indices worked out pretty well. I had a huge dataset of 15M cells and a normal scVI run with fp16 precision and batch size of 2048 would take ~9 hours. I sketched the dataset to downsample while preserving the manifold and had that as an input to the train and validation set. Specifically, I downsampled to 20% of data which is 3M cells and split them half way across train and validation. Remaining 80% were passed as test. The model then took ~3 hours to converge. I compared the UMAPs of both the runs and they looked very similar to me.

 ![image](https://canada1.discourse-cdn.com/flex035/uploads/forum11/original/2X/3/3e18dd68559ae1cd9ed5728a7f0eb8951c81df70.png)

---

<div class="post-metadata">

**Author:** ![cane11](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/cane11/32/241_2.png) [@cane11](https://discourse.scverse.org/u/cane11)\
**Post date:** [December 5, 2024, 7:10pm UTC](https://discourse.scverse.org/t/train-scvi-on-a-sampled-dataset/3302/8 "2024-12-05T19:10:12Z")

</div>

Nice. Do you subset to the same cells for UMAP? Could you share losses? Plotting them would be sufficient. Scib-metrics would also be great. Though looking at UMAPs can be misleading, it gives me the impression that the second one actually looks better (no gradient from blue to orange cells etc) - not sure whether this is expected though.

---

<div class="post-metadata">

**Author:** ![abs51295](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/abs51295/32/994_2.png) [@abs51295](https://discourse.scverse.org/u/abs51295)\
**Post date:** [December 6, 2024, 12:13am UTC](https://discourse.scverse.org/t/train-scvi-on-a-sampled-dataset/3302/9 "2024-12-06T00:13:18Z")

</div>

Yes, the UMAP is on all cells. My understanding is that when you pass the data as test it’s never used in training but just passed through the model to get the latent space at the end when using `model.get_latent_representation()`. Here are the loss plots:

 ![image](https://canada1.discourse-cdn.com/flex035/uploads/forum11/original/2X/6/63743bc104425a18e7d20cc75fa3386095f2e7ba.png)  
 ![image](https://canada1.discourse-cdn.com/flex035/uploads/forum11/original/2X/5/5238c7121fa2549650eb3616b200fb987acc55b9.png)
