【问题标题】:Expected object of scalar type Short but got scalar type Float for argument #2 'mat2' in call to _th_mm标量类型 Short 的预期对象,但在调用 _th_mm 时获得了参数 #2 'mat2' 的标量类型 Float
【发布时间】:2019-12-06 00:09:37
【问题描述】:

我在使用Pytorch 实现LSTM 时遇到数据类型问题。基于类似的问题,我尝试将 Input、h 和 c 的格式更改为 ShortTensor,如您所见,但我仍然收到相同的错误:

RuntimeError: 标量类型的预期对象 Short 但得到了标量类型 在调用 _th_mm 时浮动参数 #2 'mat2'

class data(Dataset):
    def __init__(self, samples=10000, number=30):
        self.x = torch.from_numpy(np.matrix
                        (np.random.random_integers(0,9,samples*number).reshape(samples, number)))
        self.y = torch.from_numpy(np.zeros((samples))).type(torch.ShortTensor)
        for index, row in enumerate(self.x):
            self.y[index] = 1 if torch.sum(row) >= 130 else 0

class LSTM(nn.Module):
    def __init__(self,i_size, h_size, n_layer, batch_size = 30 ):
        super().__init__()
        self.lstm = nn.LSTM(input_size=i_size, hidden_size=h_size, num_layers=n_layer)
        self.h = torch.randn(n_layer, batch_size, h_size).type(torch.ShortTensor)
        self.c = torch.randn(n_layer, batch_size, h_size).type(torch.ShortTensor)
        self.hidden = (self.h, self.c)
        self.linear = nn.Linear(n_layer, 1)

    def forward(self,x):
        out, hidden = self.lstm(x.type(torch.ShortTensor), self.hidden)
        out = nn.Softmax(self.linear(out.short()))
        return out

data_set = data()
train_data = data_set.x[0:8000, :, None]
train_label = data_set.y[0:8000]
test_data = data_set.x[8000:, : , None]
test_label = data_set.y[8000:]

input_size = 1
hidden_size = 30
layer_num = 200
model_LSTM = LSTM(input_size, hidden_size, layer_num)
#model_LSTM.cuda()
y_ = model_LSTM(train_data)

【问题讨论】:

    标签: deep-learning pytorch lstm


    【解决方案1】:

    我在int16 中生成输入数据,显然nn.LSTM 只需要float32,错误并不清楚。

    【讨论】:

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