Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RegressionModel use_gpu parameters not working #392

Open
4 tasks
Angel030331 opened this issue Nov 4, 2024 · 3 comments
Open
4 tasks

RegressionModel use_gpu parameters not working #392

Angel030331 opened this issue Nov 4, 2024 · 3 comments
Labels
question Further information is requested

Comments

@Angel030331
Copy link

Angel030331 commented Nov 4, 2024

Please use the template below to post a question to https://discourse.scverse.org/c/ecosytem/cell2location/.

Problem

the use_gpu parameters cannot be used in NB regression model training
...

Description of the data input and hyperparameters

...

DATA_PATH = '/Users/onkiwong/Desktop/Year_4/sem1/BIOF3001/Group_project/datasets/seqFISH+'
cellcount = os.path.join(DATA_PATH, 'raw_somatosensory_sc_exp.txt')
sp_data = os.path.join(DATA_PATH, 'Out_rect_locations.csv')
celltype = os.path.join(DATA_PATH, 'somatosensory_sc_labels.txt')

...

df_celltype = pd.read_csv(celltype, header=None, sep='\t')
df_celltype.columns = ['celltype']
df_celltype.index = adata_ref.obs.index
adata_ref.obs['Subset'] = df_celltype['celltype']
adata_ref.obs['Method'] = '3GEX'
adata_ref.obs['Sample'] = adata_ref.obs_names
adata_ref.obs['Sample'] = adata_ref.obs['Sample'].apply(lambda x: x[0:4])

from cell2location.utils.filtering import filter_genes
selected = filter_genes(adata_ref, cell_count_cutoff=5, cell_percentage_cutoff2=0.03, nonz_mean_cutoff=1.12)

5, 0.03, 1.12

In our case, a few genes are cut

adata_ref = adata_ref[:, selected].copy()

RegressionModel.setup_anndata(adata=adata_ref, batch_key='Sample', labels_key='Subset')

os.environ["THEANO_FLAGS"] = 'device=cuda,floatX=' + 'float32' + ',force_device=True' + ',dnn.enabled=False'
from cell2location.models import RegressionModel
mod = RegressionModel(adata_ref)

Use all data for training (validation not implemented yet, train_size=1)

mod.train(max_epochs=400, batch_size=None, train_size=1, lr=0.002, use_gpu=True)

plot ELBO loss history during training, removing first 20 epochs from the plot

mod.plot_history(20)
Screenshot 2024-11-04 at 16 51 17

Single cell reference data: number of cells, number of cell types, number of genes

...

Single cell reference data: technology type (e.g. mix of 10X 3' and 5')

...

Spatial data: number of locations numbers, technology type (e.g. Visium, ISS, Nanostring WTA)

...

@Angel030331 Angel030331 added the question Further information is requested label Nov 4, 2024
@danielgchen
Copy link

It looks the "use_gpu" parameter is deprecated in recent package updates, you can use the GPU via the "accelerator" parameter, e.g. mod.train(accelerator='gpu', batch_size=None)! I found that I needed to set the "batch_size" parameter to "None" in the new update order to fully copy the data to GPU or else it is not able to find the data and throws an error like IndexError: Dimension specified as 0 but tensor has no dimensions but might just be on my system.

@Angel030331
Copy link
Author

Angel030331 commented Nov 5, 2024

It looks the "use_gpu" parameter is deprecated in recent package updates, you can use the GPU via the "accelerator" parameter, e.g. mod.train(accelerator='gpu', batch_size=None)! I found that I needed to set the "batch_size" parameter to "None" in the new update order to fully copy the data to GPU or else it is not able to find the data and throws an error like IndexError: Dimension specified as 0 but tensor has no dimensions but might just be on my system.

However, when I try this approach, a ValueError arises instead. Please refer to the attached diagram.
Screenshot 2024-11-05 at 10 34 41
image

@Sawakotx
Copy link

Same issue. I just do not use the "use_gpu" parameter and it can work. Also, before import cell2location, I set this code for GPU(according to the common error of cell2location's documents [https://cell2location.readthedocs.io/en/latest/commonerrors.html].

#this line should go before importing cell2location
os.environ["THEANO_FLAGS"] = 'device=cuda,floatX=float32,force_device=True'
import torch
#used_device = torch.device('cpu')#'cuda:3' if torch.cuda.is_available() else
used_device =torch.device('cuda' if torch.cuda.is_available() else 'cpu')

Looking forward to more advice from cell2location team!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants