-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasicTransformerModel.py
162 lines (137 loc) · 6.89 KB
/
basicTransformerModel.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
import torch
import torch.nn as nn
import torch.nn.functional as F
class MultiHeadAttention(nn.Module):
def __init__(self, d_model, num_heads):
super(MultiHeadAttention, self).__init__()
assert d_model % num_heads == 0
self.d_k = d_model // num_heads
self.num_heads = num_heads
self.linear_q = nn.Linear(d_model, d_model)
self.linear_k = nn.Linear(d_model, d_model)
self.linear_v = nn.Linear(d_model, d_model)
self.linear_out = nn.Linear(d_model, d_model)
def forward(self, query, key, value, mask=None):
batch_size = query.size(0)
# Linear projections
query = self.linear_q(query)
key = self.linear_k(key)
value = self.linear_v(value)
# Split into multiple heads
query = query.view(batch_size, -1, self.num_heads, self.d_k).transpose(1, 2)
key = key.view(batch_size, -1, self.num_heads, self.d_k).transpose(1, 2)
value = value.view(batch_size, -1, self.num_heads, self.d_k).transpose(1, 2)
# Attention mechanism
scores = torch.matmul(query, key.transpose(-2, -1)) / torch.sqrt(torch.tensor(self.d_k, dtype=torch.float32))
if mask is not None:
scores = scores.masked_fill(mask == 0, -1e9)
attention_weights = F.softmax(scores, dim=-1)
context = torch.matmul(attention_weights, value)
# Merge heads
context = context.transpose(1, 2).contiguous().view(batch_size, -1, self.num_heads * self.d_k)
# Final linear layer
output = self.linear_out(context)
return output, attention_weights
class PositionwiseFeedforward(nn.Module):
def __init__(self, d_model, d_ff):
super(PositionwiseFeedforward, self).__init__()
self.linear1 = nn.Linear(d_model, d_ff)
self.linear2 = nn.Linear(d_ff, d_model)
def forward(self, x):
x = F.relu(self.linear1(x))
x = self.linear2(x)
return x
class PositionalEncoding(nn.Module):
def __init__(self, d_model, max_len=5000):
super(PositionalEncoding, self).__init__()
self.dropout = nn.Dropout(p=0.1)
pe = torch.zeros(max_len, d_model)
position = torch.arange(0, max_len, dtype=torch.float32).unsqueeze(1)
div_term = torch.exp(torch.arange(0, d_model, 2, dtype=torch.float32) * (-torch.log(torch.tensor(10000.0)) / d_model))
pe[:, 0::2] = torch.sin(position * div_term)
pe[:, 1::2] = torch.cos(position * div_term)
pe = pe.unsqueeze(0)
self.register_buffer('pe', pe)
def forward(self, x):
x = x + self.pe[:, :x.size(1)]
return self.dropout(x)
class TransformerEncoderLayer(nn.Module):
def __init__(self, d_model, num_heads, d_ff, dropout=0.1):
super(TransformerEncoderLayer, self).__init__()
self.self_attn = MultiHeadAttention(d_model, num_heads)
self.ffn = PositionwiseFeedforward(d_model, d_ff)
self.layer_norm1 = nn.LayerNorm(d_model)
self.layer_norm2 = nn.LayerNorm(d_model)
self.dropout1 = nn.Dropout(dropout)
self.dropout2 = nn.Dropout(dropout)
def forward(self, x, mask):
# Multi-Head Self-Attention
attn_output, _ = self.self_attn(x, x, x, mask)
attn_output = self.dropout1(attn_output)
x = self.layer_norm1(x + attn_output)
# Position-wise Feedforward Network
ffn_output = self.ffn(x)
ffn_output = self.dropout2(ffn_output)
x = self.layer_norm2(x + ffn_output)
return x
class TransformerEncoder(nn.Module):
def __init__(self, num_layers, d_model, num_heads, d_ff, input_vocab_size, max_len, dropout=0.1):
super(TransformerEncoder, self).__init__()
self.embedding = nn.Embedding(input_vocab_size, d_model)
self.position_encoding = PositionalEncoding(d_model, max_len)
self.layers = nn.ModuleList([TransformerEncoderLayer(d_model, num_heads, d_ff, dropout) for _ in range(num_layers)])
def forward(self, src, mask):
x = self.embedding(src)
x = self.position_encoding(x)
for layer in self.layers:
x = layer(x, mask)
return x
class TransformerDecoderLayer(nn.Module):
def __init__(self, d_model, num_heads, d_ff, dropout=0.1):
super(TransformerDecoderLayer, self).__init__()
self.self_attn = MultiHeadAttention(d_model, num_heads)
self.enc_dec_attn = MultiHeadAttention(d_model, num_heads)
self.ffn = PositionwiseFeedforward(d_model, d_ff)
self.layer_norm1 = nn.LayerNorm(d_model)
self.layer_norm2 = nn.LayerNorm(d_model)
self.layer_norm3 = nn.LayerNorm(d_model)
self.dropout1 = nn.Dropout(dropout)
self.dropout2 = nn.Dropout(dropout)
self.dropout3 = nn.Dropout(dropout)
def forward(self, x, encoder_output, src_mask, tgt_mask):
# Masked Multi-Head Self-Attention
attn_output, _ = self.self_attn(x, x, x, tgt_mask)
attn_output = self.dropout1(attn_output)
x = self.layer_norm1(x + attn_output)
# Multi-Head Attention over Encoder Outputs
attn_output, _ = self.enc_dec_attn(x, encoder_output, encoder_output, src_mask)
attn_output = self.dropout2(attn_output)
x = self.layer_norm2(x + attn_output)
# Position-wise Feedforward Network
ffn_output = self.ffn(x)
ffn_output = self.dropout3(ffn_output)
x = self.layer_norm3(x + ffn_output)
return x
class TransformerDecoder(nn.Module):
def __init__(self, num_layers, d_model, num_heads, d_ff, output_vocab_size, max_len, dropout=0.1):
super(TransformerDecoder, self).__init__()
self.embedding = nn.Embedding(output_vocab_size, d_model)
self.position_encoding = PositionalEncoding(d_model, max_len)
self.layers = nn.ModuleList([TransformerDecoderLayer(d_model, num_heads, d_ff, dropout) for _ in range(num_layers)])
self.linear = nn.Linear(d_model, output_vocab_size)
def forward(self, tgt, encoder_output, src_mask, tgt_mask):
x = self.embedding(tgt)
x = self.position_encoding(x)
for layer in self.layers:
x = layer(x, encoder_output, src_mask, tgt_mask)
x = self.linear(x)
return x
class Transformer(nn.Module):
def __init__(self, num_layers, d_model, num_heads, d_ff, input_vocab_size, output_vocab_size, src_max_len, tgt_max_len, dropout=0.1):
super(Transformer, self).__init__()
self.encoder = TransformerEncoder(num_layers, d_model, num_heads, d_ff, input_vocab_size, src_max_len, dropout)
self.decoder = TransformerDecoder(num_layers, d_model, num_heads, d_ff, output_vocab_size, tgt_max_len, dropout)
def forward(self, src, tgt, src_mask, tgt_mask):
encoder_output = self.encoder(src, src_mask)
decoder_output = self.decoder(tgt, encoder_output, src_mask, tgt_mask)
return decoder_output