I found a strange problem when using scVI to remove batch effects. It report a “ValueError: Expected more than 1 value per channel when training, got input size torch.Size([1, 128])” when I deal with totally 21618 cells with 3 batches. However, this problem disappears if I just remove any one cells from the whole dataset. I also tried more cells in one dataset and it works well. However, if I just remove some cells and keep 21618 cells left, the problem comes again. It seems the problem just comes with 21618 cells. I also tried both GPU and CPU and just got the same result. I don’t understand this problem
Hi, sorry you’re running into this issue. This is because the batch normalization layer in scVI requires more than one observation in a mini-batch to work properly, and the specific number of observations in your dataset is leading to the last mini-batch having only one cell.
This can be fixed by changing batch_size
from the default of 128
to something else, or passing in drop_last=True
to the datasplitter.
Thanks for your answer.
Martinkim0
I am new to scvi-tools and have the same problem. Currenly, I am not aware how to use DataSplitter and lost myself in manual. Can you please show an example? I am using very basic model.
scvi.model.SCVI.setup_anndata(adata, layer=“counts”, batch_key=“sample_id”)
? AnnDataManager(adata)
? scvi.dataloaders.DataSplitter(adata)
model = scvi.model.SCVI(adata, n_hidden = 128, n_layers=2, n_latent=30, gene_likelihood=“nb”)
model.train()
Hi, just add datasplitter_kwargs={“drop_last”: True} to model.train. It should skip the last batch. This is only supported in scVI-tools 1.2 and latter.
Thank you, Cane11. I would never guess myself!