Skip to content

Instantly share code, notes, and snippets.

@ikhlestov
Created September 12, 2017 17:05
Show Gist options
  • Save ikhlestov/c20fb43740ed34f08c7640a206d1323f to your computer and use it in GitHub Desktop.
Save ikhlestov/c20fb43740ed34f08c7640a206d1323f to your computer and use it in GitHub Desktop.
pytorch: train model on cuda
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