File size: 5,916 Bytes
cd37439
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# coding: UTF-8
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np


class Config(object):
    """配置参数"""

    def __init__(self, dataset, embedding):
        self.model_name = "DPCNN"
        self.train_path = dataset + "/data/train.txt"  # 训练集
        self.dev_path = dataset + "/data/dev.txt"  # 验证集
        self.test_path = dataset + "/data/test.txt"  # 测试集
        self.class_list = [
            x.strip()
            for x in open(dataset + "/data/class.txt", encoding="utf-8").readlines()
        ]  # 类别名单
        self.vocab_path = dataset + "/data/vocab.pkl"  # 词表
        self.save_path = (
            dataset + "/saved_dict/" + self.model_name + ".ckpt"
        )  # 模型训练结果
        self.log_path = dataset + "/log/" + self.model_name
        self.embedding_pretrained = (
            torch.tensor(
                np.load(dataset + "/data/" + embedding)["embeddings"].astype("float32")
            )
            if embedding != "random"
            else None
        )  # 预训练词向量
        self.device = torch.device(
            "cuda" if torch.cuda.is_available() else "cpu"
        )  # 设备

        self.dropout = 0.5  # 随机失活
        self.require_improvement = 1000  # 若超过1000batch效果还没提升,则提前结束训练
        self.num_classes = len(self.class_list)  # 类别数
        self.n_vocab = 0  # 词表大小,在运行时赋值
        self.num_epochs = 20  # epoch数
        self.batch_size = 128  # mini-batch大小
        self.pad_size = 32  # 每句话处理成的长度(短填长切)
        self.learning_rate = 1e-3  # 学习率
        self.embed = (
            self.embedding_pretrained.size(1)
            if self.embedding_pretrained is not None
            else 300
        )  # 字向量维度
        self.num_filters = 250  # 卷积核数量(channels数)


"""Deep Pyramid Convolutional Neural Networks for Text Categorization"""


class DPCNN(nn.Module):
    def __init__(self, config):
        super(DPCNN, self).__init__()
        if config.embedding_pretrained is not None:
            self.embedding = nn.Embedding.from_pretrained(
                config.embedding_pretrained, freeze=False
            )
        else:
            self.embedding = nn.Embedding(
                config.n_vocab, config.embed, padding_idx=config.n_vocab - 1
            )
        self.conv_region = nn.Conv2d(1, config.num_filters, (3, config.embed), stride=1)
        self.conv = nn.Conv2d(config.num_filters, config.num_filters, (3, 1), stride=1)
        self.max_pool = nn.MaxPool2d(kernel_size=(3, 1), stride=2)
        self.padding1 = nn.ZeroPad2d((0, 0, 1, 1))  # top bottom
        self.padding2 = nn.ZeroPad2d((0, 0, 0, 1))  # bottom
        self.relu = nn.ReLU()
        self.fc = nn.Linear(config.num_filters, config.num_classes)

    def forward(self, x):
        x = x[0]
        x = self.embedding(x)
        x = x.unsqueeze(1)  # [batch_size, 250, seq_len, 1]
        x = self.conv_region(x)  # [batch_size, 250, seq_len-3+1, 1]

        x = self.padding1(x)  # [batch_size, 250, seq_len, 1]
        x = self.relu(x)
        x = self.conv(x)  # [batch_size, 250, seq_len-3+1, 1]
        x = self.padding1(x)  # [batch_size, 250, seq_len, 1]
        x = self.relu(x)
        x = self.conv(x)  # [batch_size, 250, seq_len-3+1, 1]
        while x.size()[2] > 2:
            x = self._block(x)
        x = x.squeeze()  # [batch_size, num_filters(250)]
        x = self.fc(x)
        return x

    def _block(self, x):
        x = self.padding2(x)
        px = self.max_pool(x)

        x = self.padding1(px)
        x = F.relu(x)
        x = self.conv(x)

        x = self.padding1(x)
        x = F.relu(x)
        x = self.conv(x)

        # Short Cut
        x = x + px
        return x
    
    def feature(self, x):
        """
        提取中间层特征向量,用于可视化
        返回最终squeeze后的特征(全连接层前面的那一层)
        """
        with torch.no_grad():
            x = x[0]
            x = self.embedding(x)
            x = x.unsqueeze(1)  # [batch_size, 1, seq_len, embed]
            x = self.conv_region(x)  # [batch_size, num_filters, seq_len-3+1, 1]

            x = self.padding1(x)  # [batch_size, num_filters, seq_len, 1]
            x = self.relu(x)
            x = self.conv(x)  # [batch_size, num_filters, seq_len-3+1, 1]
            x = self.padding1(x)  # [batch_size, num_filters, seq_len, 1]
            x = self.relu(x)
            x = self.conv(x)  # [batch_size, num_filters, seq_len-3+1, 1]
            while x.size()[2] > 2:
                x = self._block(x)
            features = x.squeeze()  # [batch_size, num_filters(250)]
            return features.cpu().numpy()
    
    def get_prediction(self, x):
        """
        获取模型最终层输出向量(logits)
        """
        with torch.no_grad():
            x = x[0]
            x = self.embedding(x)
            x = x.unsqueeze(1)
            x = self.conv_region(x)

            x = self.padding1(x)
            x = self.relu(x)
            x = self.conv(x)
            x = self.padding1(x)
            x = self.relu(x)
            x = self.conv(x)
            while x.size()[2] > 2:
                x = self._block(x)
            x = x.squeeze()
            predictions = self.fc(x)  # [batch_size, num_classes]
            return predictions.cpu().numpy()
    
    def prediction(self, features):
        """
        根据中间特征向量预测结果
        features: 来自feature()函数的输出 [batch_size, num_filters]
        """
        with torch.no_grad():
            features_tensor = torch.tensor(features, dtype=torch.float32).to(next(self.parameters()).device)
            predictions = self.fc(features_tensor)  # 直接通过最后的分类层
            return predictions.cpu().numpy()