小编TTT*_*TTT的帖子

Pytorch 中的 LSTM:如何添加/更改序列长度维度?

我在 pytorch 中运行 LSTM,但据我了解,它只采用序列长度 = 1。当我将序列长度重塑为 4 或其他数字时,我会收到输入和目标长度不匹配的错误。如果我重塑输入和目标,则模型会抱怨它不接受多目标标签。

我的训练数据集有 66512 行和 16839 列,目标中有 3 个类别/类。我想使用批量大小 200 和序列长度 4,即在序列中使用 4 行数据。

请告知如何调整我的模型和/或数据,以便能够运行各种序列长度(例如 4)的模型。

batch_size=200
import torch  
from torch.utils.data import TensorDataset
from torch.utils.data import DataLoader
train_target = torch.tensor(train_data[['Label1','Label2','Label3']].values.astype(np.float32))
train_target = np.argmax(train_target, axis=1)
train = torch.tensor(train_data.drop(['Label1','Label2','Label3'], axis = 1).values.astype(np.float32)) 
train_tensor = TensorDataset(train.unsqueeze(1), train_target) 
train_loader = DataLoader(dataset = train_tensor, batch_size = batch_size, shuffle = True)

print(train.shape)
print(train_target.shape)

torch.Size([66512, 16839])
torch.Size([66512])


import torch.nn as nn

class LSTMModel(nn.Module):
    def __init__(self, input_dim, hidden_dim, layer_dim, output_dim):
        super(LSTMModel, self).__init__() …
Run Code Online (Sandbox Code Playgroud)

python sequence dimensions lstm pytorch

5
推荐指数
1
解决办法
5978
查看次数

标签 统计

dimensions ×1

lstm ×1

python ×1

pytorch ×1

sequence ×1