import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchtext.datasets import text_classification
from torchtext.data import Field, BucketIterator
class TextCNN(nn.Module):
def __init__(self, vocab_size, embedding_dim, output_dim, kernel_dim, kernel_sizes):
super().__init__()
# Embedding layer
self.embedding = nn.Embedding(vocab_size, embedding_dim)
# convolutional layers
self.convs = nn.ModuleList([
nn.Conv2d(1, kernel_dim, (k, embedding_dim))
for k in kernel_sizes
])
# dropout
self.dropout = nn.Dropout(0.5)
# final, fully-connected layer
self.fc = nn.Linear(len(kernel_sizes) * kernel_dim, output_dim)
def forward(self, text):
# text = [batch_size, sent_length]
embedded = self.embedding(text)
# embedded = [batch_size, sent_length, emb_dim]
embedded = embedded.unsqueeze(1)
# embedded = [batch_size, 1, sent_length, emb_dim]
conved = [F.relu(conv(embedded)).squeeze(3) for conv in self.convs]
# conved_n = [batch_size, kernel_dim, sent_length - kernel_size[n] + 1]
pooled = [F.max_pool1d(conv, conv.shape[2]).squeeze(2) for conv in conved]
# pooled_n = [batch_size, kernel_dim]
cat = self.dropout(torch.cat(pooled, dim=1))
# cat = [batch_size, kernel_dim * len(kernel_sizes)]
return self.fc(cat)
# return [batch_size, output_dim]
def main():
# Set up the fields for text and labels
TEXT = Field(lower=True, tokenize='spacy')
LABEL = Field(sequential=False, is_target=True)
# Use the AG_NEWS dataset for classification
train
Bert+BiLSTM+attention+TextCNN来实现文本分类
于 2022-12-15 22:33:36 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)