python-pytorch实现skip-gram 0.5.000【直接可运行】

2024-04-10 10:04

本文主要是介绍python-pytorch实现skip-gram 0.5.000【直接可运行】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

python-pytorch实现skip-gram 0.5.000【直接可运行】

    • 参考
    • 导入包
    • 加载数据和切词
    • 获取wordList、raw_text
    • 获取vocab、vocab_size
    • word_to_idx、idx_to_word
    • 准备训练数据
    • 准备模型和参数
    • 训练模型
    • 保存模型
    • 简单预测
    • 获取训练后的词向量
    • 画图看下分布
    • 利用词向量计算相似度
      • 余弦
      • 点积

参考

https://blog.csdn.net/Metal1/article/details/132886936

https://blog.csdn.net/L_goodboy/article/details/136347947

导入包

import jieba
import torch
import numpy as np
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from matplotlib import pyplot as plt
from sklearn.decomposition import PCA
from tqdm import tqdm, trange
torch.manual_seed(1)

加载数据和切词

# 加载停用词词表
def load_stop_words():"""停用词是指在信息检索中,为节省存储空间和提高搜索效率,在处理自然语言数据(或文本)之前或之后会自动过滤掉某些字或词"""with open('data/stopwords.txt', "r", encoding="utf-8") as f:return f.read().split("\n")# 加载文本,切词
def cut_words():stop_words = load_stop_words()with open('data/zh.txt', encoding='utf8') as f:allData = f.readlines()result = []for words in allData:c_words = jieba.lcut(words)for word in c_words:if word not in stop_words and word != "\n":result.append(word)return result# 加载文本,切词
def cut_sentense(str):stop_words = load_stop_words()with open('data/zh.txt', encoding='utf8') as f:allData = f.readlines()result = []c_words = jieba.lcut(str)for word in c_words:if word not in stop_words and word != "\n":result.append(word)return result

获取wordList、raw_text

wordList = []
data = cut_words()
data

count = 0
for words in data:if words not in wordList:wordList.append(words)
print("wordList=", wordList)raw_text = wordList
print("raw_text=", raw_text)
# 超参数
learning_rate = 0.003
# 放cuda或者cpu里
device = torch.device('cpu')
# 上下文信息,即涉及文本的前n个和后n个
context_size = 2
# 词嵌入的维度,即一个单词用多少个浮点数表示比如 the=[10.2323,12.132133,4.1219774]...
embedding_dim = 100
epoch = 10
def make_context_vector(context, word_to_ix):idxs = [word_to_ix[w] for w in context]return torch.tensor(idxs, dtype=torch.long)

获取vocab、vocab_size

# 把所有词集合转成dict
vocab = set(wordList)
vocab_size = len(vocab)
vocab,vocab_size

word_to_idx、idx_to_word

word_to_idx = {word: i for i, word in enumerate(vocab)}
idx_to_word = {i: word for i, word in enumerate(vocab)}

准备训练数据

data3 = []
window_size1=2
for i,word in enumerate(raw_text):target = raw_text[i]contexts=raw_text[max(i - window_size1, 0): min(i + window_size1 + 1, len(raw_text))]for context in contexts:if target!=context:data3.append((context,target))
data3,len(data3)

准备模型和参数

class SkipGramModel(nn.Module):def __init__(self, vocab_size, embedding_dim):super(SkipGramModel, self).__init__()self.embedding = nn.Embedding(vocab_size, embedding_dim)self.linear = nn.Linear(embedding_dim, vocab_size)def forward(self, center_word):embedded = self.embedding(center_word)output = self.linear(embedded)return outputmodel = SkipGramModel(vocab_size, embedding_dim)
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)

训练模型

# Training
for epoch in tqdm(range(2000)):loss_sum = 0for target,input in data3:targetidx=word_to_idx[target]inputidx=word_to_idx[input]output=model(torch.tensor(inputidx,dtype=torch.long))loss=criterion(output,torch.tensor(targetidx))optimizer.zero_grad()  # 清空梯度loss.backward()  # 反向传播optimizer.step()  # 更新参数loss_sum += loss.item()if (epoch+1) % 10 == 0:print("loss is ",loss_sum/len(data2),loss.item())

保存模型

torch.save(model.state_dict(),"skipgram.pth")

简单预测

inputidx=word_to_idx["refresh"]output=model(torch.tensor(inputidx,dtype=torch.long))
print(output.topk(4))
cc,index=output.topk(4)
idx_to_word[index[0].item()],idx_to_word[index[1].item()],idx_to_word[index[2].item()],idx_to_word[index[3].item()]def predict(centerword):inputidx=word_to_idx[centerword]output=model(torch.tensor(inputidx,dtype=torch.long))print(output.topk(4))cc,index=output.topk(4)idx_to_word[index[0].item()],idx_to_word[index[1].item()],idx_to_word[index[2].item()],idx_to_word[index[3].item()]

获取训练后的词向量

trained_vector_dic={}
for word, idx in word_to_idx.items(): # 输出每个词的嵌入向量trained_vector_dic[word]=model.embedding.weight[idx]
trained_vector_dic

画图看下分布

fig, ax = plt.subplots() 
for word, idx in word_to_idx.items():# 获取每个单词的嵌入向量vec = model.embedding.weight[:,idx].detach().numpy() ax.scatter(vec[0], vec[1]) # 在图中绘制嵌入向量的点ax.annotate(word, (vec[0], vec[1]), fontsize=12) # 点旁添加单词标签
plt.title(' 二维词嵌入 ') # 图题
plt.xlabel(' 向量维度 1') # X 轴 Label
plt.ylabel(' 向量维度 2') # Y 轴 Label
plt.show() # 显示图

利用词向量计算相似度

余弦

# https://blog.csdn.net/qq_41487299/article/details/106299882
import torch
import torch.nn.functional as F# 计算余弦相似度
cosine_similarity = F.cosine_similarity(x.unsqueeze(0), y.unsqueeze(0))print(cosine_similarity)cosine_similarity1 = F.cosine_similarity(torch.tensor(trained_vector_dic["保持数据"].unsqueeze(0)), torch.tensor(trained_vector_dic["打印信息"]).unsqueeze(0))
print(cosine_similarity1)

点积

dot_product = torch.dot(torch.tensor(trained_vector_dic["保持数据"]), torch.tensor(trained_vector_dic["打印信息"]))
x_length = torch.norm(torch.tensor(trained_vector_dic["保持数据"]))
y_length = torch.norm(torch.tensor(trained_vector_dic["打印信息"]))
similarity = dot_product / (x_length * y_length)print(similarity)
torch.tensor(trained_vector_dic["参数值"]),len(trained_vector_dic)
c1=cos(trained_vector_dic["删除"],trained_vector_dic["服务"])
print(c1)

这篇关于python-pytorch实现skip-gram 0.5.000【直接可运行】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/890752

相关文章

一文教你Python如何快速精准抓取网页数据

《一文教你Python如何快速精准抓取网页数据》这篇文章主要为大家详细介绍了如何利用Python实现快速精准抓取网页数据,文中的示例代码简洁易懂,具有一定的借鉴价值,有需要的小伙伴可以了解下... 目录1. 准备工作2. 基础爬虫实现3. 高级功能扩展3.1 抓取文章详情3.2 保存数据到文件4. 完整示例

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

基于Python打造一个智能单词管理神器

《基于Python打造一个智能单词管理神器》这篇文章主要为大家详细介绍了如何使用Python打造一个智能单词管理神器,从查询到导出的一站式解决,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 项目概述:为什么需要这个工具2. 环境搭建与快速入门2.1 环境要求2.2 首次运行配置3. 核心功能使用指

Python实现微信自动锁定工具

《Python实现微信自动锁定工具》在数字化办公时代,微信已成为职场沟通的重要工具,但临时离开时忘记锁屏可能导致敏感信息泄露,下面我们就来看看如何使用Python打造一个微信自动锁定工具吧... 目录引言:当微信隐私遇到自动化守护效果展示核心功能全景图技术亮点深度解析1. 无操作检测引擎2. 微信路径智能获

Python中pywin32 常用窗口操作的实现

《Python中pywin32常用窗口操作的实现》本文主要介绍了Python中pywin32常用窗口操作的实现,pywin32主要的作用是供Python开发者快速调用WindowsAPI的一个... 目录获取窗口句柄获取最前端窗口句柄获取指定坐标处的窗口根据窗口的完整标题匹配获取句柄根据窗口的类别匹配获取句

利用Python打造一个Excel记账模板

《利用Python打造一个Excel记账模板》这篇文章主要为大家详细介绍了如何使用Python打造一个超实用的Excel记账模板,可以帮助大家高效管理财务,迈向财富自由之路,感兴趣的小伙伴快跟随小编一... 目录设置预算百分比超支标红预警记账模板功能介绍基础记账预算管理可视化分析摸鱼时间理财法碎片时间利用财

在 Spring Boot 中实现异常处理最佳实践

《在SpringBoot中实现异常处理最佳实践》本文介绍如何在SpringBoot中实现异常处理,涵盖核心概念、实现方法、与先前查询的集成、性能分析、常见问题和最佳实践,感兴趣的朋友一起看看吧... 目录一、Spring Boot 异常处理的背景与核心概念1.1 为什么需要异常处理?1.2 Spring B

Python中的Walrus运算符分析示例详解

《Python中的Walrus运算符分析示例详解》Python中的Walrus运算符(:=)是Python3.8引入的一个新特性,允许在表达式中同时赋值和返回值,它的核心作用是减少重复计算,提升代码简... 目录1. 在循环中避免重复计算2. 在条件判断中同时赋值变量3. 在列表推导式或字典推导式中简化逻辑

判断PyTorch是GPU版还是CPU版的方法小结

《判断PyTorch是GPU版还是CPU版的方法小结》PyTorch作为当前最流行的深度学习框架之一,支持在CPU和GPU(NVIDIACUDA)上运行,所以对于深度学习开发者来说,正确识别PyTor... 目录前言为什么需要区分GPU和CPU版本?性能差异硬件要求如何检查PyTorch版本?方法1:使用命

python处理带有时区的日期和时间数据

《python处理带有时区的日期和时间数据》这篇文章主要为大家详细介绍了如何在Python中使用pytz库处理时区信息,包括获取当前UTC时间,转换为特定时区等,有需要的小伙伴可以参考一下... 目录时区基本信息python datetime使用timezonepandas处理时区数据知识延展时区基本信息