【问题标题】:Expected object of scalar type Double but got scalar type Float for argument #3 'mat1' in call to _th_addmm_标量类型 Double 的预期对象,但在调用 _th_addmm_ 时获得了参数 #3 'mat1' 的标量类型 Float
【发布时间】:2020-10-25 10:55:27
【问题描述】:

我正在尝试使用 AlexNet 作为我从 .wav 文件数据派生的 3 通道图像输入的特征提取器。我有形状 (593, 3, 227, 227) 的特征提取器的输入。但是,当使用 AlexNet 模型时,我得到了错误

Traceback (most recent call last):
  File "MainUI.py", line 1625, in <module>
    main(False)
  File "MainUI.py", line 1604, in main
    accuracy_measurement(oversample)
  File "MainUI.py", line 1463, in accuracy_measurement
    features = model.extract_features(features.double())
  File "/Users/sruthikurada/opt/anaconda3/lib/python3.7/site-packages/alexnet_pytorch/model.py", line 77, in extract_features
    x = self.features(inputs)
  File "/Users/sruthikurada/opt/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py", line 550, in __call__
    result = self.forward(*input, **kwargs)
  File "/Users/sruthikurada/opt/anaconda3/lib/python3.7/site-packages/torch/nn/modules/container.py", line 100, in forward
    input = module(input)
  File "/Users/sruthikurada/opt/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py", line 550, in __call__
    result = self.forward(*input, **kwargs)
  File "/Users/sruthikurada/opt/anaconda3/lib/python3.7/site-packages/torch/nn/modules/conv.py", line 353, in forward
    return self._conv_forward(input, self.weight)
  File "/Users/sruthikurada/opt/anaconda3/lib/python3.7/site-packages/torch/nn/modules/conv.py", line 350, in _conv_forward
    self.padding, self.dilation, self.groups)
RuntimeError: Expected object of scalar type Double but got scalar type Float for argument #3 'mat1' in call to _th_addmm_

导致此错误的代码:

features, labels = extract_features(train_files)

print(features.shape) # (593, 3, 227, 227)

import torch
from alexnet_pytorch import AlexNet
model = AlexNet.from_pretrained('alexnet')

features = torch.from_numpy(features).type('torch.DoubleTensor')

features = model.extract_features(features.double()) # <-- This is where the error occurs
print(features.shape)

如您所见,我使用了double() 命令,但这并没有帮助。您能提供一些帮助吗?

【问题讨论】:

标签: python pytorch transfer-learning


【解决方案1】:

当我有一个 dtype torch.int64 的张量作为 nn.Conv2d() 的输入时,我得到了一个类似的错误:

RuntimeError:预期标量类型为 Long 的对象,但在调用 th_addmm

时得到参数 #3 'mat1' 的标量类型 Float
import torch.nn as nn
import torch.nn.functional as F

simple_model = nn.Sequential(
    nn.Conv2d(3, 8, kernel_size=3, stride=1, padding=1),
    nn.MaxPool2d(2, 2)
)

print(images.dtype)
for images, labels in train_dl:
    print('images.shape:', images.shape)
    out = simple_model(images)
    print('out.shape:', out.shape)
    break

当我将图像更改为 images.float() 时,问题就解决了。

print(images.dtype)
for images, labels in train_dl:
    print('images.shape:', images.shape)
    out = simple_model(images.float()) #must be float instead of double
    print('out.shape:', out.shape)
    break

【讨论】:

    猜你喜欢
    • 2020-06-15
    • 2020-10-21
    • 2020-05-31
    • 1970-01-01
    • 2021-08-08
    • 2021-09-16
    • 2019-11-06
    • 2020-12-02
    • 2021-07-10
    相关资源
    最近更新 更多