当前位置: 首页 > news >正文

ELMo 说明解析及用法

ELMo(Embeddings from Language Models) 的全面解析,包括原理、用法、解决的问题以及代码实现示例:

一. ELMo 简介

ELMo(Embeddings from Language Models)是由 AllenNLP 在 2018 年提出的 上下文相关的词嵌入模型。与传统静态词嵌入(如 Word2Vec、GloVe)不同,ELMo 生成的词向量会随上下文动态变化,解决了多义词和复杂语境下的语义表示问题。

二. ELMo 的核心思想

  • 双向语言模型(BiLM): ELMo 通过双向 LSTM 分别建模正向和反向的语言模型,捕捉上下文信息。
    • 正向语言模型:根据前文预测当前词。
    • 反向语言模型:根据后文预测当前词。
  • 多层表示融合: 整合 LSTM 不同层的隐藏状态(浅层捕捉语法,深层捕捉语义),生成动态词向量。

结构图

示意图

三、实现过程

四. ELMo 解决的问题

问题类型传统方法缺陷ELMo 的改进
多义词歧义Word2Vec 对多义词只有单一表示根据上下文生成不同嵌入(如 "bank" 在金融/河流场景不同)
复杂语境理解忽略句子结构信息通过双向 LSTM 捕捉前后文依赖关系
任务特定特征提取需从头训练模型提供预训练嵌入,支持下游任务微调

五. ELMo 的用法

  • 安装依赖

#导入

pip install allennlp allennlp-models

  •  自定义 ELMo 嵌入提取
from allennlp.commands.elmo import ElmoEmbedder# 加载预训练 ELMo
elmo = ElmoEmbedder()# 提取单句词向量
sentence = ["I", "ate", "an", "apple"]
vectors = elmo.embed_sentence(sentence)  # 返回三层 LSTM 的输出(每层 1024 维)
print(vectors.shape)  # (3, 4, 1024): 3 层 x 4 词 x 1024 维# 提取批量句子
batch = [["Hello", "world"], ["ELMo", "is", "awesome"]]
batch_vectors = elmo.embed_sentences(batch)
import torch
from allennlp.modules.elmo import Elmo# 配置 ELMo
options_file = "path/to/options.json"
weight_file = "path/to/weights.hdf5"
elmo = Elmo(options_file, weight_file, num_output_representations=1)# 模拟输入
input_ids = torch.randn(2, 10, 50)  # 假设已转换为字符 ID
embeddings = elmo(input_ids)["elmo_representations"][0]  # (2, 10, 1024)

六. 使用场景 分类、命名实体识别(NER)和语义相似度计算等任务

  •  文本分类(Text Classification)    利用 ELMo 的动态词向量增强输入表示,提升分类效果(如情感分析、新闻分类)

from allennlp.modules.elmo import Elmo, batch_to_ids
import torch
import torch.nn as nn# 配置 ELMo
options_file = "https://allennlp.s3.amazonaws.com/models/elmo/2x4096_512_2048cnn/2x4096_512_2048cnn_elmo_options.json"
weight_file = "https://allennlp.s3.amazonaws.com/models/elmo/2x4096_512_2048cnn/2x4096_512_2048cnn_elmo_weights.hdf5"# 定义分类模型
class ELMoTextClassifier(nn.Module):def __init__(self, num_classes):super().__init__()self.elmo = Elmo(options_file, weight_file, num_output_representations=1, dropout=0)self.lstm = nn.LSTM(input_size=1024, hidden_size=256, batch_first=True)self.classifier = nn.Linear(256, num_classes)def forward(self, sentences):# 生成 ELMo 嵌入character_ids = batch_to_ids(sentences)  # 将文本转为字符IDelmo_emb = self.elmo(character_ids)["elmo_representations"][0]  # (batch, seq_len, 1024)# 通过LSTM和分类器lstm_out, _ = self.lstm(elmo_emb)logits = self.classifier(lstm_out[:, -1, :])  # 取最后时间步return logits# 示例使用
model = ELMoTextClassifier(num_classes=2)
sentences = [["I", "love", "this", "movie"], ["This", "is", "terrible"]]
output = model(sentences)
print(output.shape)  # torch.Size([2, 2])
  • 命名实体识别(Named Entity Recognition, NER) 利用 ELMo 捕捉上下文敏感的实体边界(如人名、地名)

from allennlp.modules.elmo import Elmo, batch_to_ids
import torch
import torch.nn as nnclass ELMoForNER(nn.Module):def __init__(self, num_tags):super().__init__()self.elmo = Elmo(options_file, weight_file, num_output_representations=1, dropout=0)self.lstm = nn.LSTM(input_size=1024, hidden_size=256, batch_first=True, bidirectional=True)self.classifier = nn.Linear(512, num_tags)  # 双向LSTM输出拼接def forward(self, sentences):character_ids = batch_to_ids(sentences)elmo_emb = self.elmo(character_ids)["elmo_representations"][0]  # (batch, seq_len, 1024)# 双向LSTMlstm_out, _ = self.lstm(elmo_emb)  # (batch, seq_len, 512)# 每个词对应的标签logitstag_logits = self.classifier(lstm_out)  # (batch, seq_len, num_tags)return tag_logits# 示例使用
model = ELMoForNER(num_tags=5)  # 假设5种实体类型
sentences = [["Apple", "is", "based", "in", "Cupertino"]]
output = model(sentences)
print(output.shape)  # torch.Size([1, 5, 5])
  • 语义相似度计算(Semantic Similarity)

       计算句子对的语义相似度(如问答匹配、 paraphrase 检测)。

代码实现:

from allennlp.modules.elmo import Elmo, batch_to_ids
import torch
import torch.nn.functional as Fdef elmo_sentence_similarity(sentence1, sentence2):# 初始化ELMoelmo = Elmo(options_file, weight_file, num_output_representations=1, dropout=0)# 生成句子嵌入char_ids = batch_to_ids([sentence1, sentence2])embeddings = elmo(char_ids)["elmo_representations"][0]  # (2, seq_len, 1024)# 取句子整体嵌入(均值池化)sent1_emb = torch.mean(embeddings[0], dim=0)  # (1024,)sent2_emb = torch.mean(embeddings[1], dim=0)  # (1024,)# 计算余弦相似度similarity = F.cosine_similarity(sent1_emb.unsqueeze(0), sent2_emb.unsqueeze(0), dim=1)return similarity.item()# 示例使用
sentence1 = ["The", "cat", "sat", "on", "the", "mat"]
sentence2 = ["A", "feline", "is", "sitting", "on", "a", "rug"]
similarity = elmo_sentence_similarity(sentence1, sentence2)
print(f"Similarity: {similarity:.4f}")  # 输出范围 [-1, 1]
  • 词义消歧(Word Sense Disambiguation)

       根据上下文动态区分多义词的不同含义。

代码实现:

from allennlp.modules.elmo import ElmoEmbedderdef disambiguate_word_sense(word, context):elmo = ElmoEmbedder()embeddings = elmo.embed_sentence(context)  # (3 layers, seq_len, 1024)# 获取目标词的ELMo嵌入(所有层拼接)word_index = context.index(word)word_embedding = torch.cat([torch.tensor(embeddings[i][word_index]) for i in range(3)], dim=0)  # 3072维return word_embedding# 示例:区分 "bank" 的不同含义
context1 = ["He", "went", "to", "the", "bank", "to", "deposit", "money"]  # 金融机构
context2 = ["They", "fished", "by", "the", "bank", "of", "the", "river"]  # 河岸embedding1 = disambiguate_word_sense("bank", context1)
embedding2 = disambiguate_word_sense("bank", context2)similarity = F.cosine_similarity(embedding1.unsqueeze(0), embedding2.unsqueeze(0), dim=1)
print(f"Similarity between 'bank' senses: {similarity.item():.4f}")  # 预期较低(不同含义)

相关文章:

  • 高线性低噪放:精密ADC信号链的守护者
  • C4.5算法深度解析:决策树进化的里程碑
  • 打造智能未来:如何使用 .NET 9、Blazor 与 Semantic Kernel 创建属于你的 AI 知识库
  • Ubuntu22.04.4 开启root帐号SSH登陆
  • [GESP202312 五级] 烹饪问题
  • 可理解性输入:洗澡习惯
  • Redis核心数据结构实战
  • rust单体web项目模板搭建
  • 管理综合知识点
  • Kafka动态配置深度解析
  • CSS Background 相关属性详解 文字镂空效果
  • 【HarmonyOS Next之旅】DevEco Studio使用指南(三十六) -> 配置构建(三)
  • 【FPGA学习】 分秒计数器(暂停、按键消抖)
  • Vui:轻量级语音对话模型整合包,让交互更自然
  • 2025国际无人机应用及防控大会四大技术专题深度解析
  • 版本控制与向后兼容性:Protobuf 消息设计的黄金法则
  • Dalvik和ART的区别
  • 分库分表技术栈讲解-Sharding-JDBC
  • 机器学习:特征向量与数据维数概念
  • 如何在FastAPI中玩转GitHub认证,让用户一键登录?
  • wordpress文章点赞功能/自贡网站seo
  • html5做个网站多少钱/成年培训班有哪些
  • 丹阳网站建设开发/百度关键词首页排名
  • 济阳做网站哪家好/杭州优化公司在线留言
  • 成都网站模板/大数据营销专业
  • 网站设计公司排名前十/推广普通话手抄报文字