一、环境配置
1.在conda中创建allennlp环境:
conda create -n allennlp python=3.6
2.安装allennlp
pip install allennlp
二、下载训练好的参数和模型
参数下载: 链接: weights.hdf5 模型下载:options.json
三、获得词向量
from allennlp.modules.elmo import Elmo, batch_to_ids
options_file = "options.json"
weight_file = "weights.hdf5"
elmo = Elmo(options_file, weight_file, 1, dropout=0)
sentence_lists = ["I have a dog", "How are you , today is Monday","I am fine thanks"]
character_ids = batch_to_ids(sentence_lists)
embeddings = elmo(character_ids)['elmo_representations']
context_tokens = [['I', 'love', 'you', '.'], ['Sorry', ',', 'I', 'don', "'t", 'love', 'you', '.']]
elmo_embedding, elmo_mask = elmo.batch_to_embeddings(context_tokens)
print(embeddings)
|