AttributeError: 'torch.device' object has no attribute '_apply'

This is my code:

class my_model(nn.Module):
         def __init__(self,**kwargs):
                  super(my_model,self).__init__(**kwargs)
                  self.model=torchvision.models.resnet34
         
         def forward(self,x):
                 print(x)
                out=self.model(x)
                return out


learn=Learner(data,my_model,metrics=error_rate)

learn.fit_one_cycle(4)

I want to try to put a custom model in the learn class for training, but I get this error:

Traceback (most recent call last):
File “D:\Anaconda3\envs\pytorch-gpu\lib\site-packages\IPython\core\interactiveshell.py”, line 3326, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File “”, line 27, in
learn=Learner(data,my_model,metrics=error_rate)
File “”, line 18, in init
File “D:\Anaconda3\envs\pytorch-gpu\lib\site-packages\fastai\basic_train.py”, line 165, in post_init
self.model = self.model.to(self.data.device)
File “D:\Anaconda3\envs\pytorch-gpu\lib\site-packages\torch\nn\modules\module.py”, line 426, in to
return self._apply(convert)
AttributeError: ‘torch.device’ object has no attribute ‘_apply’

I see in the forum that someone seems to have this error, but he seems to correct it by updating Kaggle’s notebook, but I run this in pycharm

I think you need to initialize your model object first in my_model. Specifically, this:
self.model = torchvision.models.resnet34(pretrained=True)
This should ideally fix the error you posted.
Hope this helps!