Changes

Jump to: navigation, search

DPS921/PyTorch: Convolutional Neural Networks

351 bytes removed, 14:17, 30 November 2020
Parallelization Methods
Here is a toy model that contains two linear layers. Each linear layer is designed to run a separate GPU.
import torch import torch.nn as nn import torch.optim as optim
The code is very similar to a single GPU implementation, except for the ''.to('cuda:x')'' calls, where ''cuda:0'' and ''cuda:1'' are each their own GPU.
<code>model = ToyModel()</code>
<code>loss_fn = nn.MSELoss()</code>
<code>optimizer = optim.SGD(model.parameters(), lr=0.001)</code>
 
<code>optimizer.zero_grad()</code>
<code>outputs = model(torch.randn(20, 10))</code>
<code>labels = torch.randn(20, 5).to('cuda:1')</code>
<code>loss_fn(outputs, labels).backward()</code>
<code>optimizer.step()</code>
model = ToyModel()
loss_fn(outputs, labels).backward()
optimizer.step()
 
The backward() and torch.optim will automatically take care of gradients as if the model is on one GPU. You only need to make sure that the labels are on the same device as the outputs when calling the loss function.
56
edits

Navigation menu