在gpu上发布训练pytorch模型



我正在尝试在PyTorch中实现一个基本MNIST GAN的鉴别器。当我在CPU上运行训练时,它可以在没有任何问题的情况下提供所需的输出。然而,当我在GPU上运行它时,它显示了一个运行时错误。我正在粘贴我的模型的代码以及下面的训练,以及我为尝试在GPU上运行训练所做的修改。

dev = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
def preprocess(x):
return x.view(-1,1,28,28).to(dev)
discriminator = nn.Sequential(
Lambda(preprocess),
nn.Conv2d(1,64,3,stride=2,padding=1),
nn.LeakyReLU(negative_slope=0.2),
nn.Dropout(0.4),
nn.Conv2d(64,64,3,stride=2,padding=1),
nn.LeakyReLU(negative_slope=0.2),
nn.Dropout(0.4),
Lambda(lambda x:x.view(x.size(0),-1)),
nn.Linear(3136,1),
nn.Sigmoid()
)
loss = nn.BCELoss()
opt = optim.Adam(discriminator.parameters(),lr = 0.002)
discriminator.to(dev)
def train_discriminator(model, dataset,opt, n_iter=100, n_batch=256):
half_batch = int(n_batch / 2)
for i in range(n_iter):
X_real, y_real = generate_real_samples(dataset, half_batch)
error = loss(model(X_real),y_real)
error.backward()
X_fake, y_fake = generate_fake_samples(half_batch)
error = loss(model(X_fake),y_fake)
error.backward()
opt.step()

现在运行train_discriminator(discriminator,dataset,opt)我犯了以下错误,我无法理解。

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-15-ee20eb2a8e55> in <module>
----> 1 train_discriminator(discriminator,dataset,opt)
<ipython-input-13-9e6f9b4874c8> in train_discriminator(model, dataset, opt, n_iter, n_batch)
3     for i in range(n_iter):
4         X_real, y_real = generate_real_samples(dataset, half_batch)
----> 5         error = loss(model(X_real),y_real)
6         error.backward()
7         X_fake, y_fake = generate_fake_samples(half_batch)
~/environments/workspace/lib/python3.7/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
530             result = self._slow_forward(*input, **kwargs)
531         else:
--> 532             result = self.forward(*input, **kwargs)
533         for hook in self._forward_hooks.values():
534             hook_result = hook(self, input, result)
~/environments/workspace/lib/python3.7/site-packages/torch/nn/modules/loss.py in forward(self, input, target)
496 
497     def forward(self, input, target):
--> 498         return F.binary_cross_entropy(input, target, weight=self.weight, reduction=self.reduction)
499 
500 
~/environments/workspace/lib/python3.7/site-packages/torch/nn/functional.py in binary_cross_entropy(input, target, weight, size_average, reduce, reduction)
2075 
2076     return torch._C._nn.binary_cross_entropy(
-> 2077         input, target, weight, reduction_enum)
2078 
2079 
RuntimeError: Expected object of device type cuda but got device type cpu for argument #2 'target' in call to _thnn_binary_cross_entropy_forward

如果有人能提出任何需要做出的改变来解决这个问题,我将不胜感激。

根据错误消息,地面实况不在GPU:中

RuntimeError:在对_thn_binary_cross_entropy_forward 的调用中,预期对象的设备类型为cuda,但得到参数#2"target"的设备类型cpu

最新更新