Flows demo fails because of missing .to(ptu.device)
#5
Open
Description
The AutoregressiveFlow
and RealNVP
cells fails with the error message copied at the end of the message. I ran all the cells sequentially from the beginning.
If I add real_nvp = real_nvp.to(ptu.device)
, all works fine:
real_nvp = RealNVP([AffineTransform("left", n_hidden=2, hidden_size=64),
AffineTransform("right", n_hidden=2, hidden_size=64),
AffineTransform("left", n_hidden=2, hidden_size=64),
AffineTransform("right", n_hidden=2, hidden_size=64)],
train_loader.dataset, 'moons', train_labels)
real_nvp = real_nvp.to(ptu.device) # <-- ADDED THIS LINE
train_losses, test_losses = train_epochs(real_nvp, train_loader, test_loader, dict(epochs=250, lr=5e-3, epochs_to_plot=[0, 3, 6, 10, 25, 249]))
Error messages:
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-54-8b25cbd7d1cc> in <module>
1 ar_flow = AutoregressiveFlow(train_loader.dataset, 'moons', train_labels)
----> 2 train_losses, test_losses = train_epochs(ar_flow, train_loader, test_loader, dict(epochs=100, lr=5e-3, epochs_to_plot=[0, 1, 3, 6, 10, 99]))
<ipython-input-41-5d16b72566e2> in train_epochs(model, train_loader, test_loader, train_args)
37 for epoch in tqdm_notebook(range(epochs), desc='Epoch', leave=False):
38 model.train()
---> 39 train(model, train_loader, optimizer)
40 train_loss = eval_loss(model, train_loader)
41 train_losses.append(train_loss)
<ipython-input-41-5d16b72566e2> in train(model, train_loader, optimizer)
4 for x in train_loader:
5 x = x.to(ptu.device).float()
----> 6 loss = model.nll(x)
7 optimizer.zero_grad()
8 loss.backward()
<ipython-input-52-b941007c468c> in nll(self, x)
100
101 def nll(self, x):
--> 102 return - self.log_prob(x).mean()
103
104 def plot(self, title):
<ipython-input-52-b941007c468c> in log_prob(self, x)
96
97 def log_prob(self, x):
---> 98 z, log_det = self.flow(x)
99 return (self.base_dist.log_prob(z) + log_det).sum(dim=1) # shape: [batch_size, dim]
100
<ipython-input-52-b941007c468c> in flow(self, x)
92 x1, x2 = torch.chunk(x, 2, dim=1)
93 z1, log_det1 = self.dim1_flow.flow(x1.squeeze())
---> 94 z2, log_det2 = self.dim2_flow.flow(x2, cond=x1)
95 return torch.cat([z1.unsqueeze(1), z2.unsqueeze(1)], dim=1), torch.cat([log_det1.unsqueeze(1), log_det2.unsqueeze(1)], dim=1)
96
<ipython-input-52-b941007c468c> in flow(self, x, cond)
33 def flow(self, x, cond):
34 # parameters of flow on x depend on what it's conditioned on
---> 35 loc, log_scale, weight_logits = torch.chunk(self.mlp(cond), 3, dim=1)
36 weights = F.softmax(weight_logits)
37
~/.virtualenvs/deepul/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
487 result = self._slow_forward(*input, **kwargs)
488 else:
--> 489 result = self.forward(*input, **kwargs)
490 for hook in self._forward_hooks.values():
491 hook_result = hook(self, input, result)
<ipython-input-52-b941007c468c> in forward(self, x)
12
13 def forward(self, x):
---> 14 return self.layers(x)
15
16 # same CDF flow as in Demo 1, but conditioned on an auxillary variable
~/.virtualenvs/deepul/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
487 result = self._slow_forward(*input, **kwargs)
488 else:
--> 489 result = self.forward(*input, **kwargs)
490 for hook in self._forward_hooks.values():
491 hook_result = hook(self, input, result)
~/.virtualenvs/deepul/lib/python3.6/site-packages/torch/nn/modules/container.py in forward(self, input)
90 def forward(self, input):
91 for module in self._modules.values():
---> 92 input = module(input)
93 return input
94
~/.virtualenvs/deepul/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
487 result = self._slow_forward(*input, **kwargs)
488 else:
--> 489 result = self.forward(*input, **kwargs)
490 for hook in self._forward_hooks.values():
491 hook_result = hook(self, input, result)
~/.virtualenvs/deepul/lib/python3.6/site-packages/torch/nn/modules/linear.py in forward(self, input)
65 @weak_script_method
66 def forward(self, input):
---> 67 return F.linear(input, self.weight, self.bias)
68
69 def extra_repr(self):
~/.virtualenvs/deepul/lib/python3.6/site-packages/torch/nn/functional.py in linear(input, weight, bias)
1350 if input.dim() == 2 and bias is not None:
1351 # fused op is marginally faster
-> 1352 ret = torch.addmm(torch.jit._unwrap_optional(bias), input, weight.t())
1353 else:
1354 output = input.matmul(weight.t())
RuntimeError: Expected object of backend CPU but got backend CUDA for argument #4 'mat1'
Metadata
Assignees
Labels
No labels