#1787·trax

AttributeError: 'function' 对象没有 'n_steps_per_checkpoint' 属性,适用于 NLP 机器翻译模型

作者: Sumit1673创建于 2023年9月29日更新于 2023年9月29日

def nmt_attention_model(input_vocab_size: int = 33300, target_vocab_size: int = 33300, d_model: int = 1024, n_encoder_layers: int = 2, n_decoder_layers: int = 2, n_attn_heads: int = 1, dropout: float = 0.0, mode: str = "train") -> tl.Serial: """Returns an LSTM sequence-to-sequence model with attention.""" inp_encoder = encoder_fn(input_vocab_size, d_model, n_encoder_layers) pre_attn_decoder = pre_attention_decoder(mode, target_vocab_size, d_model=d_model, )

return tl.Serial(
    tl.Select([0, 1, 0, 1]),
    tl.Parallel(inp_encoder, pre_attn_decoder),
    tl.Fn("CreateAttnInputs", create_attention_inps, n_out=4),
    # nest it inside a Residual layer to add to the pre-attention decoder activations (i.e. queries)
    tl.Residual(tl.AttentionQKV(d_model, n_heads=n_attn_heads, dropout=dropout, mode=mode)),
    # dropping mask (since there are 3 inputs, activations, mask and target tokens)
    tl.Select([0, 2]),
    [tl.LSTM(d_model) for _ in range(n_decoder_layers)],
    tl.Dense(target_vocab_size),
    tl.LogSoftmax()
)

def train_fun(train_batch_stream): return training.TrainTask( labeled_data=train_batch_stream, loss=tl.CrossEntropyLoss(), optimizer=trax.optimizers.Adam(0.01), lr=trax.lr.warmup_and_rsqrt_decay(1000, 0.01), n_steps_per_checkpoint=20 )

def eval_fun(eval_batch_stream): return training.EvalTask( labeled_data=eval_batch_stream, metrics=[tl.CrossEntropyLoss(), tl.Accuracy()] )

t = training.Loop(nmt_attention_model(mode='train'), train_fun, eval_tasks=[eval_fun],