-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathencoder.py
54 lines (42 loc) · 1.6 KB
/
encoder.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
import torch
import torch.nn as nn
import torch.nn.functional as F
import time
class TextEncoder(nn.Module):
def __init__(self, channels, kernel_size, depth, dropout_rate, n_symbols):
super().__init__()
self.dropout_rate = dropout_rate
self.embedding = nn.Embedding(n_symbols, channels)
padding = (kernel_size - 1) // 2
self.cnn = list()
for _ in range(depth):
self.cnn.append(nn.Sequential(
nn.Conv1d(channels, channels, kernel_size=kernel_size, padding=padding),
nn.BatchNorm1d(channels),
nn.ReLU(),
nn.Dropout(dropout_rate),
))
self.cnn = nn.Sequential(*self.cnn)
self.lstm = nn.LSTM(channels, channels//2, 1, batch_first=True, bidirectional=True)
def forward(self, x, input_lengths):
x = self.embedding(x) # [B, T, emb]
x = x.transpose(1, 2) # [B, emb, T]
x = self.cnn(x) # [B, chn, T]
x = x.transpose(1, 2) # [B, T, chn]
input_lengths = input_lengths.cpu().numpy()
x = nn.utils.rnn.pack_padded_sequence(
x, input_lengths, batch_first=True)
self.lstm.flatten_parameters()
x, _ = self.lstm(x)
x, _ = nn.utils.rnn.pad_packed_sequence(
x, batch_first=True)
#x = F.dropout(x, self.dropout_rate, self.training)
return x
def inference(self, x):
x = self.embedding(x)
x = x.transpose(1, 2)
x = self.cnn(x)
x = x.transpose(1, 2)
self.lstm.flatten_parameters()
x, _ = self.lstm(x)
return x