近期利用零散的时间,系统的学习下 nlp经典的模型,主要是看了韩国一个小哥哥的github开源代码,写的很好,大家有时间 可以去看看,美中不足的是没有注释和说明,这里我主要是参考他的代码,自己学习学习,并顺便把nlp的零散的基础知识补充一下
基础知识回顾:
一. CNN网络
二. textCNN网络
三.代码展示:
import torch
import torch.nn as nn
import torch.optim as optim
import pdb
import torch.nn.functional as F
import numpy as np
### textCNN 文本分类
num_class = 2 ## 二分类
embedding_size = 3
class TextCNN(nn.Module):
def __init__(self,num_channels = 1) -> None: ## input [6,1,3,3]
super(TextCNN,self).__init__()
self.embed = nn.Embedding(voc_size, embedding_size)
self.conv1 = nn.Conv2d(num_channels,5,1,1) # num_channel = 1 表示通道数, 5 表示 filter kernel 的数量,滤波器的数量
### 1 -> filter的大小 1*1 的大小, 1 -> stride 步子的大小
self.conv2 = nn.Conv2d(5,5,1,1) ## input = [6,5,3,3] batch = 6, channel_num = 5,filter_size =1, stride = 1
self.fc = nn.Linear(5*3*3,num_class)
def forward(self,X):
embedded = self.embed(X) ## X [6,3],embedded = [6,3,3]
X = embedded.unsqueeze(1) ## X = [6,1,3,3]
out1 = self.conv1(X) ## X -> [6,1,3,3] out1 -> [6,5,3,3] 3 = (3-1)/stride + 1
y1 = F.relu(out1) ## y -> [6,5,3,3]
y1 = F.max_pool2d(y1,1,1)
out2 = self.conv2(y1) ## out2 = [6,5,3,3]
y2 = F.relu(out2) ## y -> [6,5,3,3]
y2 = F.max_pool2d(y2,1,1)
y2 = y2.view(-1,5*3*3)
out = self.fc(y2)
return out
if __name__ == '__main__':
sentences = ["i love you", "he loves me", "she likes baseball", "i hate you", "sorry for that", "this is awful"]
labels = [1, 1, 1, 0, 0, 0]
word_list = ' '.join(sentences).split()
word_list = list(set(word_list))
word_dict = {w:i for i,w in enumerate(word_list)}
voc_size = len(word_list)
model = TextCNN()
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
inputs = torch.LongTensor([np.asarray([word_dict[n] for n in sen.split()]) for sen in sentences])
targets = torch.LongTensor([out for out in labels]) # To using Torch Softmax Loss function
# pdb.set_trace()
# Training
for epoch in range(5000):
optimizer.zero_grad()
output = model(inputs)
# output : [batch_size, num_classes], target_batch : [batch_size] (LongTensor, not one-hot)
loss = criterion(output, targets)
if (epoch + 1) % 200 == 0:
print('Epoch:', '%04d' % (epoch + 1), 'cost =', '{:.6f}'.format(loss))
loss.backward()
optimizer.step()
# Test
test_text = 'sorry hate you'
tests = [np.asarray([word_dict[n] for n in test_text.split()])]
test_batch = torch.LongTensor(tests)
# Predict
predict = model(test_batch).data.max(1, keepdim=True)[1]
if predict[0][0] == 0:
print(test_text,"is Bad Mean...")
else:
print(test_text,"is Good Mean!!")
|