-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3680c9f
commit a158fec
Showing
48 changed files
with
241 additions
and
68 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+40 Bytes
...e/20200930-H0749/test/events.out.tfevents.1601471269.minh-B365-M-AORUS-ELITE.8517.9893.v2
Binary file not shown.
Binary file added
BIN
+40 Bytes
.../20200930-H0749/train/events.out.tfevents.1601471269.minh-B365-M-AORUS-ELITE.8517.9885.v2
Binary file not shown.
Binary file added
BIN
+40 Bytes
...e/20200930-H0817/test/events.out.tfevents.1601471297.minh-B365-M-AORUS-ELITE.8610.9893.v2
Binary file not shown.
Binary file added
BIN
+40 Bytes
.../20200930-H0817/train/events.out.tfevents.1601471297.minh-B365-M-AORUS-ELITE.8610.9885.v2
Binary file not shown.
Binary file added
BIN
+40 Bytes
...e/20200930-H0955/test/events.out.tfevents.1601471395.minh-B365-M-AORUS-ELITE.8771.9893.v2
Binary file not shown.
Binary file added
BIN
+40 Bytes
.../20200930-H0955/train/events.out.tfevents.1601471395.minh-B365-M-AORUS-ELITE.8771.9885.v2
Binary file not shown.
Binary file added
BIN
+40 Bytes
...e/20200930-H1159/test/events.out.tfevents.1601478719.minh-B365-M-AORUS-ELITE.5694.9893.v2
Binary file not shown.
Binary file added
BIN
+40 Bytes
.../20200930-H1159/train/events.out.tfevents.1601478719.minh-B365-M-AORUS-ELITE.5694.9885.v2
Binary file not shown.
Binary file added
BIN
+40 Bytes
...e/20200930-H2928/test/events.out.tfevents.1601468968.minh-B365-M-AORUS-ELITE.6210.9893.v2
Binary file not shown.
Binary file added
BIN
+40 Bytes
.../20200930-H2928/train/events.out.tfevents.1601468968.minh-B365-M-AORUS-ELITE.6210.9885.v2
Binary file not shown.
Binary file added
BIN
+40 Bytes
...e/20200930-H3044/test/events.out.tfevents.1601469044.minh-B365-M-AORUS-ELITE.6429.9893.v2
Binary file not shown.
Binary file added
BIN
+40 Bytes
.../20200930-H3044/train/events.out.tfevents.1601469044.minh-B365-M-AORUS-ELITE.6429.9885.v2
Binary file not shown.
Binary file added
BIN
+40 Bytes
...e/20200930-H3100/test/events.out.tfevents.1601469060.minh-B365-M-AORUS-ELITE.6495.9893.v2
Binary file not shown.
Binary file added
BIN
+40 Bytes
.../20200930-H3100/train/events.out.tfevents.1601469060.minh-B365-M-AORUS-ELITE.6495.9885.v2
Binary file not shown.
Binary file added
BIN
+40 Bytes
...e/20200930-H3123/test/events.out.tfevents.1601469083.minh-B365-M-AORUS-ELITE.6566.9893.v2
Binary file not shown.
Binary file added
BIN
+40 Bytes
.../20200930-H3123/train/events.out.tfevents.1601469083.minh-B365-M-AORUS-ELITE.6566.9885.v2
Binary file not shown.
Binary file added
BIN
+40 Bytes
...e/20200930-H4018/test/events.out.tfevents.1601469618.minh-B365-M-AORUS-ELITE.7018.9893.v2
Binary file not shown.
Binary file added
BIN
+40 Bytes
.../20200930-H4018/train/events.out.tfevents.1601469618.minh-B365-M-AORUS-ELITE.7018.9885.v2
Binary file not shown.
Binary file added
BIN
+40 Bytes
...e/20200930-H4033/test/events.out.tfevents.1601469633.minh-B365-M-AORUS-ELITE.7084.9893.v2
Binary file not shown.
Binary file added
BIN
+40 Bytes
.../20200930-H4033/train/events.out.tfevents.1601469633.minh-B365-M-AORUS-ELITE.7084.9885.v2
Binary file not shown.
Binary file added
BIN
+40 Bytes
...e/20200930-H4056/test/events.out.tfevents.1601469656.minh-B365-M-AORUS-ELITE.7162.9893.v2
Binary file not shown.
Binary file added
BIN
+40 Bytes
.../20200930-H4056/train/events.out.tfevents.1601469656.minh-B365-M-AORUS-ELITE.7162.9885.v2
Binary file not shown.
Binary file added
BIN
+40 Bytes
...e/20200930-H4240/test/events.out.tfevents.1601469760.minh-B365-M-AORUS-ELITE.7294.9893.v2
Binary file not shown.
Binary file added
BIN
+40 Bytes
.../20200930-H4240/train/events.out.tfevents.1601469760.minh-B365-M-AORUS-ELITE.7294.9885.v2
Binary file not shown.
Binary file added
BIN
+40 Bytes
.../20200930-H4422/test/events.out.tfevents.1601477062.minh-B365-M-AORUS-ELITE.11214.9893.v2
Binary file not shown.
Binary file added
BIN
+40 Bytes
...20200930-H4422/train/events.out.tfevents.1601477062.minh-B365-M-AORUS-ELITE.11214.9885.v2
Binary file not shown.
Binary file added
BIN
+40 Bytes
.../20200930-H4457/test/events.out.tfevents.1601477097.minh-B365-M-AORUS-ELITE.11292.9893.v2
Binary file not shown.
Binary file added
BIN
+40 Bytes
...20200930-H4457/train/events.out.tfevents.1601477097.minh-B365-M-AORUS-ELITE.11292.9885.v2
Binary file not shown.
Binary file added
BIN
+40 Bytes
.../20200930-H4605/test/events.out.tfevents.1601477165.minh-B365-M-AORUS-ELITE.11366.9893.v2
Binary file not shown.
Binary file added
BIN
+40 Bytes
...20200930-H4605/train/events.out.tfevents.1601477165.minh-B365-M-AORUS-ELITE.11366.9885.v2
Binary file not shown.
Binary file added
BIN
+40 Bytes
...e/20200930-H4726/test/events.out.tfevents.1601470046.minh-B365-M-AORUS-ELITE.7607.9893.v2
Binary file not shown.
Binary file added
BIN
+40 Bytes
.../20200930-H4726/train/events.out.tfevents.1601470046.minh-B365-M-AORUS-ELITE.7607.9885.v2
Binary file not shown.
Binary file added
BIN
+40 Bytes
...e/20200930-H5041/test/events.out.tfevents.1601477441.minh-B365-M-AORUS-ELITE.2383.9893.v2
Binary file not shown.
Binary file added
BIN
+40 Bytes
.../20200930-H5041/train/events.out.tfevents.1601477441.minh-B365-M-AORUS-ELITE.2383.9885.v2
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
import tensorflow as tf | ||
|
||
def scaled_dot_product_attention(q, k, v, mask): | ||
"""Calculate the attention weights. | ||
q, k, v must have matching leading dimensions. | ||
k, v must have matching penultimate dimension, i.e.: seq_len_k = seq_len_v. | ||
The mask has different shapes depending on its type(padding or look ahead) | ||
but it must be broadcastable for addition. | ||
Args: | ||
q: query shape == (..., seq_len_q, depth) | ||
k: key shape == (..., seq_len_k, depth) | ||
v: value shape == (..., seq_len_v, depth_v) | ||
mask: Float tensor with shape broadcastable | ||
to (..., seq_len_q, seq_len_k). Defaults to None. | ||
Returns: | ||
output, attention_weights | ||
""" | ||
|
||
matmul_qk = tf.matmul(q, k, transpose_b=True) # (..., seq_len_q, seq_len_k) | ||
|
||
# scale matmul_qk | ||
dk = tf.cast(tf.shape(k)[-1], tf.float32) | ||
scaled_attention_logits = matmul_qk / tf.math.sqrt(dk) | ||
|
||
# add the mask to the scaled tensor. | ||
if mask is not None: | ||
scaled_attention_logits += (mask * -1e9) | ||
|
||
# softmax is normalized on the last axis (seq_len_k) so that the scores | ||
# add up to 1. | ||
attention_weights = tf.nn.softmax(scaled_attention_logits, axis=-1) # (..., seq_len_q, seq_len_k) | ||
|
||
output = tf.matmul(attention_weights, v) # (..., seq_len_q, depth_v) | ||
|
||
return output, attention_weights | ||
|
||
|
||
class MultiHeadAttention(tf.keras.layers.Layer): | ||
def __init__(self, d_model, num_heads): | ||
super(MultiHeadAttention, self).__init__() | ||
self.num_heads = num_heads | ||
self.d_model = d_model | ||
|
||
assert d_model % self.num_heads == 0 | ||
|
||
self.depth = d_model // self.num_heads | ||
|
||
self.wq = tf.keras.layers.Dense(d_model) | ||
self.wk = tf.keras.layers.Dense(d_model) | ||
self.wv = tf.keras.layers.Dense(d_model) | ||
|
||
self.dense = tf.keras.layers.Dense(d_model) | ||
|
||
def split_heads(self, x, batch_size): | ||
"""Split the last dimension into (num_heads, depth). | ||
Transpose the result such that the shape is (batch_size, num_heads, seq_len, depth) | ||
""" | ||
x = tf.reshape(x, (batch_size, -1, self.num_heads, self.depth)) | ||
return tf.transpose(x, perm=[0, 2, 1, 3]) | ||
|
||
def call(self, v, k, q, mask = None): | ||
batch_size = tf.shape(q)[0] | ||
|
||
q = self.wq(q) # (batch_size, seq_len, d_model) | ||
k = self.wk(k) # (batch_size, seq_len, d_model) | ||
v = self.wv(v) # (batch_size, seq_len, d_model) | ||
|
||
q = self.split_heads(q, batch_size) # (batch_size, num_heads, seq_len_q, depth) | ||
k = self.split_heads(k, batch_size) # (batch_size, num_heads, seq_len_k, depth) | ||
v = self.split_heads(v, batch_size) # (batch_size, num_heads, seq_len_v, depth) | ||
|
||
# scaled_attention.shape == (batch_size, num_heads, seq_len_q, depth) | ||
# attention_weights.shape == (batch_size, num_heads, seq_len_q, seq_len_k) | ||
scaled_attention, attention_weights = scaled_dot_product_attention( | ||
q, k, v, mask) | ||
|
||
scaled_attention = tf.transpose(scaled_attention, perm=[0, 2, 1, 3]) # (batch_size, seq_len_q, num_heads, depth) | ||
|
||
concat_attention = tf.reshape(scaled_attention, | ||
(batch_size, -1, self.d_model)) # (batch_size, seq_len_q, d_model) | ||
|
||
output = self.dense(concat_attention) # (batch_size, seq_len_q, d_model) | ||
|
||
return output, attention_weights | ||
|
||
def reset_state(self, batch_size): | ||
return tf.zeros((batch_size, self.units)) | ||
|
||
def create_padding_mask(seq): | ||
seq = tf.cast(tf.math.equal(seq, 0), tf.float32) | ||
|
||
# add extra dimensions to add the padding | ||
# to the attention logits. | ||
return seq[:, tf.newaxis, tf.newaxis, :] # (batch_size, 1, 1, seq_len) | ||
|
||
class FullyConnected(tf.keras.layers.Layer): | ||
def __init__(self, dmodel, dff = 2048): | ||
self.ffn = tf.keras.Sequential([ | ||
tf.keras.layers.Dense(dff, activation='relu'), # (batch_size, seq_len, dff) | ||
tf.keras.layers.Dense(d_model) # (batch_size, seq_len, d_model) | ||
]) | ||
def call(self, x ): | ||
return self.ffn(x) | ||
|
||
|
||
class TransformerLayer(tf.keras.layers.Layer): | ||
def __init__(self, d_model, num_heads, dff, rate=0.1, with_external = False): | ||
super(TransformerLayer, self).__init__() | ||
|
||
self.mha = MultiHeadAttention(d_model, num_heads) | ||
self.ffn = FullyConnected(d_model, dff) | ||
|
||
self.layernorm1 = tf.keras.layers.LayerNormalization(epsilon=1e-6) | ||
self.layernorm2 = tf.keras.layers.LayerNormalization(epsilon=1e-6) | ||
|
||
self.dropout1 = tf.keras.layers.Dropout(rate) | ||
self.dropout2 = tf.keras.layers.Dropout(rate) | ||
|
||
self.with_external = with_external | ||
# if self.with_external: | ||
|
||
def call(self, x, training, mask): | ||
attn_output, _ = self.mha(x, x, x, mask) # (batch_size, input_seq_len, d_model) | ||
attn_output = self.dropout1(attn_output, training=training) | ||
out1 = self.layernorm1(x + attn_output) # (batch_size, input_seq_len, d_model) | ||
|
||
ffn_output = self.ffn(out1) # (batch_size, input_seq_len, d_model) | ||
ffn_output = self.dropout2(ffn_output, training=training) | ||
out2 = self.layernorm2(out1 + ffn_output) # (batch_size, input_seq_len, d_model) | ||
|
||
return out2 | ||
|
||
class Transformer(tf.keras.layers.Layer): | ||
def __init__(self, num_layers, d_model, num_heads, dff, rate=0.1): | ||
self.enc_layers = [TransformerLayer(d_model, num_heads, dff, rate) | ||
for _ in range(num_layers)] | ||
self.enc_layers = [TransformerLayer(d_model, num_heads, dff, rate, with_external=Transformer) | ||
for _ in range(num_layers)] | ||
|
Oops, something went wrong.