RNN - RuntimeError:输入必须有 3 维,得到 2

Posted

技术标签:

【中文标题】RNN - RuntimeError:输入必须有 3 维,得到 2【英文标题】:RNN - RuntimeError: input must have 3 dimensions, got 2 【发布时间】:2021-04-08 08:46:40 【问题描述】:

我收到以下错误:

RuntimeError:输入必须有 3 个维度,得到 2 个维度

我尝试将一个特征列输入 GRU 神经网络。

下面是我的数据加载器和神经网络。当我检索一批数据时,我还包含了我的数据加载器的输出。

我做错了什么???

def batch_data(feature1, sequence_length, batch_size):
“”"
Batch the neural network data using DataLoader
:param feature1: the single feature column
:param sequence_length: The sequence length of each batch
:param batch_size: The size of each batch; the number of sequences in a batch
:return: DataLoader with batched data
“”"
    # total number of batches we can make
    n_batches = len(feature1)//batch_size

    # Keep only enough characters to make full batches
    feature1= feature1[:n_batches * batch_size]

    y_len = len(feature1) - sequence_length

    x, y = [], []
    for idx in range(0, y_len):
        idx_end = sequence_length + idx
        x_batch = feature1[idx:idx_end]
        x.append(x_batch)
        # only making predictions after the last item in the batch
        batch_y = feature1[idx_end]    
        y.append(batch_y)    

    # create tensor datasets
    data = TensorDataset(torch.from_numpy(np.asarray(x)), torch.from_numpy(np.asarray(y)))

    data_loader = DataLoader(data, shuffle=False, batch_size=batch_size)

    # return a dataloader
    return data_loader



# test dataloader on subset of actual data

test_text = data_subset_b
t_loader = batch_data(test_text, sequence_length=5, batch_size=10)
 
data_iter = iter(t_loader)
sample_x, sample_y = data_iter.next()
 
print(sample_x.shape)
print(sample_x)
print()
print(sample_y.shape)
print(sample_y)

当我传入数据时,会生成以下批次……

torch.Size([10, 5])
tensor([[ 0.0045, 0.0040, -0.0008, 0.0005, -0.0012],
[ 0.0040, -0.0008, 0.0005, -0.0012, 0.0000],
[-0.0008, 0.0005, -0.0012, 0.0000, -0.0015],
[ 0.0005, -0.0012, 0.0000, -0.0015, 0.0008],
[-0.0012, 0.0000, -0.0015, 0.0008, 0.0000],
[ 0.0000, -0.0015, 0.0008, 0.0000, 0.0000],
[-0.0015, 0.0008, 0.0000, 0.0000, -0.0008],
[ 0.0008, 0.0000, 0.0000, -0.0008, -0.0039],
[ 0.0000, 0.0000, -0.0008, -0.0039, -0.0026],
[ 0.0000, -0.0008, -0.0039, -0.0026, -0.0082]], dtype=torch.float64)

torch.Size([10])
tensor([ 0.0000, -0.0015, 0.0008, 0.0000, 0.0000, -0.0008, -0.0039, -0.0026,
-0.0082, 0.0078], dtype=torch.float64)

【问题讨论】:

【参考方案1】:

根据您收到的错误提示,GRU 预期的输入张量形状是三维的,形状为 (batch_size, seq_len, input_size)1

但是你正在输入一个形状为 (10, 5) 的张量。你说你的输入有一个特征值,所以你应该为 input_size 添加一个尺寸为 1 的维度。这可以像这样完成

sample_x.unsqueeze(-1)

【讨论】:

在这种特殊情况下要挑剔,(batch, seq_len, input_size)【参考方案2】:

实际上错误本身会告诉您问题所在。作为 GRU 的超类的 RNN 类,期望输入形状为:

 (#batch,#number_of_timesteps,#number_of_features)

因此,对于您的情况,您有 1 个功能,5 个时间步长。在您的数据加载器中,您需要将 X 扩展为 (#batch,5,1)。

【讨论】:

以上是关于RNN - RuntimeError:输入必须有 3 维,得到 2的主要内容,如果未能解决你的问题,请参考以下文章

PyTorch:DecoderRNN:RuntimeError:输入必须有 3 维,得到 2

PyTorch:RuntimeError:输入、输出和索引必须在当前设备上

RuntimeError: cudnn RNN backward can only be called in training mode

RuntimeError: Expected hidden size (2, 24, 50), got (2, 30, 50)

ValueError: Tensor 必须来自与 Tensorflow 中具有双向 RNN 的 Tensor 相同的图

RuntimeError: 给定组=1,大小为 [32, 3, 16, 16, 16] 的权重,预期输入 [100, 16, 16, 16, 3] 有 3 个通道,但有 16 个通道