Skip to content

Conversation

jiangzhonglian
Copy link
Contributor

If you don't configure this string of code, you will get an error when you iterate over the update from 4000_checkpoint.tar:

encoder_optimizer.step()  

Error message:

exp_avg.mul_(beta1).add_(1 - beta1, grad)
RuntimeError: expected backend CPU and dtype Float but got backend CUDA and dtype Float

Fix it: pytorch/pytorch#2830

model = Model()
model.load_state_dict(checkpoint['model'])
model.cuda()
optimizer = optim.Adam(model.parameters())
optimizer.load_state_dict(checkpoint['optimizer'])
for state in optimizer.state.values():
    for k, v in state.items():
        if isinstance(v, torch.Tensor):
            state[k] = v.cuda()

If you don't configure this string of code, you will get an error when you iterate over the update from 4000_checkpoint.tar:

```
encoder_optimizer.step()  
```


Error message:

```
exp_avg.mul_(beta1).add_(1 - beta1, grad)
RuntimeError: expected backend CPU and dtype Float but got backend CUDA and dtype Float
```


Fix it: pytorch/pytorch#2830

```
with torch.no_grad():
    correct = 0
    total = 0
    for images, labels in test_loader:
        images = images.to(device)  # missing line from original code
        labels = labels.to(device)  # missing line from original code
        images = images.reshape(-1, 28 * 28)
        out = model(images)
        _, predicted = torch.max(out.data, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum().item()
```
If you don't configure this string of code, you will get an error when you iterate over the update from 4000_checkpoint.tar:

```
encoder_optimizer.step()  
```

Error message:

```
exp_avg.mul_(beta1).add_(1 - beta1, grad)
RuntimeError: expected backend CPU and dtype Float but got backend CUDA and dtype Float
```

Fix it: pytorch/pytorch#2830

```
model = Model()
model.load_state_dict(checkpoint['model'])
model.cuda()
optimizer = optim.Adam(model.parameters())
optimizer.load_state_dict(checkpoint['optimizer'])
for state in optimizer.state.values():
    for k, v in state.items():
        if isinstance(v, torch.Tensor):
            state[k] = v.cuda()
```
@netlify
Copy link

netlify bot commented Jul 25, 2019

Deploy preview for pytorch-tutorials-preview ready!

Built with commit 3e1613d

https://deploy-preview-577--pytorch-tutorials-preview.netlify.com

@netlify
Copy link

netlify bot commented Jul 25, 2019

Deploy preview for pytorch-tutorials-preview ready!

Built with commit a473681

https://deploy-preview-577--pytorch-tutorials-preview.netlify.com

@brianjo brianjo requested review from vincentqb and jspisak July 30, 2019 16:11
Copy link
Contributor

@vincentqb vincentqb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for updating the tutorial -- this looks good to me!

@vincentqb
Copy link
Contributor

@brianjo -- can you merge this?

@brianjo
Copy link
Contributor

brianjo commented Aug 6, 2019

@brianjo -- can you merge this

Sure thing. It might be a while as we need to merge some other fixes first. Thanks!

@brianjo brianjo merged commit 8816366 into pytorch:master Aug 7, 2019
@jiangzhonglian
Copy link
Contributor Author

Cool ! I am so happy to help you a little!
Hahaha, in fact, we also translated Pytorch Chinese Documentation, the pytorch tutorial is the best in-depth tutorial I have learned.

@jspisak
Copy link
Contributor

jspisak commented Aug 7, 2019

Thank you!!! @jiangzhonglian

rodrigo-techera pushed a commit to Experience-Monks/tutorials that referenced this pull request Nov 29, 2021
chatbot_tutorial.py:   Solve the optimizer cuda call problem
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants