MCPcopy Create free account
hub / github.com/Tencent/NeuralNLP-NeuralClassifier / forward

Method forward

model/classification/textvdcnn.py:94–139  ·  view source on GitHub ↗
(self, batch)

Source from the content-addressed store, hash-verified

92 param_group["lr"] = 0
93
94 def forward(self, batch):
95 def convolutional_block(inputs, num_layers, convs, batch_norms):
96 """Convolutional Block of VDCNN
97 Convolutional block contains 2 conv layers, and can be repeated
98 Temp Conv-->Batch Norm-->ReLU-->Temp Conv-->Batch Norm-->ReLU
99 """
100 hidden_layer = inputs
101 for i in range(0, num_layers):
102 batch_norm = batch_norms[i](convs[i](inputs))
103 hidden_layer = torch.nn.functional.relu(batch_norm)
104 return hidden_layer
105
106 if self.config.feature.feature_names[0] == "token":
107 embedding = self.token_embedding(
108 batch[cDataset.DOC_TOKEN].to(self.config.device))
109 else:
110 embedding = self.char_embedding(
111 batch[cDataset.DOC_CHAR].to(self.config.device))
112 embedding = embedding.transpose(1, 2)
113
114 # first conv layer (kernel_size=3, #feature_map=64)
115 first_conv = self.first_conv(embedding)
116 first_conv = torch.nn.functional.relu(first_conv)
117
118 # all convolutional blocks
119 conv_block = first_conv
120 for i in range(0, len(self.num_kernels)):
121 conv_block = convolutional_block(
122 conv_block,
123 num_layers=self.vdcnn_num_convs[self.vdcnn_depth][i],
124 convs=self.convs[i],
125 batch_norms=self.batch_norms[i])
126 if i < len(self.num_kernels) - 1:
127 # max-pooling with stride=2
128 pool = torch.nn.functional.max_pool1d(conv_block,
129 kernel_size=3, stride=2)
130 else:
131 # k-max-pooling
132 pool = torch.topk(conv_block, self.top_k)[0].view(
133 conv_block.size(0), -1)
134
135 pool_shape = int(np.prod(pool.size()[1:]))
136 doc_embedding = torch.reshape(pool, (-1, pool_shape))
137 fc1 = self.linear1(doc_embedding)
138 fc2 = self.linear2(fc1)
139 return self.dropout(self.linear(fc2))

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected