typeError:\uuuu init\uuuuuuuuuu()获取了意外的关键字参数“text\u len”

2024-04-26 14:59:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试在colab中运行tacotron模型

def prepare_dataloaders(hparams):
    # Get data, data loaders and collate function ready
        
    trainset = TextMelLoader(hparams.training_files, hparams)
    valset = TextMelLoader(hparams.validation_files, hparams)
    collate_fn = TextMelCollate(1, text_len = 190, mel_len = 890)

    train_loader = DataLoader(trainset,
                          num_workers=2,
                          sampler=HkSampler(trainset),
                          batch_size=hparams.batch_size,
                          pin_memory=False,
                          drop_last=True,
                          collate_fn=collate_fn)
    return train_loader, valset, collate_fn

class TextMelCollate():
    """ Zero-pads model inputs and targets based on number of frames per setep
    """
    def __init__(self, n_frames_per_step, text_len=20, mel_len=100):
        # we only support one frame per step
        print('we are in TextMelCollage ==> ', text_len)
        assert (n_frames_per_step == 1)
        self.n_frames_per_step = n_frames_per_step
        print(text_len)
        self.text_len = text_len
        self.mel_len = mel_len

    def __call__(self, batch):
        """Collate's training batch from normalized text and mel-spectrogram
        PARAMS
        ------
        batch: [text_normalized, mel_normalized]
        """
        # Right zero-pad all one-hot text sequences to max input length
        text_len = max([len(x[0]) for x in batch]) // 5 * 5 + 5
        self.text_len = max(self.text_len, text_len)

        text_padded = torch.LongTensor(len(batch), self.text_len)
        text_padded.zero_()
        input_lengths = torch.LongTensor([len(x[0]) for x in batch])
        for i in range(len(batch)):
            text = batch[i][0]
            text_padded[i, :text.size(0)] = text

        # Right zero-pad mel-spec
        num_mels = batch[0][1].size(0)
        mel_len = max([x[1].size(1) for x in batch]) // 5 * 5 + 5
        self.mel_len = max(self.mel_len, mel_len)

        # include mel padded and gate padded
        mel_padded = torch.FloatTensor(len(batch), num_mels, self.mel_len)
        mel_padded.zero_()
        gate_padded = torch.FloatTensor(len(batch), self.mel_len)
        gate_padded.zero_()
        output_lengths = torch.LongTensor(len(batch))
        for i in range(len(batch)):
            mel = batch[i][1]
            mel_padded[i, :, :mel.size(1)] = mel
            gate_padded[i, mel.size(1) - 1:] = 1
            output_lengths[i] = mel.size(1)

        return text_padded, input_lengths, mel_padded, gate_padded, \
            output_lengths




但我得到了如下错误
在TextMelCollate的init中,text\u len是int类型的
我不明白为什么会发生类型错误

您可以在colab中运行此代码
https://colab.research.google.com/drive/1XU4SWsybhuUYqooyLcFHXmyX3pOOWJON#scrollTo=UxGBm1FdJhlW&uniqifier=2

%tensorflow_versiononly switches the major version: 1.x or 2.x.<br> You set:1.15. This will be interpreted as: 1.x`.
TensorFlow 1.x selected.
tpu backend at grpc://10.92.95.162:8470
grpc://10.92.95.162:8470
Parsing command line hparams: batch_size=128
Traceback (most recent call last):
File "hk_train.py", line 220, in
args.warm_start, hparams)
File "hk_train.py", line 116, in train
train_loader, valset, collate_fn = prepare_dataloaders(hparams)
File "hk_train.py", line 56, in prepare_dataloaders
collate_fn = TextMelCollate(1, text_len = 190, mel_len = 890)

TypeError: init() got an unexpected keyword argument 'text_len'

enter image description here


Tags: textinselfsizeframeslenstepbatch