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

行时出现这个错误: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()))


目录
相关文章
RuntimeError: Given groups=1, weight of size 64 128 1 7, expected input[16,
RuntimeError: Given groups=1, weight of size 64 128 1 7, expected input[16,
2358 0
|
API 数据格式
TensorFlow2._:model.summary() Output Shape为multiple解决方法
TensorFlow2._:model.summary() Output Shape为multiple解决方法
185 0
TensorFlow2._:model.summary() Output Shape为multiple解决方法
|
11月前
|
PyTorch 算法框架/工具 异构计算
Pytorch出现RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor)
这个问题的主要原因是输入的数据类型与网络参数的类型不符。
186 0
|
11月前
Expected more than 1 value per channel when training, got input size torch.Size
因为模型中用了batchnomolization,训练中用batch训练的时候当前batch恰好只含一个sample,而由于BatchNorm操作需要多于一个数据计算平均值,因此造成该错误。
694 0
|
11月前
AttributeError: module ‘torch.utils‘ has no attribute ‘data‘
属性错误:模块的'torch.utils'没有属性'data'
68 0
|
11月前
[AssertionError: nput tensor input format are different]
分析到这儿就明白了。input tensor虽然格式也是CHW, 但它还有一个batch维度,所以报错。
121 0
|
PyTorch 算法框架/工具
pytorch报错 RuntimeError: The size of tensor a (25) must match the size of tensor b (50) at non-singleton dimension 1 怎么解决?
这个错误提示表明,在进行某个操作时,张量a和b在第1个非单例维(即除了1以外的维度)上的大小不一致。例如,如果a是一个形状为(5, 5)的张量,而b是一个形状为(5, 10)的张量,则在第二个维度上的大小不匹配。
3076 0
解决AssertionError: size of input tensor and input format are different.tensor shape: (3, 138input_for
解决AssertionError: size of input tensor and input format are different.tensor shape: (3, 138input_for
302 0
|
TensorFlow 算法框架/工具
解决TypeError: tf__update_state() got an unexpected keyword argument ‘sample_weight‘
解决TypeError: tf__update_state() got an unexpected keyword argument ‘sample_weight‘
229 0
解决TypeError: tf__update_state() got an unexpected keyword argument ‘sample_weight‘
|
机器学习/深度学习 PyTorch 算法框架/工具
解决Pytorch中RuntimeError: expected scalar type Double but found Float
解决Pytorch中RuntimeError: expected scalar type Double but found Float
2419 0