【问题标题】:Understanding introductory example on transformers in Trax了解 Trax 中变压器的介绍性示例
【发布时间】:2020-12-08 23:45:04
【问题描述】:

我的目标是了解 Trax 中有关变压器的介绍性示例,可以在 https://trax-ml.readthedocs.io/en/latest/notebooks/trax_intro.html 找到:

import trax

# Create a Transformer model.
# Pre-trained model config in gs://trax-ml/models/translation/ende_wmt32k.gin
model = trax.models.Transformer(
    input_vocab_size=33300,
    d_model=512, d_ff=2048,
    n_heads=8, n_encoder_layers=6, n_decoder_layers=6,
    max_len=2048, mode='predict')

# Initialize using pre-trained weights.
model.init_from_file('gs://trax-ml/models/translation/ende_wmt32k.pkl.gz',
                     weights_only=True)

# Tokenize a sentence.
sentence = 'It is nice to learn new things today!'
tokenized = list(trax.data.tokenize(iter([sentence]),  # Operates on streams.
                                    vocab_dir='gs://trax-ml/vocabs/',
                                    vocab_file='ende_32k.subword'))[0]

# Decode from the Transformer.
tokenized = tokenized[None, :]  # Add batch dimension.
tokenized_translation = trax.supervised.decoding.autoregressive_sample(
    model, tokenized, temperature=0.0)  # Higher temperature: more diverse results.

# De-tokenize,
tokenized_translation = tokenized_translation[0][:-1]  # Remove batch and EOS.
translation = trax.data.detokenize(tokenized_translation,
                                   vocab_dir='gs://trax-ml/vocabs/',
                                   vocab_file='ende_32k.subword')
print(translation)

这个例子工作得很好。但是,当我尝试使用 initialised 模型翻译另一个示例时,例如

sentence = 'I would like to try another example.'
tokenized = list(trax.data.tokenize(iter([sentence]),
                                    vocab_dir='gs://trax-ml/vocabs/',
                                    vocab_file='ende_32k.subword'))[0]
tokenized = tokenized[None, :]
tokenized_translation = trax.supervised.decoding.autoregressive_sample(
    model, tokenized, temperature=0.0)
tokenized_translation = tokenized_translation[0][:-1]
translation = trax.data.detokenize(tokenized_translation,
                                   vocab_dir='gs://trax-ml/vocabs/',
                                   vocab_file='ende_32k.subword')
print(translation)

我在本地计算机和 Google Colab 上都得到了输出 !。其他示例也是如此。

当我构建和初始化一个新模型时,一切正常。

这是一个错误吗?如果没有,这里发生了什么,我该如何避免/修复这种行为?

标记化和去标记化似乎运作良好,我调试了它。 trax.supervised.decoding.autoregressive_sample 中似乎出了问题/出乎意料。

【问题讨论】:

    标签: python machine-translation trax


    【解决方案1】:

    我自己发现了...需要重置模型的state。所以下面的代码对我有用:

    def translate(model, sentence, vocab_dir, vocab_file):
        empty_state = model.state # save empty state
        tokenized_sentence = next(trax.data.tokenize(iter([sentence]), vocab_dir=vocab_dir,
                                                     vocab_file=vocab_file))
        tokenized_translation = trax.supervised.decoding.autoregressive_sample(
            model, tokenized_sentence[None, :], temperature=0.0)[0][:-1]
        translation = trax.data.detokenize(tokenized_translation, vocab_dir=vocab_dir,
                                           vocab_file=vocab_file)
        model.state = empty_state # reset state
        return translation
    
    # Create a Transformer model.
    # Pre-trained model config in gs://trax-ml/models/translation/ende_wmt32k.gin
    model = trax.models.Transformer(input_vocab_size=33300, d_model=512, d_ff=2048, n_heads=8,
                                    n_encoder_layers=6, n_decoder_layers=6, max_len=2048,
                                    mode='predict')
    # Initialize using pre-trained weights.
    model.init_from_file('gs://trax-ml/models/translation/ende_wmt32k.pkl.gz',
                         weights_only=True)
    
    print(translate(model, 'It is nice to learn new things today!',
                    vocab_dir='gs://trax-ml/vocabs/', vocab_file='ende_32k.subword'))
    print(translate(model, 'I would like to try another example.',
                    vocab_dir='gs://trax-ml/vocabs/', vocab_file='ende_32k.subword'))
    

    【讨论】:

    • 您是否尝试过使用相同的代码将德语翻译成英语?这是如何运作的?我找不到任何经过预训练的变压器重量。
    • 不,我没有。是的,你需要有一个预先训练好的变压器。我研究了这个例子来了解变压器和 Trax,我对 en-de 翻译器并不感兴趣。您是否尝试将网址'gs://trax-ml/models/translation/ende_wmt32k.pkl.gz' 替换为'gs://trax-ml/models/translation/deen_wmt32k.pkl.gz'?那是我天真的猜测......
    • 我试过了,但它不能识别这个模型,我认为它甚至不是一个真正的模型。我会尝试别的。但是感谢您的入住。您知道我在哪里可以找到 trax 中的其他预训练翻译模型吗?
    • 否,但您可以在 Gitter 上的 Trax 社区 (gitter.im/trax-ml/community) 询问(或阅读积压工作)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-06
    • 2010-11-22
    • 1970-01-01
    • 1970-01-01
    • 2018-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多