I got a RuntimeError: Expected object of type torch.DoubleTensor but found type torch.cuda.FloatTensor for argument #2 ‘weight’ when i try to train a DCGAN. It remind me that my input`s type is torch.cuda.FloatTensor, but my model only accept torch.DoubleTensor data, they does not match. The problem is i have set my model to specified GPU.
main.py
...
cudnn.benchmark = True
device = torch.device('cuda:3')
G = Generator().to(device=device)
D = Discriminator().to(device=device)
...
model.py
class Generator(nn.Module):
def __init__(self):
super(Generator, self).__init__()
self.step = nn.Sequential(
nn.Conv2d(1, 16, 3, padding=1),
nn.BatchNorm2d(16, eps=0.001),
nn.ReLU(),
nn.Conv2d(16, 32, 3, padding=1),
nn.BatchNorm2d(32, eps=0.002),
nn.ReLU(),
nn.Conv2d(32, 64, 3, padding=1),
nn.BatchNorm2d(64, eps=0.003),
nn.ReLU(),
nn.Conv2d(64, 32, 3, padding=1),
nn.BatchNorm2d(32, eps=0.003),
nn.ReLU(),
nn.Conv2d(32, 16, 3, padding=1),
nn.BatchNorm2d(16, eps=0.002),
nn.ReLU(),
nn.Conv2d(16, 1, 3, padding=1),
nn.Tanh()
)
def forward(self, x):
return self.step(x)
class Discriminator(nn.Module):
def __init__(self):
super(Discriminator, self).__init__()
self.convs = nn.Sequential(
nn.Conv2d(1, 16, 3, padding=1),
nn.BatchNorm2d(16, eps=0.001),
nn.LeakyReLU(),
nn.Conv2d(16, 16, 3, padding=1),
nn.BatchNorm2d(16, eps=0.001),
nn.LeakyReLU(),
nn.MaxPool2d(2, 2),
nn.Conv2d(16, 32, 3, padding=1),
nn.BatchNorm2d(32, eps=0.001),
nn.LeakyReLU(),
nn.Conv2d(32, 32, 3, padding=1),
nn.BatchNorm2d(32, eps=0.001),
nn.LeakyReLU(),
nn.MaxPool2d(2, 2),
nn.Conv2d(32, 64, 3, padding=1),
nn.BatchNorm2d(64, eps=0.001),
nn.LeakyReLU()
)
self.fcn = nn.Sequential(
nn.Linear(64 * 7 * 7, 512),
nn.LeakyReLU(),
nn.Dropout(p=0.4),
nn.Linear(512, 32),
nn.LeakyReLU(),
nn.Dropout(p=0.4),
nn.Linear(32, 1),
nn.Sigmoid()
)
def forward(self, x):
x = self.convs(x)
x = x.view(x.size(0), -1)
x = self.fcn(x)
return x
nn.Module.to(device) just does not work for me. Does i make some mistake or is there some way to do this in pytorch 0.4.0?