How to solve pytorch cuda problem?

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

username: albanD
cooked: <p>Hi,</p>
<p>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.</p>

username: thaingi
cooked: <p>Thanks!output is cuda True<br>
target is cuda True.Both inputs are cuda.With this statement output.is_cuda and target.is_cuda.Please how can I solve.</p>

username: albanD
cooked: <p>This is weird indeed.<br>
Do you have a small code sample that reproduces this? Most likely a CPU Tensor somewhere <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

username: chetan06
cooked: <p>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</p>

username: thaingi
cooked: <p>Thanks a lot!The code is<br>
def train_epoch(model, loader, optimizer, epoch, n_epochs, print_freq=1):<br>
batch_time = AverageMeter()<br>
losses = AverageMeter()<br>
error = AverageMeter()</p>
<pre><code># 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.</code></pre>

username: chetan06
cooked: <p>Have you pushed model to gpu?</p>

username: thaingi
cooked: <p>yes.model=model.cuda()<br>
<span class="hashtag">#print</span>(‘modelis cuda’,model.is_cuda)<br>
output = model(input)#</p>

username: chetan06
cooked: <p>Use:<br>
criterion = nn.CrossEntropyLoss() # outside the train function</p>
<p>loss = criterion(output, target) <span class="hashtag">#inside</span> train function</p>

username: thaingi
cooked: <aside class="quote no-group" data-username="chetan06" data-post="10" data-topic="80053">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/chetan06/48/45417_2.png" class="avatar"> chetan06:</div>
<blockquote>
<p>criterion = nn.CrossEntropyLoss()</p>
</blockquote>
</aside>
<p>Thanks!But the error shows:<strong>RuntimeError: Expected object of device type cuda but got device type cpu for argument <span class="hashtag-raw">#1</span> ‘self’ in call to _thnn_nll_loss_forward</strong></p>

username: thaingi
cooked: <p>sorry.I am trying again.</p>

username: thaingi
cooked: <p>ouside the train is where.Inside train,<strong>NameError: name ‘criterion’ is not defined</strong> shows.Thanks.</p>

username: chetan06
cooked: <p>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</p>

username: twpann
cooked: <p>Thanks!But error exists:in train_epoch<br>
loss = criterion(output, target)</p>