forked from melkor169/LeadSheetGen_Valence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aux_files.py
executable file
·225 lines (164 loc) · 8.17 KB
/
aux_files.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Sep 29 15:59:51 2020
"""
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
import numpy as np
class Encoder(tf.keras.layers.Layer):
def __init__(self, input_vocab_size, num_layers = 4, d_model = 512, num_heads = 8, dff = 2048, maximum_position_encoding = 10000, dropout = 0.0):
super(Encoder, self).__init__()
self.d_model = d_model
self.embedding = tf.keras.layers.Embedding(input_vocab_size, d_model, mask_zero=True)
self.pos = positional_encoding(maximum_position_encoding, d_model)
self.encoder_layers = [ EncoderLayer(d_model = d_model, num_heads = num_heads, dff = dff, dropout = dropout) for _ in range(num_layers)]
self.dropout = tf.keras.layers.Dropout(dropout)
def call(self, inputs, mask=None, training=None):
x = self.embedding(inputs)
# positional encoding
x *= tf.math.sqrt(tf.cast(self.d_model, tf.float32))
x += self.pos[: , :tf.shape(x)[1], :]
x = self.dropout(x, training=training)
#Encoder layer
embedding_mask = self.embedding.compute_mask(inputs)
for encoder_layer in self.encoder_layers:
x = encoder_layer(x, mask = embedding_mask)
return x
def compute_mask(self, inputs, mask=None):
return self.embedding.compute_mask(inputs)
class EncoderLayer(tf.keras.layers.Layer):
def __init__(self, d_model = 512, num_heads = 8, dff = 2048, dropout = 0.0):
super(EncoderLayer, self).__init__()
self.multi_head_attention = MultiHeadAttention(d_model, num_heads)
self.dropout_attention = tf.keras.layers.Dropout(dropout)
self.add_attention = tf.keras.layers.Add()
self.layer_norm_attention = tf.keras.layers.LayerNormalization(epsilon=1e-6)
self.dense1 = tf.keras.layers.Dense(dff, activation='relu')
self.dense2 = tf.keras.layers.Dense(d_model)
self.dropout_dense = tf.keras.layers.Dropout(dropout)
self.add_dense = tf.keras.layers.Add()
self.layer_norm_dense = tf.keras.layers.LayerNormalization(epsilon=1e-6)
def call(self, inputs, mask=None, training=None):
# print(mask)
attention = self.multi_head_attention([inputs,inputs,inputs], mask = [mask,mask])
attention = self.dropout_attention(attention, training = training)
x = self.add_attention([inputs , attention])
x = self.layer_norm_attention(x)
# x = inputs
## Feed Forward
dense = self.dense1(x)
dense = self.dense2(dense)
dense = self.dropout_dense(dense, training = training)
x = self.add_dense([x , dense])
x = self.layer_norm_dense(x)
return x
class Decoder(tf.keras.layers.Layer):
def __init__(self, target_vocab_size, num_layers = 4, d_model = 512, num_heads = 8, dff = 2048, maximum_position_encoding = 10000, dropout = 0.0):
super(Decoder, self).__init__()
self.d_model = d_model
self.embedding = tf.keras.layers.Embedding(target_vocab_size, d_model, mask_zero=True)
self.pos = positional_encoding(maximum_position_encoding, d_model)
self.decoder_layers = [ DecoderLayer(d_model = d_model, num_heads = num_heads, dff = dff, dropout = dropout) for _ in range(num_layers)]
self.dropout = tf.keras.layers.Dropout(dropout)
def call(self, inputs, mask=None, training=None):
x = self.embedding(inputs[0])
# positional encoding
x *= tf.math.sqrt(tf.cast(self.d_model, tf.float32))
x += self.pos[: , :tf.shape(x)[1], :]
x = self.dropout(x, training=training)
#Decoder layer
embedding_mask = self.embedding.compute_mask(inputs[0])
for decoder_layer in self.decoder_layers:
x = decoder_layer([x,inputs[1]], mask = [embedding_mask, mask])
return x
# Comment this out if you want to use the masked_loss()
def compute_mask(self, inputs, mask=None):
return self.embedding.compute_mask(inputs[0])
class DecoderLayer(tf.keras.layers.Layer):
def __init__(self, d_model = 512, num_heads = 8, dff = 2048, dropout = 0.0):
super(DecoderLayer, self).__init__()
self.multi_head_attention1 = MultiHeadAttention(d_model, num_heads, causal = True)
self.dropout_attention1 = tf.keras.layers.Dropout(dropout)
self.add_attention1 = tf.keras.layers.Add()
self.layer_norm_attention1 = tf.keras.layers.LayerNormalization(epsilon=1e-6)
self.multi_head_attention2 = MultiHeadAttention(d_model, num_heads)
self.dropout_attention2 = tf.keras.layers.Dropout(dropout)
self.add_attention2 = tf.keras.layers.Add()
self.layer_norm_attention2 = tf.keras.layers.LayerNormalization(epsilon=1e-6)
self.dense1 = tf.keras.layers.Dense(dff, activation='relu')
self.dense2 = tf.keras.layers.Dense(d_model)
self.dropout_dense = tf.keras.layers.Dropout(dropout)
self.add_dense = tf.keras.layers.Add()
self.layer_norm_dense = tf.keras.layers.LayerNormalization(epsilon=1e-6)
def call(self, inputs, mask=None, training=None):
# print(mask)
attention = self.multi_head_attention1([inputs[0],inputs[0],inputs[0]], mask = [mask[0],mask[0]])
attention = self.dropout_attention1(attention, training = training)
x = self.add_attention1([inputs[0] , attention])
x = self.layer_norm_attention1(x)
attention = self.multi_head_attention2([x, inputs[1],inputs[1]], mask = [mask[0],mask[1]])
attention = self.dropout_attention2(attention, training = training)
x = self.add_attention1([x , attention])
x = self.layer_norm_attention1(x)
## Feed Forward
dense = self.dense1(x)
dense = self.dense2(dense)
dense = self.dropout_dense(dense, training = training)
x = self.add_dense([x , dense])
x = self.layer_norm_dense(x)
return x
class MultiHeadAttention(tf.keras.layers.Layer):
def __init__(self, d_model = 512, num_heads = 8, causal=False, dropout=0.0):
super(MultiHeadAttention, self).__init__()
assert d_model % num_heads == 0
depth = d_model // num_heads
self.w_query = tf.keras.layers.Dense(d_model)
self.split_reshape_query = tf.keras.layers.Reshape((-1,num_heads,depth))
self.split_permute_query = tf.keras.layers.Permute((2,1,3))
self.w_value = tf.keras.layers.Dense(d_model)
self.split_reshape_value = tf.keras.layers.Reshape((-1,num_heads,depth))
self.split_permute_value = tf.keras.layers.Permute((2,1,3))
self.w_key = tf.keras.layers.Dense(d_model)
self.split_reshape_key = tf.keras.layers.Reshape((-1,num_heads,depth))
self.split_permute_key = tf.keras.layers.Permute((2,1,3))
self.attention = tf.keras.layers.Attention(causal=causal, dropout=dropout)
self.join_permute_attention = tf.keras.layers.Permute((2,1,3))
self.join_reshape_attention = tf.keras.layers.Reshape((-1,d_model))
self.dense = tf.keras.layers.Dense(d_model)
def call(self, inputs, mask=None, training=None):
q = inputs[0]
v = inputs[1]
k = inputs[2] if len(inputs) > 2 else v
query = self.w_query(q)
query = self.split_reshape_query(query)
query = self.split_permute_query(query)
value = self.w_value(v)
value = self.split_reshape_value(value)
value = self.split_permute_value(value)
key = self.w_key(k)
key = self.split_reshape_key(key)
key = self.split_permute_key(key)
if mask is not None:
if mask[0] is not None:
mask[0] = tf.keras.layers.Reshape((-1,1))(mask[0])
mask[0] = tf.keras.layers.Permute((2,1))(mask[0])
if mask[1] is not None:
mask[1] = tf.keras.layers.Reshape((-1,1))(mask[1])
mask[1] = tf.keras.layers.Permute((2,1))(mask[1])
attention = self.attention([query, value, key], mask=mask)
attention = self.join_permute_attention(attention)
attention = self.join_reshape_attention(attention)
x = self.dense(attention)
return x
def get_angles(pos, i, d_model):
angle_rates = 1 / np.power(10000, (2 * (i//2)) / np.float32(d_model))
return pos * angle_rates
def positional_encoding(position, d_model):
angle_rads = get_angles( np.arange(position)[:, np.newaxis], np.arange(d_model)[np.newaxis, :], d_model)
# apply sin to even indices in the array; 2i
angle_rads[:, 0::2] = np.sin(angle_rads[:, 0::2])
# apply cos to odd indices in the array; 2i+1
angle_rads[:, 1::2] = np.cos(angle_rads[:, 1::2])
pos_encoding = angle_rads[np.newaxis, ...]
return tf.cast(pos_encoding, dtype=tf.float32)