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

网站已备案 还不能访问知名的中小企业有哪些

网站已备案 还不能访问,知名的中小企业有哪些,龙岩iot开发福建小程序建设,宜昌市工程造价信息网PyTorch 实现 GloVe#xff08;Global Vectors for Word Representation#xff09; 的完整代码#xff0c;使用 中文语料 进行训练#xff0c;包括 共现矩阵构建、模型定义、训练和测试。 1. GloVe 介绍 基于词的共现信息#xff08;不像 Word2Vec 使用滑动窗口预测Global Vectors for Word Representation 的完整代码使用 中文语料 进行训练包括 共现矩阵构建、模型定义、训练和测试。 1. GloVe 介绍 基于词的共现信息不像 Word2Vec 使用滑动窗口预测  适合较大规模的数据比 Word2Vec 更稳定 学习出的词向量能捕捉语义信息如类比关系 import torch import torch.nn as nn import torch.optim as optim import numpy as np import jieba from collections import Counter from scipy.sparse import coo_matrix# 1. 数据预处理 corpus [我们 喜欢 深度 学习,自然 语言 处理 是 有趣 的,人工智能 改变 了 世界,深度 学习 是 人工智能 的 重要 组成部分 ]# 分词 tokenized_corpus [list(jieba.cut(sentence)) for sentence in corpus] vocab set(word for sentence in tokenized_corpus for word in sentence) word2idx {word: idx for idx, word in enumerate(vocab)} idx2word {idx: word for word, idx in word2idx.items()}# 计算共现矩阵 window_size 2 co_occurrence Counter()for sentence in tokenized_corpus:indices [word2idx[word] for word in sentence]for center_idx in range(len(indices)):center_word indices[center_idx]for offset in range(-window_size, window_size 1):context_idx center_idx offsetif 0 context_idx len(indices) and context_idx ! center_idx:context_word indices[context_idx]co_occurrence[(center_word, context_word)] 1# 转换为稀疏矩阵 rows, cols, values zip(*[(c[0], c[1], v) for c, v in co_occurrence.items()]) X coo_matrix((values, (rows, cols)), shape(len(vocab), len(vocab)))# 2. 定义 GloVe 模型 class GloVe(nn.Module):def __init__(self, vocab_size, embedding_dim):super(GloVe, self).__init__()self.w_embeddings nn.Embedding(vocab_size, embedding_dim) # 中心词嵌入self.c_embeddings nn.Embedding(vocab_size, embedding_dim) # 上下文词嵌入self.w_bias nn.Embedding(vocab_size, 1) # 中心词偏置self.c_bias nn.Embedding(vocab_size, 1) # 上下文词偏置nn.init.xavier_uniform_(self.w_embeddings.weight)nn.init.xavier_uniform_(self.c_embeddings.weight)def forward(self, center, context, co_occur):w_emb self.w_embeddings(center)c_emb self.c_embeddings(context)w_bias self.w_bias(center).squeeze()c_bias self.c_bias(context).squeeze()dot_product (w_emb * c_emb).sum(dim1)loss (dot_product w_bias c_bias - torch.log(co_occur 1e-8)) ** 2return loss.mean()# 初始化模型 embedding_dim 10 model GloVe(len(vocab), embedding_dim)# 3. 训练 GloVe criterion nn.MSELoss() optimizer optim.Adam(model.parameters(), lr0.01) num_epochs 100# 转换数据 co_occurrence_tensor torch.tensor(X.data, dtypetorch.float) pairs list(zip(X.row, X.col, co_occurrence_tensor))for epoch in range(num_epochs):total_loss 0np.random.shuffle(pairs)for center, context, co_occur in pairs:optimizer.zero_grad()loss model(torch.tensor([center], dtypetorch.long),torch.tensor([context], dtypetorch.long),torch.tensor([co_occur], dtypetorch.float) # 修正数据类型)loss.backward()optimizer.step()total_loss loss.item()if (epoch 1) % 10 0:print(fEpoch [{epoch 1}/{num_epochs}], Loss: {total_loss:.4f})# 4. 获取词向量 word_vectors model.w_embeddings.weight.data.numpy()# 5. 计算相似度 def most_similar(word, top_n3):if word not in word2idx:return 单词不在词汇表中word_vec word_vectors[word2idx[word]].reshape(1, -1)similarities np.dot(word_vectors, word_vec.T).squeeze()similar_idx similarities.argsort()[::-1][1:top_n 1]return [(idx2word[idx], similarities[idx]) for idx in similar_idx]# 测试 test_words [深度, 学习, 人工智能] for word in test_words:print(f【{word}】的相似单词:, most_similar(word))数据预处理 分词使用 jieba.cut()构建共现矩阵计算窗口内的单词共现频率使用稀疏矩阵存储提高计算效率 GloVe 模型 Embedding 层 训练词向量中心词和上下文词分开Bias 变量 用于调整预测值损失函数 最小化 log(共现次数) 与词向量点积的差值 计算词向量相似度 使用 cosine similarity找出 top_n 最相似的单词
http://www.w-s-a.com/news/806812/

相关文章:

  • 用织梦做的网站好用吗w网站链接如何做脚注
  • 东莞做网站公司在哪哪里有网站培训的
  • 做宣传 为什么要做网站那重庆网站建设公司在线联系
  • 网站设计制作售价多少钱制作图片的软件是
  • 网站验证码目录简单带数据库的网站模版
  • 制作网站用c#做前台网站建设专题的意义
  • 广西建设职业技术学院教育网站牡丹区建设局网站
  • 网站后台怎么用ftp打开上海外贸进出口有限公司
  • 淘宝建设网站的意义大学生做那个视频网站
  • 如何提高你的网站的粘性建设银行流水网站
  • 微信h5在哪个网站做泰州专业网站制作公司
  • 现在.net做网站的多吗建设工程造价网
  • pc访问手机网站跳转违法网站开发人员
  • 网站前端做报名框wordpress 启动慢
  • 沈阳做网站客户多吗前端可以做网站吗
  • 网站设计规划书新媒体营销策略分析
  • dw个人网站主页怎么做天津工程信息建设网
  • 顺义做网站的公司网站页面设计基础教程
  • 安阳哪个公司做网站好企业没有做网站有的坏处
  • 网站开发有必要用php框架wordpress分页导航代码
  • wordpress建站seo鞍山制作网站哪家好
  • 网站空间流量查询上海门户网站制作
  • 网站开发技术是什么专业会的加强普法网站和普法网络集群建设
  • 上海建筑网站seo 推广
  • 乌兰察布做网站公司爱站网关键词挖掘工具站长工具
  • 白银网站建设白银申请网站空间怎么做
  • 免费炫酷网站模板网站建设需要用到什么软件有哪些
  • 电商网站开发 文献综述大型网站建设企业
  • 如何在建设部网站补录项目单仁牛商
  • 社保网站上做减员一直不审核软件程序开发