DestVI Tensor Nan Error

Hi,

I’m redirected from Tensor Nan Error · Issue #65 · scverse/scvi-tutorials · GitHub

I’m following DestVI_tutorial.ipynb to run the training in order to get the cell type composition matrix

My input singlecell data:
AnnData object with n_obs × n_vars = 1691 × 19972
obs: ‘cell_types’

print(np.max(sc_adata.X))
print(np.min(sc_adata.X))
print(np.all(np.isfinite(sc_adata.X)))
print(np.min(sc_adata.X.sum(1)))

10738.0
0.0
True
2508.0

sc_adata.obs.cell_types:

0        Inhibitory
1        Inhibitory
2        Inhibitory
3        Inhibitory
4        Inhibitory
           ...     
1686    Endothelial
1687    Endothelial
1688    Endothelial
1689    Endothelial
1690    Endothelial
Name: cell_types, Length: 1691, dtype: category
Categories (6, object): ['Astrocyte', 'Endothelial', 'Excitatory', 'Inhibitory', 'Microglia', 'OD']

My input ST data:
AnnData object with n_obs × n_vars = 3067 × 135
obsm: ‘location’

print(np.max(st_adata.X))
print(np.min(st_adata.X))
print(np.all(np.isfinite(st_adata.X)))
print(np.min(st_adata.X.sum(1)))

529.64
0.0
True
7.1822705

print(np.max(st_adata.obsm[‘spatial’]))
print(np.min(st_adata.obsm[‘spatial’]))
print(np.all(np.isfinite(st_adata.obsm[‘spatial’])))
print(np.min(st_adata.obsm[‘spatial’].sum(1)))

4405.402956
-4349.457843
True
-6999.981796

However, when training the sc model, the following error occurred:

ValueError: Expected parameter loc (Tensor of shape (128, 5)) of distribution Normal(loc: torch.Size([128, 5]), scale: torch.Size([128, 5])) to satisfy the constraint Real(), but found invalid values:
tensor([[nan, nan, nan, nan, nan],
        nan, nan, nan, nan, nan],
        [nan, nan, nan, nan, nan]], device='cuda:0', grad_fn=<AddmmBackward0>)

Have you faced with similar error before? Hoping that you can help me, thank you so much!

As Can mentioned in your github issue, it looks like you are feeding log normalized spatial transcriptomics data into the model. The model expects raw counts which is why you are getting nans in the weights after training.

Thank you for the prompt response. I’ve tried feeding the raw ST data(as integers) into the DestVI model, but it still doesn’t work.

I’ve uploaded my dataset in h5ad format as well as the jupyter notebook script that I’m working on in this link (ready to run):

https://drive.google.com/drive/folders/1u0tJUsZj3bmbn3MNoywoRBLYboaW15gD?usp=sharing

Hope that you can help me, thank you!

:grinning:

I took a quick look at your notebook, and noticed that neither of the data sources have var_names which is problematic when you take the intersection of the var_names after filtering down to HVGs. This left the count matrix with 70 genes. This might be the issue, potentially with exploding gradients.

Hi, Thank you very much for your kind assistance!

Regarding the DestVI nan errors, I’ve made an effort to add the var_names into the scRNA data and the ST data. However, the problem still exists.One researcher suggests a smaller learning_rate. While a smaller learning rate doesn’t solve the problem.

The jupyter notebook and the dataset in h5ad format are lately updated in the following link.

https://drive.google.com/drive/folders/1u0tJUsZj3bmbn3MNoywoRBLYboaW15gD?usp=sharing

Hope that you can help me, thank you!

There looks to still be an issue with the var names. You can see in the printed summary of the summary that there are only 64 genes remaining after taking the intersection.

Hello, I am having the same issue, with 336 n_vars, are there any potential other causes for this error?