Created
September 12, 2017 17:05
-
-
Save ikhlestov/c20fb43740ed34f08c7640a206d1323f to your computer and use it in GitHub Desktop.
pytorch: train model on cuda
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch | |
### tensor example | |
x_cpu = torch.randn(10, 20) | |
w_cpu = torch.randn(20, 10) | |
# direct transfer to the GPU | |
x_gpu = x_cpu.cuda() | |
w_gpu = w_cpu.cuda() | |
result_gpu = x_gpu @ w_gpu | |
# get back from GPU to CPU | |
result_cpu = result_gpu.cpu() | |
### model example | |
model = model.cuda() | |
# train step | |
inputs = Variable(inputs.cuda()) | |
outputs = model(inputs) | |
# get back from GPU to CPU | |
outputs = outputs.cpu() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment