天天看點

RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the

行時出現這個錯誤:RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same。

主要是mode沒有把mode沒有調用cuda,而input卻調用了,造成了不統一。這兩個地方要保持一緻。下面舉個同時調用cuda的例子

autoencoderModel = autoencoderModel.cuda() 1,聲明mode時,調用cuda

criterion = nn.MSELoss()

optimizer = torch.optim.Adam(autoencoderModel.parameters(), lr=LEARNING_RATE)

#=======================================================================================================================

# Model Training and Saving

bestLoss = 1

for epoch in range(EPOCHS):

   autoencoderModel.train()

   if epoch % 50 == 0 and epoch > 0:

       optimizer.param_groups[0]['lr'] = optimizer.param_groups[0]['lr'] * 0.1

   for i, autoencoderInput in enumerate(train_loader):

       autoencoderInput = autoencoderInput.cuda()2,input時,調用cuda

       autoencoderOutput = autoencoderModel(autoencoderInput)

       loss = criterion(autoencoderOutput, autoencoderInput)

       optimizer.zero_grad()

       loss.backward()

       optimizer.step()

       if i % PRINT_RREQ == 0:

           print('Epoch: [{0}][{1}/{2}]\t' 'Loss {loss:.4f}\t'.format(epoch, i, len(train_loader), loss=loss.item()))

繼續閱讀