forked from ZiJianZhao/SeqGAN-PyTorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
discriminator.py
46 lines (38 loc) · 1.61 KB
/
discriminator.py
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
# -*- coding: utf-8 -*-
import os
import random
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
class Discriminator(nn.Module):
"""A CNN for text classification
architecture: Embedding >> Convolution >> Max-pooling >> Softmax
"""
def __init__(self, num_classes, vocab_size, emb_dim, filter_sizes, num_filters, dropout):
super(Discriminator, self).__init__()
self.emb = nn.Embedding(vocab_size, emb_dim)
self.convs = nn.ModuleList([
nn.Conv2d(1, n, (f, emb_dim)) for (n, f) in zip(num_filters, filter_sizes)
])
self.highway = nn.Linear(sum(num_filters), sum(num_filters))
self.dropout = nn.Dropout(p=dropout)
self.lin = nn.Linear(sum(num_filters), num_classes)
self.softmax = nn.LogSoftmax()
self.init_parameters()
def forward(self, x):
"""
Args:
x: (batch_size * seq_len)
"""
emb = self.emb(x).unsqueeze(1) # batch_size * 1 * seq_len * emb_dim
convs = [F.relu(conv(emb)).squeeze(3) for conv in self.convs] # [batch_size * num_filter * length]
pools = [F.max_pool1d(conv, conv.size(2)).squeeze(2) for conv in convs] # [batch_size * num_filter]
pred = torch.cat(pools, 1) # batch_size * num_filters_sum
highway = self.highway(pred)
pred = torch.sigmoid(highway) * F.relu(highway) + (1. - torch.sigmoid(highway)) * pred
pred = self.softmax(self.lin(self.dropout(pred)))
return pred
def init_parameters(self):
for param in self.parameters():
param.data.uniform_(-0.05, 0.05)