-
Notifications
You must be signed in to change notification settings - Fork 15
/
models.py
186 lines (155 loc) · 6.8 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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# Copyright 2019 Hitachi, Ltd. (author: Yusuke Fujita)
# Modified by: Yexin Yang
# Licensed under the MIT license.
import numpy as np
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn import TransformerEncoder, TransformerEncoderLayer
from torch.optim.lr_scheduler import _LRScheduler
from torch.optim import Optimizer
class NoamScheduler(_LRScheduler):
"""
See https://arxiv.org/pdf/1706.03762.pdf
lrate = d_model**(-0.5) * \
min(step_num**(-0.5), step_num*warmup_steps**(-1.5))
Args:
d_model: int
The number of expected features in the encoder inputs.
warmup_steps: int
The number of steps to linearly increase the learning rate.
"""
def __init__(self, optimizer, d_model, warmup_steps, last_epoch=-1):
self.d_model = d_model
self.warmup_steps = warmup_steps
super(NoamScheduler, self).__init__(optimizer, last_epoch)
# the initial learning rate is set as step = 1
if self.last_epoch == -1:
for param_group, lr in zip(self.optimizer.param_groups, self.get_lr()):
param_group['lr'] = lr
self.last_epoch = 0
print(self.d_model)
def get_lr(self):
last_epoch = max(1, self.last_epoch)
scale = self.d_model ** (-0.5) * min(last_epoch ** (-0.5), last_epoch * self.warmup_steps ** (-1.5))
return [base_lr * scale for base_lr in self.base_lrs]
class TransformerModel(nn.Module):
def __init__(self, n_speakers, in_size, n_heads, n_units, n_layers, dim_feedforward=2048, dropout=0.5, has_pos=False):
""" Self-attention-based diarization model.
Args:
n_speakers (int): Number of speakers in recording
in_size (int): Dimension of input feature vector
n_heads (int): Number of attention heads
n_units (int): Number of units in a self-attention block
n_layers (int): Number of transformer-encoder layers
dropout (float): dropout ratio
"""
super(TransformerModel, self).__init__()
self.n_speakers = n_speakers
self.in_size = in_size
self.n_heads = n_heads
self.n_units = n_units
self.n_layers = n_layers
self.has_pos = has_pos
self.src_mask = None
self.encoder = nn.Linear(in_size, n_units)
self.encoder_norm = nn.LayerNorm(n_units)
if self.has_pos:
self.pos_encoder = PositionalEncoding(n_units, dropout)
encoder_layers = TransformerEncoderLayer(n_units, n_heads, dim_feedforward, dropout)
self.transformer_encoder = TransformerEncoder(encoder_layers, n_layers)
self.decoder = nn.Linear(n_units, n_speakers)
self.init_weights()
def _generate_square_subsequent_mask(self, sz):
mask = (torch.triu(torch.ones(sz, sz)) == 1).transpose(0, 1)
mask = mask.float().masked_fill(mask == 0, float('-inf')).masked_fill(mask == 1, float(0.0))
return mask
def init_weights(self):
initrange = 0.1
self.encoder.bias.data.zero_()
self.encoder.weight.data.uniform_(-initrange, initrange)
self.decoder.bias.data.zero_()
self.decoder.weight.data.uniform_(-initrange, initrange)
def forward(self, src, has_mask=False, activation=None):
if has_mask:
device = src.device
if self.src_mask is None or self.src_mask.size(0) != srz.size(1):
mask = self._generate_square_subsequent_mask(srz.size(1)).to(device)
self.src_mask = mask
else:
self.src_mask = None
ilens = [x.shape[0] for x in src]
src = nn.utils.rnn.pad_sequence(src, padding_value=-1, batch_first=True)
# src: (B, T, E)
src = self.encoder(src)
src = self.encoder_norm(src)
# src: (T, B, E)
src = src.transpose(0, 1)
if self.has_pos:
# src: (T, B, E)
src = self.pos_encoder(src)
# output: (T, B, E)
output = self.transformer_encoder(src, self.src_mask)
# output: (B, T, E)
output = output.transpose(0, 1)
# output: (B, T, C)
output = self.decoder(output)
if activation:
output = activation(output)
output = [out[:ilen] for out, ilen in zip(output, ilens)]
return output
def get_attention_weight(self, src):
# NOTE: NOT IMPLEMENTED CORRECTLY!!!
attn_weight = []
def hook(module, input, output):
# attn_output, attn_output_weights = multihead_attn(query, key, value)
# output[1] are the attention weights
attn_weight.append(output[1])
handles = []
for l in range(self.n_layers):
handles.append(self.transformer_encoder.layers[l].self_attn.register_forward_hook(hook))
self.eval()
with torch.no_grad():
self.forward(src)
for handle in handles:
handle.remove()
self.train()
return torch.stack(attn_weight)
class PositionalEncoding(nn.Module):
"""Inject some information about the relative or absolute position of the tokens
in the sequence. The positional encodings have the same dimension as
the embeddings, so that the two can be summed. Here, we use sine and cosine
functions of different frequencies.
.. math::
\text{PosEncoder}(pos, 2i) = sin(pos/10000^(2i/d_model))
\text{PosEncoder}(pos, 2i+1) = cos(pos/10000^(2i/d_model))
\text{where pos is the word position and i is the embed idx)
Args:
d_model: the embed dim (required).
dropout: the dropout value (default=0.1).
max_len: the max. length of the incoming sequence (default=5000).
Examples:
>>> pos_encoder = PositionalEncoding(d_model)
"""
def __init__(self, d_model, dropout=0.1, max_len=5000):
super(PositionalEncoding, self).__init__()
self.dropout = nn.Dropout(p=dropout)
pe = torch.zeros(max_len, d_model)
position = torch.arange(0, max_len, dtype=torch.float).unsqueeze(1)
div_term = torch.exp(torch.arange(0, d_model, 2).float() * (-math.log(10000.0) / d_model))
pe[:, 0::2] = torch.sin(position * div_term)
pe[:, 1::2] = torch.cos(position * div_term)
pe = pe.unsqueeze(0).transpose(0, 1)
self.register_buffer('pe', pe)
def forward(self, x):
# Add positional information to each time step of x
x = x + self.pe[:x.size(0), :]
return self.dropout(x)
if __name__ == "__main__":
import torch
model = TransformerModel(5, 40, 4, 512, 2, 0.1)
input = torch.randn(8, 500, 40)
print("Model output:", model(input).size())
print("Model attention:", model.get_attention_weight(input).size())
print("Model attention sum:", model.get_attention_weight(input)[0][0][0].sum())