nsf-paper icon indicating copy to clipboard operation
nsf-paper copied to clipboard

NumericalDivergenceError during NSF model training

Open ssohuiKim opened this issue 9 months ago • 3 comments

I am encountering a NumericalDivergenceError when training the NSF model. The error occurs during training (using tro.train_model(*Dtf, status_freq=50, verbose=True)) even after applying various pre-processing steps. I am using raw count data (with size factors computed via preprocessing function's "sz= scanpy" option) and filtering cells based on total_counts > 3500, as well as additional filtering with sc.pp.filter_cells(adata, min_genes=200) and sc.pp.filter_genes(adata, min_cells=3). Despite these steps, the error persists.

Image

The steps I did.

  1. filter the anndata object
adata_filtered = adata[adata.obs.total_counts > 3500, :].copy()
sc.pp.filter_cells(adata_filtered, min_genes=200)
sc.pp.filter_genes(adata_filtered, min_cells=3)
  1. Initialize the model using raw counts and computed size factor
sz = adata_filtered.obs["total_counts"].to_numpy().reshape(-1, 1)
sz[sz < 1e-5] = 1e-5
fit = sf.SpatialFactorization(J=adata_filtered.shape[1], L=L, Z=Z, psd_kernel=ker, nonneg=True, lik="poi")
fit.init_loadings(adata_filtered.layers["counts"], X=adata_filtered.obsm["spatial"], sz=sz, shrinkage=0.1)
  1. Train the model
tro = training.ModelTrainer(fit)
tf.debugging.set_log_device_placement(True)
tro.train_model(*Dtf, status_freq=50, verbose=True)

ssohuiKim avatar Mar 26 '25 01:03 ssohuiKim

Try using the NSFH model instead, it is more numerically stable than NSF.

willtownes avatar Mar 27 '25 16:03 willtownes

Is there any tutorial code for NSFH?? Anyway Thanks for your answer 😸

ssohuiKim avatar Mar 28 '25 05:03 ssohuiKim

I don't really have a proper tutorial but you can follow along with some of these notebooks: https://github.com/willtownes/nsf-paper/tree/main/scrna/visium_brain_sagittal

willtownes avatar Apr 17 '25 20:04 willtownes