Allennlp:如何加载预训练的 ELMo 作为 allennlp 模型的嵌入?

Allennlp: How to load a pretrained ELMo as the embedding of allennlp model?

我是 allennlp 的新人。我训练了一个 elmo 模型以将其作为嵌入应用于其他 allennlp 模型但失败了。看来我的模型与配置提供的接口不兼容。我能做什么?

我的 elmo 由 allennlp 使用以下命令训练:

allennlp train config/elmo.jsonnet --serialization-dir /xxx

elmo.jsonnet 除了数据集和词汇外,与 https://github.com/allenai/allennlp-models/blob/main/training_config/lm/bidirectional_language_model.jsonnet 几乎相同。

在那之后,我得到了一个 elmo 模型:

config.json
weights.th
vocabulary/
vocabulary/.lock
vocabulary/non_padded_namespaces.txt
vocabulary/tokens.txt
meta.json

当我尝试将模型加载到 https://github.com/allenai/allennlp-models/blob/main/training_config/rc/bidaf_elmo.jsonnet 中的 bidaf-elmo 等其他模型时,我发现它需要选项和权重:

"elmo": {
    "type": "elmo_token_embedder",
    "do_layer_norm": false,
    "dropout": 0,
    "options_file": "xxx/options.json",
    "weight_file": "xxx/weights.hdf5"
}

哪些不包含在我的模型中。 我试图将 model.state_dict() 更改为 weights.hdf5 但我收到错误消息:

KeyError: "Unable to open object (object 'char_embed' doesn't exist)"

中需要
File "/home/xxx/anaconda3/envs/thesis_torch1.8/lib/python3.8/site-packages/allennlp/modules/elmo.py", line 393, in _load_char_embedding
    char_embed_weights = fin["char_embed"][...]

我用allennlp训练的模型好像接口不兼容。如何将我的 elmo 应用为其他模型的嵌入?

你是对的,这两种格式不一致。

恐怕没有捷径可走。我认为您必须编写一个 TokenEmbedder 来读取和应用 bidirectional_language_model.jsonnet.

的输出

如果您愿意,我们很乐意将其作为对 AllenNLP 的贡献!