自然语言处理基本概念

自然语言处理基本概念

所有学习循环神经网络的人都是看这一篇博客长大的:
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)
相关推荐
兆。8 分钟前
python全栈-人工智能基础-机器学习
人工智能·python·机器学习
魔镜前的帅比16 分钟前
Few-shot / Chain-of-Thought 提示技巧
人工智能·chatgpt
深度学习lover26 分钟前
<项目代码>yolo遥感航拍船舶识别<目标检测>
人工智能·python·yolo·目标检测·计算机视觉·遥感船舶识别
Coovally AI模型快速验证30 分钟前
基于SimCLR的自监督 YOLO:YOLOv5/8也能在低标注场景目标检测性能飙升
人工智能·科技·yolo·目标检测·机器学习·计算机视觉
不老刘1 小时前
新一代图像生成工具:Nano Banana Pro 带来更自然的创作体验
人工智能·google·gemini·nano banana pro
袁庭新1 小时前
人人都能学AI,人人都要学AI
人工智能·aigc
Tzarevich1 小时前
前端调用大语言模型:基于 Vite 的工程化实践与 HTTP 请求详解
人工智能
Soonyang Zhang1 小时前
MoeDistributeDispatch算子代码阅读
人工智能·算子·ascendc
sanggou1 小时前
Windsurf AI IDE 完全使用指南
ide·人工智能