自然语言处理基本概念

自然语言处理基本概念

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

python 复制代码
import jieba
import torch
from torch import nn

s1 = "我吃饭了!"
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 x

model = 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)
相关推荐
weixin_513449961 分钟前
walk_these_ways项目学习记录第十篇(通过行为多样性 (MoB) 实现地形泛化)--从仿真到部署
人工智能·学习·算法
2501_948114244 分钟前
Claude Sonnet 4.6 深度评测:性能逼近 Opus、成本打骨折,附接入方案与选型指南
大数据·网络·人工智能·安全·架构
angleboy88 分钟前
【原创】如何WIN 10/11系统下解决YOLOv13训练异常的安装指南
人工智能·深度学习·yolo
kobesdu10 分钟前
ROS导航调参指南:机器人模型、TEB/DWA与Costmap全解析
人工智能·机器人·ros
沫儿笙10 分钟前
库卡焊接机器人混合气节气装置
人工智能·机器人
ZhuNian的学习乐园15 分钟前
LLM智能体调度:从ReAct到多智能体调度
人工智能·python·深度学习
沫儿笙15 分钟前
弧焊机器人节气装置
人工智能·机器人
小超同学你好16 分钟前
LangGraph 25. 实战:Agent资源优化怎么做?用 State 与条件边管理预算、取证与模型档位(附 SRE 分诊 demo)
人工智能·深度学习·语言模型
大公产经晚间消息25 分钟前
美团医药健康与鱼跃、海氏海诺等头部医疗器械品牌深化合作,开拓即时零售新主场
人工智能
xianluohuanxiang29 分钟前
高精度气象:极端天气一来,零售最先出问题的不是客流,而是补货体系和损失控制
开发语言·人工智能·深度学习·机器学习·零售