训练模型与推理模型之间的关联
在此练习中,您将把已训练模型中的权重迁移到推理模型。对于编码器-解码器模型,有 3 个含有参数的层:
- 编码器
GRU层 - 解码器
GRU层 - 解码器
Dense层
其他层(例如 TimeDistributed)不含参数,因此无需拷贝权重。
本练习已为您提供经过训练的编码器 GRU 层(tr_en_gru)、训练好的解码器 GRU(tr_de_gru)以及训练好的 Dense 层(tr_de_dense)。您也可以访问推理模型的所有层(包括编码器),例如编码器 GRU 层(en_gru)、解码器 GRU(de_gru)以及 Dense 层(de_dense)。
本练习是课程的一部分
使用 Keras 的机器翻译
练习说明
- 加载已训练的编码器
GRU层的权重。 - 为推理模型中的编码器
GRU层设置权重。 - 加载训练好的解码器
GRU层的权重,并在推理模型中设置相应权重。 - 加载训练好的解码器
Dense层的权重,并在推理模型中设置相应权重。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Load the weights to the encoder GRU from the trained model
en_gru_w = ____.get_weights()
# Set the weights of the encoder GRU of the inference model
en_gru.____(____)
# Load and set the weights to the decoder GRU
de_gru.____(tr_de_gru.____)
# Load and set the weights to the decoder Dense
____.set_weights(____.____)