Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7939159

Browse files
heloa-netSvetlana Karslioglu
andauthored
Fix #1983 by moving input and model to device (#2249)
Fixes #1983 Moved input to device on line `234` instead of `232` for readability. `y` on line `232` can't be moved to device due to being an `int` I've found out about this issue while following the tutorial from Quickstart and reaching "Optimization model parameters". I was iterating on the code started on Quickstart and the model had been sent to the device. [Optimization tutorial] (https://github.com/pytorch/tutorials/blob/main/beginner_source/basics/optimization_tutorial.py) doesn't move the model or input to the device and this is when I got the error described in #1983. In light of this, should optimization_tutorial.py also be updated? Test plan: * Tested in Colab using GPU, [link](https://colab.research.google.com/drive/1X7BJHd42FN9N_8DiXd7h4ScplN2-OmLo?usp=sharing) Co-authored-by: Svetlana Karslioglu <[email protected]>
1 parent 7a9632a commit 7939159

1 file changed

Lines changed: 2 additions & 1 deletion

File tree

beginner_source/basics/quickstart_tutorial.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ def test(dataloader, model, loss_fn):
215215
# The process for loading a model includes re-creating the model structure and loading
216216
# the state dictionary into it.
217217

218-
model = NeuralNetwork()
218+
model = NeuralNetwork().to(device)
219219
model.load_state_dict(torch.load("model.pth"))
220220

221221
#############################################################
@@ -237,6 +237,7 @@ def test(dataloader, model, loss_fn):
237237
model.eval()
238238
x, y = test_data[0][0], test_data[0][1]
239239
with torch.no_grad():
240+
x = x.to(device)
240241
pred = model(x)
241242
predicted, actual = classes[pred[0].argmax(0)], classes[y]
242243
print(f'Predicted: "{predicted}", Actual: "{actual}"')

0 commit comments

Comments
 (0)