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编写一个git自动上传的脚本(打包成exe)

《基于Python编写一个git自动上传的脚本(打包成exe)》这篇文章主要为大家详细介绍了如何基于Python编写一个git自动上传的脚本并打包成exe,文中的示例代码讲解详细,感兴趣的小伙伴可以跟... 目录前言效果如下源码实现利用pyinstaller打包成exe利用ResourceHacker修改e

Python在二进制文件中进行数据搜索的实战指南

《Python在二进制文件中进行数据搜索的实战指南》在二进制文件中搜索特定数据是编程中常见的任务,尤其在日志分析、程序调试和二进制数据处理中尤为重要,下面我们就来看看如何使用Python实现这一功能吧... 目录简介1. 二进制文件搜索概述2. python二进制模式文件读取(rb)2.1 二进制模式与文本

Python中Tkinter GUI编程详细教程

《Python中TkinterGUI编程详细教程》Tkinter作为Python编程语言中构建GUI的一个重要组件,其教程对于任何希望将Python应用到实际编程中的开发者来说都是宝贵的资源,这篇文... 目录前言1. Tkinter 简介2. 第一个 Tkinter 程序3. 窗口和基础组件3.1 创建窗

基于C++的UDP网络通信系统设计与实现详解

《基于C++的UDP网络通信系统设计与实现详解》在网络编程领域,UDP作为一种无连接的传输层协议,以其高效、低延迟的特性在实时性要求高的应用场景中占据重要地位,下面我们就来看看如何从零开始构建一个完整... 目录前言一、UDP服务器UdpServer.hpp1.1 基本框架设计1.2 初始化函数Init详解

Java中Map的五种遍历方式实现与对比

《Java中Map的五种遍历方式实现与对比》其实Map遍历藏着多种玩法,有的优雅简洁,有的性能拉满,今天咱们盘一盘这些进阶偏基础的遍历方式,告别重复又臃肿的代码,感兴趣的小伙伴可以了解下... 目录一、先搞懂:Map遍历的核心目标二、几种遍历方式的对比1. 传统EntrySet遍历(最通用)2. Lambd

Django调用外部Python程序的完整项目实战

《Django调用外部Python程序的完整项目实战》Django是一个强大的PythonWeb框架,它的设计理念简洁优雅,:本文主要介绍Django调用外部Python程序的完整项目实战,文中通... 目录一、为什么 Django 需要调用外部 python 程序二、三种常见的调用方式方式 1:直接 im

Python字符串处理方法超全攻略

《Python字符串处理方法超全攻略》字符串可以看作多个字符的按照先后顺序组合,相当于就是序列结构,意味着可以对它进行遍历、切片,:本文主要介绍Python字符串处理方法的相关资料,文中通过代码介... 目录一、基础知识:字符串的“不可变”特性与创建方式二、常用操作:80%场景的“万能工具箱”三、格式化方法

springboot+redis实现订单过期(超时取消)功能的方法详解

《springboot+redis实现订单过期(超时取消)功能的方法详解》在SpringBoot中使用Redis实现订单过期(超时取消)功能,有多种成熟方案,本文为大家整理了几个详细方法,文中的示例代... 目录一、Redis键过期回调方案(推荐)1. 配置Redis监听器2. 监听键过期事件3. Redi

SpringBoot全局异常拦截与自定义错误页面实现过程解读

《SpringBoot全局异常拦截与自定义错误页面实现过程解读》本文介绍了SpringBoot中全局异常拦截与自定义错误页面的实现方法,包括异常的分类、SpringBoot默认异常处理机制、全局异常拦... 目录一、引言二、Spring Boot异常处理基础2.1 异常的分类2.2 Spring Boot默

基于SpringBoot实现分布式锁的三种方法

《基于SpringBoot实现分布式锁的三种方法》这篇文章主要为大家详细介绍了基于SpringBoot实现分布式锁的三种方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、基于Redis原生命令实现分布式锁1. 基础版Redis分布式锁2. 可重入锁实现二、使用Redisso