File size: 4,932 Bytes
65bd8af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
187
188
from torch import nn
import numpy as np
import torch
import torch.nn.functional as F


def to_var(x):
    if torch.cuda.is_available():
        x = x.cuda()
    return x
   

    

    
class MultiHeadAttentionSequence(nn.Module):
    
    
    def __init__(self, n_head, d_model, d_k, d_v, dropout=0.1):
        
        super().__init__()
        
        self.n_head = n_head
        self.d_model = d_model
        self.d_k = d_k
        self.d_v = d_v

        self.W_Q = nn.Linear(d_model, n_head*d_k)
        self.W_K = nn.Linear(d_model, n_head*d_k)
        self.W_V = nn.Linear(d_model, n_head*d_v)
        self.W_O = nn.Linear(n_head*d_v, d_model)

        self.layer_norm = nn.LayerNorm(d_model)
        
        self.dropout = nn.Dropout(dropout)

    def forward(self, q, k, v):
        
        batch, len_q, _ = q.size()
        batch, len_k, _ = k.size()
        batch, len_v, _ = v.size()

        Q = self.W_Q(q).view([batch, len_q, self.n_head, self.d_k])
        K = self.W_K(k).view([batch, len_k, self.n_head, self.d_k])
        V = self.W_V(v).view([batch, len_v, self.n_head, self.d_v])

        Q = Q.transpose(1, 2)
        K = K.transpose(1, 2).transpose(2, 3)
        V = V.transpose(1, 2)

        attention = torch.matmul(Q, K)

        attention = attention / np.sqrt(self.d_k)

        attention = F.softmax(attention, dim=-1)
        
        output = torch.matmul(attention, V)

        output = output.transpose(1, 2).reshape([batch, len_q, self.d_v*self.n_head])
            
        output = self.W_O(output)

        output = self.dropout(output)
        
        output = self.layer_norm(output + q)
        
        return output, attention
        
class MultiHeadAttentionReciprocal(nn.Module):
    
    
    def __init__(self, n_head, d_model, d_k, d_v, dropout=0.1):
        
        super().__init__()
        
        self.n_head = n_head
        self.d_model = d_model
        self.d_k = d_k
        self.d_v = d_v
        
        
        self.W_Q = nn.Linear(d_model, n_head*d_k)
        self.W_K = nn.Linear(d_model, n_head*d_k)
        self.W_V = nn.Linear(d_model, n_head*d_v)
        self.W_O = nn.Linear(n_head*d_v, d_model)
        self.W_V_2 = nn.Linear(d_model, n_head*d_v)
        self.W_O_2 = nn.Linear(n_head*d_v, d_model)
        
        self.layer_norm = nn.LayerNorm(d_model)
        
        self.dropout = nn.Dropout(dropout)
        
        self.layer_norm_2 = nn.LayerNorm(d_model)
        
        self.dropout_2 = nn.Dropout(dropout)
    
    
 

    def forward(self, q, k, v, v_2):
        
        batch, len_q, _ = q.size()
        batch, len_k, _ = k.size()
        batch, len_v, _ = v.size()
        batch, len_v_2, _ = v_2.size()        
        
            
        Q = self.W_Q(q).view([batch, len_q, self.n_head, self.d_k])
        K = self.W_K(k).view([batch, len_k, self.n_head, self.d_k])
        V = self.W_V(v).view([batch, len_v, self.n_head, self.d_v])
        V_2 = self.W_V_2(v_2).view([batch, len_v_2, self.n_head, self.d_v])
        
        
           
        Q = Q.transpose(1, 2)
        K = K.transpose(1, 2).transpose(2, 3)
        V = V.transpose(1, 2)
        V_2 = V_2.transpose(1,2) 
        
        attention = torch.matmul(Q, K)
       
            
        attention = attention /np.sqrt(self.d_k)
        
        attention_2 = attention.transpose(-2, -1)
        
        
       
        attention = F.softmax(attention, dim=-1)
        
        attention_2 = F.softmax(attention_2, dim=-1)
    
        
        output = torch.matmul(attention, V)
        
        output_2 = torch.matmul(attention_2, V_2)
            
        output = output.transpose(1, 2).reshape([batch, len_q, self.d_v*self.n_head])
       
        output_2 = output_2.transpose(1, 2).reshape([batch, len_k, self.d_v*self.n_head])
            
        output = self.W_O(output)
        
        output_2 = self.W_O_2(output_2)
        
        output = self.dropout(output)
        
        output = self.layer_norm(output + q)
        
        output_2 = self.dropout(output_2)
        
        output_2 = self.layer_norm(output_2 + k)
        
        
        
       
        
        return output, output_2, attention, attention_2
        
    
class FFN(nn.Module):
    
    def __init__(self, d_in, d_hid, dropout=0.1):
        super().__init__()
        
        self.layer_1 = nn.Conv1d(d_in, d_hid,1)
        self.layer_2 = nn.Conv1d(d_hid, d_in,1)
        self.relu = nn.ReLU()
        self.layer_norm = nn.LayerNorm(d_in)
        
        self.dropout = nn.Dropout(dropout)
        
    def forward(self, x):
        
        residual = x 
        output = self.layer_1(x.transpose(1, 2))
        
        output = self.relu(output)
        
        output = self.layer_2(output)
        
        output = self.dropout(output)
        
        output = self.layer_norm(output.transpose(1, 2)+residual)
        
        return output