-
Notifications
You must be signed in to change notification settings - Fork 17
/
models.py
90 lines (83 loc) · 3.2 KB
/
models.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
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
# Copyright (C) 2018 Anvita Gupta
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License, version 3,
# as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import torch
from torch import optim
import torch.nn as nn
import torch.nn.functional as F
import torch.autograd as autograd
from torch.autograd import Variable
from utils.torch_utils import *
class ResBlock(nn.Module):
def __init__(self, hidden):
super(ResBlock, self).__init__()
self.res_block = nn.Sequential(
nn.ReLU(True),
nn.Conv1d(hidden, hidden, 5, padding=2),#nn.Linear(DIM, DIM),
nn.ReLU(True),
nn.Conv1d(hidden, hidden, 5, padding=2),#nn.Linear(DIM, DIM),
)
def forward(self, input):
output = self.res_block(input)
return input + (0.3*output)
class Generator_lang(nn.Module):
def __init__(self, n_chars, seq_len, batch_size, hidden):
super(Generator_lang, self).__init__()
self.fc1 = nn.Linear(128, hidden*seq_len)
self.block = nn.Sequential(
ResBlock(hidden),
ResBlock(hidden),
ResBlock(hidden),
ResBlock(hidden),
ResBlock(hidden),
)
self.conv1 = nn.Conv1d(hidden, n_chars, 1)
self.n_chars = n_chars
self.seq_len = seq_len
self.batch_size = batch_size
self.hidden = hidden
def forward(self, noise):
output = self.fc1(noise)
output = output.view(-1, self.hidden, self.seq_len) # (BATCH_SIZE, DIM, SEQ_LEN)
output = self.block(output)
output = self.conv1(output)
output = output.transpose(1, 2)
shape = output.size()
output = output.contiguous()
output = output.view(self.batch_size*self.seq_len, -1)
output = gumbel_softmax(output, 0.5)
return output.view(shape) # (BATCH_SIZE, SEQ_LEN, len(charmap))
class Discriminator_lang(nn.Module):
def __init__(self, n_chars, seq_len, batch_size, hidden):
super(Discriminator_lang, self).__init__()
self.n_chars = n_chars
self.seq_len = seq_len
self.batch_size = batch_size
self.hidden = hidden
self.block = nn.Sequential(
ResBlock(hidden),
ResBlock(hidden),
ResBlock(hidden),
ResBlock(hidden),
ResBlock(hidden),
)
self.conv1d = nn.Conv1d(n_chars, hidden, 1)
self.linear = nn.Linear(seq_len*hidden, 1)
def forward(self, input):
output = input.transpose(1, 2) # (BATCH_SIZE, len(charmap), SEQ_LEN)
output = self.conv1d(output)
output = self.block(output)
output = output.view(-1, self.seq_len*self.hidden)
output = self.linear(output)
return output