Skip to content

Instantly share code, notes, and snippets.

@dvgodoy
Created May 5, 2022 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dvgodoy/03e5f69ec906ab385a0d29c860e6c91b to your computer and use it in GitHub Desktop.
Save dvgodoy/03e5f69ec906ab385a0d29c860e6c91b to your computer and use it in GitHub Desktop.
import torch
import torch.nn as nn
from torchviz import make_dot
device = 'cuda'
# sending tensor to device at creation time
a = torch.randn(1, requires_grad=True, dtype=torch.float, device=device)
plot1 = make_dot(a)
# sending tensor to device immediately after creating it
a = torch.randn(1, requires_grad=True, dtype=torch.float).to(device)
plot2 = make_dot(a)
class ManualLinearRegression(nn.Module):
def __init__(self):
super().__init__()
# To make "a" and "b" real parameters of the model, we need to wrap them with nn.Parameter
self.a = nn.Parameter(torch.randn(1, requires_grad=True, dtype=torch.float))
self.b = nn.Parameter(torch.randn(1, requires_grad=True, dtype=torch.float))
def forward(self, x):
# Computes the outputs / predictions
return self.a + self.b * x
# sending model to device
model = ManualLinearRegression().to(device)
plot3 = make_dot(model.a)
# plots n.1 and n.3 will be exactly the same
# plot n.2, on the other hand, is different - the `to` operation created an unwanted computation graph
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment