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

如何衡量一个网站的价值接广告推广的平台

如何衡量一个网站的价值,接广告推广的平台,zoho企业邮箱,如何制作简易个人网站ELMo(Embeddings from Language Models) 的全面解析,包括原理、用法、解决的问题以及代码实现示例: 一. ELMo 简介 ELMo(Embeddings from Language Models)是由 AllenNLP 在 2018 年提出的 上下文相关的词…

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}")  # 预期较低(不同含义)

http://www.dtcms.com/wzjs/261495.html

相关文章:

  • 宝山武汉阳网站建设百度搜索关键词排名靠前
  • 免费网站建设垂询186 6159 6345秦皇岛seo优化
  • 电信网络服务商广州网站优化公司如何
  • c2c网站设计南京百度seo代理
  • 郑州汉狮做网站网络公司短视频营销策略
  • 永康网站建设制作seo教程网站优化
  • 喀什地区建设局网站自媒体发布平台有哪些
  • 做中英文游戏门户网站关键词怎么弄网络推广平台公司
  • php网站功能沧浪seo网站优化软件
  • 做微商选择的哪个平台微平台网站营销渠道策略
  • 食品贸易网站建设案例搜索引擎排名优化公司
  • wordpress前端页面不显示广州网站快速排名优化
  • 做网站条件免费广告推广
  • 东台企业网站建设网站快照优化公司
  • 淘宝店标在线制作免费济南seo全网营销
  • 初中生怎样做网站赚钱2345电脑版网址导航
  • 我要自学网app下载云seo关键词排名优化软件
  • 东莞公司官网建站网站点击量查询
  • 百度可以做网站吗东莞做好网络推广
  • 厦门建设企业网站建设网站seo推广平台
  • 代做广联达 的网站2020做seo还有出路吗
  • 长沙做网站最好的公司今日军事新闻最新消息新闻
  • 江苏城乡建设职业学院就业网站百度注册公司网站
  • 贵州建设厅造价信息网站cba最新消息
  • js做网站框架成都进入搜索热度前五
  • 新人怎么自己做网站国家免费技能培训官网
  • 上海网站建设公司 珍岛网页入口网站推广
  • 个人网站的留言板数据库怎么做搭建一个网站
  • 做刷单哪个网站找小白成都百度推广电话号码是多少
  • 南宁企业建站网站推广与优化方案