Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pytorch中的LSTM:如何添加/更改序列长度维度?_Python_Pytorch_Sequence_Lstm_Dimensions - Fatal编程技术网

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

Python Pytorch中的LSTM:如何添加/更改序列长度维度?,python,pytorch,sequence,lstm,dimensions,Python,Pytorch,Sequence,Lstm,Dimensions,我在pytorch中运行LSTM,但据我所知,它只取序列长度=1。当我将序列长度重塑为4或其他数字时,我会得到输入和目标中长度不匹配的错误。如果我同时重塑输入和目标,那么模型会抱怨它不接受多目标标签 我的列车数据集有66512行和16839列,目标中有3个类别/类。我希望使用批量大小为200,序列长度为4,即在一个序列中使用4行数据 请告知如何调整我的模型和/或数据,以便能够针对不同的序列长度(例如,4)运行模型 您已经设置了input\u dim=16839,因此您的模型期望输入的形状(批量大

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

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

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


您已经设置了
input\u dim=16839
,因此您的模型期望输入的形状
(批量大小,序列号16839)

从中绘制批次的
序列张量
,其形状为
(66512,116839)
。因此,您的批次的形状
(批次大小,116839)
。这是因为它满足上述要求


但是,如果您尝试将相同的训练张量重新塑造为
序列长度
=4,则您的
输入尺寸将不再是16839,因此与模型预期的尺寸不匹配,这就是为什么会出现维度不匹配错误。

这就是最终起作用的原因-将输入数据重新塑造为4个序列,每个序列有一个目标值,我根据问题逻辑选择了目标序列中的最后一个值。现在看起来很容易,但那时候很棘手。发布的代码的其余部分是相同的

train_target = torch.tensor(train_data[['Label1','Label2','Label3']].iloc[3::4].values.astype(np.float32))
train_target = np.argmax(train_target, axis=1)
train = torch.tensor(train_data.drop(['Label1','Label2','Label3'], axis = 1).values.reshape(-1, 4, 16839).astype(np.float32)) 
train_tensor = TensorDataset(train, train_target) 
train_loader = DataLoader(dataset = train_tensor, batch_size = batch_size, shuffle = True)

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

torch.Size([16628, 4, 16839])
torch.Size([16628])
train_target = torch.tensor(train_data[['Label1','Label2','Label3']].iloc[3::4].values.astype(np.float32))
train_target = np.argmax(train_target, axis=1)
train = torch.tensor(train_data.drop(['Label1','Label2','Label3'], axis = 1).values.reshape(-1, 4, 16839).astype(np.float32)) 
train_tensor = TensorDataset(train, train_target) 
train_loader = DataLoader(dataset = train_tensor, batch_size = batch_size, shuffle = True)

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

torch.Size([16628, 4, 16839])
torch.Size([16628])