How to solve pytorch cuda problem?

I want to solve the problem of pytorch program about cuda.The error is as follows:
Traceback (most recent call last):
File “C:\Users\hp\Downloads\pytorch-master\train.py”, line 371, in
fire.Fire(train)
File “C:\Users\hp\Anaconda3\envs\tf-gpu\lib\site-packages\fire\core.py”, line 138, in Fire
component_trace = _Fire(component, args, parsed_flag_args, context, name)
File “C:\Users\hp\Anaconda3\envs\tf-gpu\lib\site-packages\fire\core.py”, line 468, in _Fire
target=component.name)
File “C:\Users\hp\Anaconda3\envs\tf-gpu\lib\site-packages\fire\core.py”, line 672, in _CallAndUpdateTrace
component = fn(*varargs, **kwargs)
File “C:\Users\hp\Downloads\pytorch-master\train.py”, line 349, in train
n_epochs=n_epochs, batch_size=batch_size, seed=seed)
File “C:\Users\hp\Downloads\pytorch-master\train.py”, line 226, in trainmodel
n_epochs=n_epochs,
File “C:\Users\hp\Downloads\pytorch-master\train.py”, line 108, in trainmodel_epoch
loss = torch.nn.functional.cross_entropy(output, target)
File “C:\Users\hp\Anaconda3\envs\tf-gpu\lib\site-packages\torch\nn\functional.py”, line 2324, in cross_entropy
return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
File “C:\Users\hp\Anaconda3\envs\tf-gpu\lib\site-packages\torch\nn\functional.py”, line 2115, in nll_loss
ret = torch._C._nn.nll_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
RuntimeError: Expected object of device type cuda but got device type cpu for argument #1 ‘self’ in call to _thnn_nll_loss_forward
Please answer.Thanks in advance!

Hi,

As mentioned in the error, one of the inputs to your cross_entropy is a CPU Tensor while the other is a GPU Tensor. You should make this consistent and make sure both are on the same device.

Thanks!output is cuda True
target is cuda True.Both inputs are cuda.With this statement output.is_cuda and target.is_cuda.Please how can I solve.

This is weird indeed.
Do you have a small code sample that reproduces this? Most likely a CPU Tensor somewhere :slight_smile:

Please share the code there may be some part of the input that is not in gpu and is used for computation. This could be the only possible reason as much I know

Thanks a lot!The code is
def train_epoch(model, loader, optimizer, epoch, n_epochs, print_freq=1):
batch_time = AverageMeter()
losses = AverageMeter()
error = AverageMeter()

# Model on train mode
model.train()

end = time.time()
input = []
target = []

for batch_idx,(input, target) in enumerate(loader):
    # Create vaiables
    if torch.cuda.is_available():
        input=input.cuda()
        target = target.cuda()
       output = model(input)
      **loss = torch.nn.functional.cross_entropy(output, target)**  This line is error.I want to know where is _thnn_nll_loss_forward.Thanks in advance.

Have you pushed model to gpu?

yes.model=model.cuda()
#print(‘modelis cuda’,model.is_cuda)
output = model(input)#

Use:
criterion = nn.CrossEntropyLoss() # outside the train function

loss = criterion(output, target) #inside train function

Thanks!But the error shows:RuntimeError: Expected object of device type cuda but got device type cpu for argument #1 ‘self’ in call to _thnn_nll_loss_forward

sorry.I am trying again.

ouside the train is where.Inside train,NameError: name ‘criterion’ is not defined shows.Thanks.

Actually I meant to say that you must I have created a function to train your model. So outside that function create a object of class nn.CrossEntropyLoss and inside the function use the same object to calculate the loss

Thanks!But error exists:in train_epoch
loss = criterion(output, target)