theislab / scarches

Reference mapping for single-cell genomics
https://docs.scarches.org/en/latest/
BSD 3-Clause "New" or "Revised" License
326 stars 51 forks source link

"AttributeError: 'float' object has no attribute 'cpu'" #36

Closed ilyakorsunsky closed 3 years ago

ilyakorsunsky commented 3 years ago

Dear Authors,

I am running scArches 0.3.0 with trvae. I run into a problem (full error message below) with my datasets when I try to either build a reference or map a query when either the source or target has only one batch variable value. Fortunately, when I add a second batch variable value to the source, trvae works fine, and to the target, scArches works fine. Unfortunately, I could not reproduce this error by downsampling batches in the notebooks you provided. I found the same main error message in a separate github but it was not clear if this applied to .

Thank you in advance for your help!


AttributeError Traceback (most recent call last)

in () 10 n_epochs=trvae_epochs, 11 alpha_epoch_anneal=trvae_epochs, ---> 12 early_stopping_kwargs=early_stopping_kwargs 13 )

/PHShome/ik936/anaconda3/envs/scarches3/lib/python3.6/site-packages/scarches/models/trvae/trvae_model.py in train(self, n_epochs, lr, eps, kwargs) 280 condition_key=self.conditionkey, 281 kwargs) --> 282 self.trainer.train(n_epochs, lr, eps) 283 self.istrained = True 284

/PHShome/ik936/anaconda3/envs/scarches3/lib/python3.6/site-packages/scarches/trainers/trvae/trainer.py in train(self, n_epochs, lr, eps) 168 169 # Validation of Model, Monitoring, Early Stopping --> 170 self.on_epoch_end() 171 if self.use_early_stopping: 172 if not self.check_early_stop():

/PHShome/ik936/anaconda3/envs/scarches3/lib/python3.6/site-packages/scarches/trainers/trvae/trainer.py in on_epochend(self) 266 if "loss" in key: 267 self.logs["epoch" + key].append( --> 268 sum(self.iter_logs[key][:]).cpu().detach().numpy() / len(self.iter_logs[key][:])) 269 270 # Validate Model

AttributeError: 'float' object has no attribute 'cpu'

M0hammadL commented 3 years ago

Hi @ilyakorsunsky. thanks for reporting this, By one batch variable you mean when you have only 1 dataset (batch) in either query or reference? right?

ilyakorsunsky commented 3 years ago

Hi @M0hammadL, that's exactly right!

Cottoneyejoe95 commented 3 years ago

Hi @ilyakorsunsky, could you also provide some more info on what losses you are using? Or how you initalize the model? That would help maybe.

Cottoneyejoe95 commented 3 years ago

Another first possible solution would be if you could uninstall scarches and then reinstall in the following way:

pip uninstall scArches pip install git+https://github.com/theislab/scarches.git

Since I fixed a error related to the number of batches a few days ago, maybe you dont have the most recent state.

ilyakorsunsky commented 3 years ago

Hi @Cottoneyejoe95, I re-installed the most recent version but now unfortunately the kernel crashes, without error messages, each time I run the train function. I included some relevant code chunks with model parameters below:

early_stopping_kwargs = { "early_stopping_metric": "val_unweighted_loss", "threshold": 0, "patience": 20, "reduce_lr": True, "lr_patience": 13, "lr_factor": 0.1, }

trvae = sca.models.TRVAE( adata=source_adata, condition_key=condition_key, conditions=source_conditions, recon_loss='mse', hidden_layer_sizes=[128, 128], )
trvae.train( n_epochs=trvae_epochs, alpha_epoch_anneal=trvae_epochs, early_stopping_kwargs=early_stopping_kwargs )

new_trvae = trvae.load_query_data(adata=target_adata, reference_model=trvae) new_trvae.train( n_epochs=surgery_epochs, alpha_epoch_anneal=trvae_epochs, early_stopping_kwargs=early_stopping_kwargs, weight_decay=0 )

Cottoneyejoe95 commented 3 years ago

I also updated official version to 0.3.1 so you should be able to install normally again.

Cottoneyejoe95 commented 3 years ago

Are you using jupyter notebooks? Could you try to run it without notebook? And see if it still crashes, maybe with error message? Is it crashing at first train function? Or at second (query learning)?

Cottoneyejoe95 commented 3 years ago

I also double checked in a jupyter notebook with Pancreas dataset (Same as in tutorial) with only one batch as reference and exactly same parameters as you mentioned above and everything runs for me. Please let me know if still doesnt run for you with updated version.

bsierieb1 commented 3 years ago

just like @ilyakorsunsky, i also had 1 data set in the reference and was getting the AttributeError: 'float' object has no attribute 'cpu' with v. 0.3.0. upgrading to v. 0.3.1 following the advice of @Cottoneyejoe95 solved the issue. thanks!

M0hammadL commented 3 years ago

@ilyakorsunsky please use other models, trVAE is our slowest models among all models. Therefore, we suggest using other methods if possible which are faster and most of the time better. (our suggestions-> scANVI->scVI->trVAE for users)

M0hammadL commented 3 years ago

@ilyakorsunsky @bsierieb1 Here is our suggestion :

I have also updated the main readthedocs page with the following suggestions. let me know if you have any other questions.

Which model to choose?

ilyakorsunsky commented 3 years ago

Thanks for the great advice! The kernel crashing was an internal issue that I resolved. Both trvae and scarches now work fine with one dataset in the reference and query.