AttributeError: 'int' object has no attribute 'to'

username: Smaranika_Sikdar
cooked: <p>For CNN, multi label classification, getting error while testing the image.</p>
<p>def runmodel(seed):<br>
torch.manual_seed(seed)<br>
random.seed(seed)<br>
np.random.seed(seed)<br>
torch.use_deterministic_algorithms(True)</p>
<p>device = torch.device(‘cpu’ if not torch.cuda.is_available() else ‘cuda’)<br>
model = CNN().to(device)<br>
criterion = nn.CrossEntropyLoss().to(device)<br>
optimizer = optim.Adam(model.parameters(), lr=0.001) #, betas=(0.9,0.999))</p>
<p>for epoch in range(1, 5):<br>
loss_train, acc_train = train(model, device, dataloader_train, optimizer, criterion, epoch)<br>
print(‘Epoch {} Train: Loss: {:.4f}, Accuracy: {:.3f}%\n’.format(<br>
epoch, loss_train, 100. * acc_train))<br>
loss_test, acc_test = test(model, device, dataloader_test, criterion)<br>
print(‘Epoch {} Test : Loss: {:.4f}, Accuracy: {:.3f}%\n’.format(<br>
epoch, loss_test, 100. * acc_test))<br>
return acc_test</p>
<p>def train(model, device, data_loader, optimizer, criterion, epoch):<br>
model.train()<br>
loss_train = 0<br>
num_correct = 0<br>
for batch_idx, (data, target) in enumerate(data_loader):<br>
data, target = data.to(device), target.to(device)<br>
optimizer.zero_grad()<br>
output = model(data)<br>
loss = criterion(output, target)<br>
loss.backward()<br>
optimizer.step()<br>
loss_train += loss.item()<br>
prediction = output.argmax(dim=1)<br>
num_correct += prediction.eq(target).sum().item()<br>
if batch_idx % 50 == 0:<br>
print(‘Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.4f}\tAccuracy: {:.0f}%’.format(<br>
epoch, batch_idx * len(data), len(data_loader.dataset),<br>
100. * batch_idx / len(data_loader), loss_train / (batch_idx + 1),<br>
100. * num_correct / (len(data) * (batch_idx + 1))))<br>
loss_train /= len(data_loader)<br>
accuracy = num_correct / len(data_loader.dataset)<br>
return loss_train, accuracy</p>
<p>def test(model, device, data_loader, criterion):<br>
model.eval()<br>
loss_test = 0<br>
num_correct = 0<br>
with torch.no_grad():<br>
for data, target in data_loader:<br>
data, target = data.to(device), target.to(device)<br>
output = model(data)<br>
loss = criterion(output, target)<br>
loss_test += loss.item()  # sum up batch loss<br>
prediction = output.argmax(dim=1)<br>
num_correct += prediction.eq(target).sum().item()<br>
loss_test /= len(data_loader)<br>
accuracy = num_correct / len(data_loader.dataset)<br>
return loss_test, accuracy</p>
<p>I am getting the error. I am running in collar.</p>

username: srishti-git1110
cooked: <aside class="quote no-group" data-username="Smaranika_Sikdar" data-post="1" data-topic="166873">
<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/smaranika_sikdar/48/55158_2.png" class="avatar"> Smaranika_Sikdar:</div>
<blockquote>
<p>criterion = nn.CrossEntropyLoss().to(device)</p>
</blockquote>
</aside>
<p>From a single skim, this isn’t right. <code>criterion = nn.CrossEntropyLoss()</code> is right - you just need to instantiate the CrossEntropyLoss.</p>
<p>Anyway, please post a minimum executable snippet enclosed within ``` mentioning the exact line that produces the said error.</p>