自然语言处理基本概念

ops/2024/10/18 12:20:55/

自然语言处理基本概念

所有学习循环神经网络的人都是看这一篇博客长大的:
https://colah.github.io/posts/2015-08-Understanding-LSTMs/

import jieba
import torch
from torch import nns1 = "我吃饭了!"
s2 = "今天天气很好!"
s3 = "这辆车很好看!"jieba.lcut(s3)
words = {word for sentence in [s1, s2, s3] for word in jieba.lcut(sentence)}
words.add("<UNK>")
words.add("<PAD>")
print(words)word2idx = {word: idx for idx, word in enumerate(words)}
idx2word = {idx: word for word, idx in word2idx.items()}
print(word2idx)
print(idx2word)idx1 = [word2idx.get(word, word2idx.get("<UNK>")) for word in jieba.lcut(s1)]
idx2 = [word2idx.get(word, word2idx.get("<UNK>")) for word in jieba.lcut(s2)]
idx3 = [word2idx.get(word, word2idx.get("<UNK>")) for word in jieba.lcut(s3)]
print(idx1,idx2,idx3)# 补 1 个 pad
idx1 += [word2idx.get("<PAD>")]
idx2 += [word2idx.get("<PAD>")]
print(idx1,idx2,idx3)# 转张量
X = torch.tensor(data=[idx1, idx2, idx3], dtype=torch.long).T
# [seq_len, batch_size]
print(X.shape)# word embedding
embed = nn.Embedding(num_embeddings=len(word2idx), embedding_dim=6)
print(len(word2idx))# [3, 5, 12] --> [3, 5, 6]
# [batch_size, seq_len, embedding_dim]
print(embed(X).shape)# [N, C, H, W]
# [N, Seq_len, Embedding_dim]
print(nn.RNN)# $h_t = \tanh(x_t W_{ih}^T + b_{ih} + h_{t-1}W_{hh}^T + b_{hh})$
rnn = nn.RNN(input_size=6, hidden_size=7, batch_first=False)
X1 = embed(X)
out, hn = rnn(X1)# 每一步的输出
print(out.shape)
# 最后一步的输出
print(hn.shape)print(out[-1, :, :])
print(hn)class Model(nn.Module):def __init__(self, dict_len=5000, embedding_dim=256, n_classes=2):super().__init__()# 嵌入:词向量self.embed = nn.Embedding(num_embeddings=dict_len,embedding_dim=embedding_dim)# 循环神经网络提取特征self.rnn = nn.RNN(input_size=embedding_dim,hidden_size=embedding_dim)# 转换输出self.out = nn.Linear(in_features=embedding_dim,out_features=n_classes)def forward(self, x):# [seq_len, batch_size] --> [seq_len, batch_size, embedding_dim]x = self.embed(x)# out: [seq_len, batch_size, embedding_dim]# hn: [1, batch_size, embedding_dim]out, hn = self.rnn(x)# [1, batch_size, embedding_dim] --> [batch_size, embedding_dim]x = torch.squeeze(input=hn, dim=0)# [batch_size, embedding_dim] --> [batch_size, n_classes]x = self.out(x)return xmodel = Model(dict_len=5000, embedding_dim=256, n_classes=2)
print(model)X = torch.randint(low=0, high=5000, size=(26, 3), dtype=torch.long)
# [seq_len, batch_size]
print(X.shape)# [batch_size, n_classes]
print(model(X).shape)

- 输出


http://www.ppmy.cn/ops/56656.html

相关文章

html转markdown nodejs实现

用到了jsdom库&#xff0c;直接现成处理html标签结构&#xff0c;只需要关心format格式化样式即可。 比较简易&#xff0c;待后续优化&#xff0c;目前只是短时间批量转换html文件。 const { JSDOM } require(jsdom);const getText (htmlString) > {if (!htmlString) re…

MySQL数字相关数据处理函数

目录 1. 随机数生成 rand ( ) 2. 四舍五入 round&#xff08;&#xff09; 3. 舍去 truncate ( ) 4. 向上/下取整 5. 空处理 ifnull&#xff08; x , y &#xff09; 1. 随机数生成 rand ( ) rand ( ) 生成 0 到 1 的随机数&#xff1b; rand ( x ) 生成 0 到 1 的随机数…

Nacos2.X源码分析:服务注册、服务发现流程

文章目录 Nacos2.1.X源码源码下载服务注册NacosClient端NacosServer端 服务发现NacosClient端NacosServer端 Nacos2.1.X源码 源码下载 源码下载地址 服务注册 官方文档&#xff0c;对于NamingService接口服务注册方法的说明 Nacos2.X 服务注册总流程图 NacosClient端 一个…

【排序算法】—— 快速排序

快速排序的原理是交换排序&#xff0c;其中qsort函数用的排序原理就是快速排序&#xff0c;它是一种效率较高的不稳定函数&#xff0c;时间复杂度为O(N*longN)&#xff0c;接下来就来学习一下快速排序。 一、快速排序思路 1.整体思路 以升序排序为例&#xff1a; (1)、首先随…

快速掌握 ==== js 正则表达式

git 地址 https://gitee.com/childe-jia/reg-test.git 背景 在日常开发中&#xff0c;我们经常会遇到使用正则表达式的场景&#xff0c;比如一些常见的表单校验&#xff0c;会让你匹配用户输入的手机号或者身份信息是否规范&#xff0c;这就可以用正则表达式去匹配。相信大多数…

Ubuntu开源软件LibreOffice将Excel多表转PDF多目录示例

一、实现的起因&#xff1a; Windows平台下&#xff0c;常见的WPS办公自动化套件中电子表格软件&#xff0c;其中具备将Excel工作表中数据转为PDF文档表格的功能。现在进一步的需求是&#xff1a;像PDF标准的电子书那样&#xff0c;具备一本书的目录结构或章节结构&#xff0c…

python工作中遇到的坑

1. 字典拷贝 有些场景下&#xff0c;需要对字典拷贝一个副本。这个副本用于保存原始数据&#xff0c;然后原来的字典去参与其他运算&#xff0c;或者作为参数传递给一些函数。 例如&#xff0c; >>> dict_a {"name": "John", "address&q…

暑假第一次作业

第一步&#xff1a;给R1,R2,R3,R4配IP [R1-GigabitEthernet0/0/0]ip address 192.168.1.1 24 [R1-Serial4/0/0]ip address 15.0.0.1 24 [R2-GigabitEthernet0/0/0]ip address 192.168.2.1 24 [R2-Serial4/0/0]ip address 25.0.0.1 24 [R3-GigabitEthernet0/0/0]ip address 192.…