零基础入门NLP-TASK5基于深度学习的文本分类2

TextCNN

零基础入门NLP-TASK5基于深度学习的文本分类2_第1张图片
在这里插入图片描述

零基础入门NLP-TASK5基于深度学习的文本分类2_第2张图片

#模型搭建
self.filter_sizes = [2, 3, 4] # n-gram window
self.out_channel = 100
self.convs = nn.ModuleList([nn.Conv2d(1, self.out_channel, (filter_size, input_size), bias=True)
for filter_size in self.filter_sizes])
#前向传播
pooled_outputs = []
for i in range(len(self.filter_sizes)):
 	 filter_height = sent_len - self.filter_sizes[i] + 1
	 conv = self.convs[i](batch_embed)
	 hidden = F.relu(conv) # sen_num x out_channel x filter_height x 1
	 mp = nn.MaxPool2d((filter_height, 1)) # (filter_height, filter_width)
	 # sen_num x out_channel x 1 x 1 -> sen_num x out_channel
	 pooled = mp(hidden).reshape(sen_num, self.out_channel)
 
	 pooled_outputs.append(pooled)

TextRNN

#模型搭建
input_size = config.word_dims
self.word_lstm = LSTM(
	 input_size=input_size,
	 hidden_size=config.word_hidden_size,
	 num_layers=config.word_num_layers,
	 batch_first=True,
	 bidirectional=True,
	 dropout_in=config.dropout_input,
	 dropout_out=config.dropout_hidden,
)
#前向传播
hiddens, _ = self.word_lstm(batch_embed, batch_masks) # sent_len x sen_num x hidden*2
hiddens.transpose_(1, 0) # sen_num x sent_len x hidden*2
if self.training:
	 hiddens = drop_sequence_sharedmask(hiddens, self.dropout_mlp)

baseline

https://tianchi.aliyun.com/notebook-ai/detail?spm=5176.12586969.1002.12.64063dadfiXoX7&postId=118259

你可能感兴趣的:(零基础入门NLP新闻文本分类)