PyTorch LSTM:RuntimeError:无效参数0:张量的大小必须匹配,但维度0除外.在维度1中得到1219和440 [英] PyTorch LSTM: RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 0. Got 1219 and 440 in dimension 1

查看:133
本文介绍了PyTorch LSTM:RuntimeError:无效参数0:张量的大小必须匹配,但维度0除外.在维度1中得到1219和440的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本的PyTorch LSTM:

I have a basic PyTorch LSTM:

import torch.nn as nn
import torch.nn.functional as F

class BaselineLSTM(nn.Module):
    def __init__(self):
        super(BaselineLSTM, self).__init__()

        self.lstm = nn.LSTM(input_size=13, hidden_size=13)

    def forward(self, x):
        x = self.lstm(x)

        return x

对于我的数据,我有:

train_set = CorruptedAudioDataset(corrupted_path, train_set=True)
train_loader = torch.utils.data.DataLoader(train_set, batch_size=128, shuffle=True, **kwargs)

我的 CorruptedAudioDataset 具有:

    def __getitem__(self, index):
        corrupted_sound_file = SoundFile(self.file_paths[index])
        corrupted_samplerate = corrupted_sound_file.samplerate
        corrupted_signal_audio_array = corrupted_sound_file.read()

        clean_path = self.file_paths[index].split('/')
        # print(self.file_paths[index], clean_path)
        clean_sound_file = SoundFile(self.file_paths[index])
        clean_samplerate = clean_sound_file.samplerate
        clean_signal_audio_array = clean_sound_file.read()


        corrupted_mfcc = mfcc(corrupted_signal_audio_array, samplerate=corrupted_samplerate)
        clean_mfcc = mfcc(clean_signal_audio_array, samplerate=clean_samplerate)


        print('return', corrupted_mfcc.shape, clean_mfcc.shape)
        return corrupted_mfcc, clean_mfcc

我的训练循环如下:

    model = BaselineLSTM()
    for epoch in range(300):
        for inputs, outputs in train_loader:
            print('inputs', inputs)

这就是我得到错误的那一行:

And that's the line that I get the error on:

  File "train_lstm_baseline.py", line 47, in train
    for inputs, outputs in train_loader:
...
RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 0. Got 1219 and 440 in dimension 1 at ../aten/src/TH/generic/THTensor.cpp:612

不确定我在做什么错.任何帮助,将不胜感激.谢谢!

Not sure what I'm doing wrong. Any help would be appreciated. Thanks!

推荐答案

基本上会引发此异常,因为您正在加载具有不同形状的批次.由于它们存储在相同的张量中,因此所有样本都必须具有相同的形状.在这种情况下,您将无法输入尺寸为0的1219和440.例如,您有类似的东西:

This exception is thrown basically because you're loading batches with different shapes. As they're stored in the same tensor, all samples must have the same shape. In this case, you have an input in dimension 0 with 1219 and 440, which is not possible. For example, you have something like:

torch.Size([1, 1219])
torch.Size([1, 440])
torch.Size([1, 550])
...

您必须具有:

torch.Size([1, n])
torch.Size([1, n])
torch.Size([1, n])
...

解决此问题的最简单方法是设置 batch_size = 1 .但是,这可能会延迟您的代码.

The easiest way to solve this problem is setting batch_size=1. However, it may delay your code.

最好的方法是将数据设置为相同的形状.在这种情况下,您需要评估您的问题以检查是否可能.

The best way is setting the data to the same shape. In this case, you need to assess your problem to check if it's possible.

希望对您有帮助.

这篇关于PyTorch LSTM:RuntimeError:无效参数0:张量的大小必须匹配,但维度0除外.在维度1中得到1219和440的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆