-
Notifications
You must be signed in to change notification settings - Fork 2
/
trans.py
168 lines (132 loc) · 4.91 KB
/
trans.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
from tensorflow.keras.layers import Add, Dense, Dropout, MultiHeadAttention, LayerNormalization, Layer, Normalization
from tensorflow.keras.optimizers import Adam
from tensorflow.keras import Model
from tensorflow.keras.initializers import TruncatedNormal
from tensorflow.keras.callbacks import EarlyStopping, LearningRateScheduler, Callback
#from wandb.keras import WandbCallback
from sklearn.model_selection import train_test_split
import math
import wandb
import numpy as np
import pandas as pd
import tensorflow as tf
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.utils import resample
from scipy import stats
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
class PositionalEmbedding(Layer):
def __init__(self, units, dropout_rate, **kwargs):
super(PositionalEmbedding, self).__init__(**kwargs)
self.units = units
self.projection = Dense(units, kernel_initializer=TruncatedNormal(stddev=0.02))
self.dropout = Dropout(rate=dropout_rate)
def build(self, input_shape):
super(PositionalEmbedding, self).build(input_shape)
self.position = self.add_weight(
name="position",
shape=(1, input_shape[1], self.units),
initializer=TruncatedNormal(stddev=0.02),
trainable=True,
)
def call(self, inputs, training):
x = self.projection(inputs)
x = x + self.position
return self.dropout(x, training=training)
class Encoder(Layer):
def __init__(
self, embed_dim, mlp_dim, num_heads, dropout_rate, attention_dropout_rate, **kwargs
):
super(Encoder, self).__init__(**kwargs)
self.mha = MultiHeadAttention(
num_heads=num_heads,
key_dim=embed_dim,
dropout=attention_dropout_rate,
kernel_initializer=TruncatedNormal(stddev=0.02),
)
self.dense_0 = Dense(
units=mlp_dim,
activation="softmax",
kernel_initializer=TruncatedNormal(stddev=0.02),
)
self.dense_1 = Dense(
units=embed_dim, kernel_initializer=TruncatedNormal(stddev=0.02)
)
self.dropout_0 = Dropout(rate=dropout_rate)
self.dropout_1 = Dropout(rate=dropout_rate)
self.norm_0 = LayerNormalization(epsilon=1e-5)
self.norm_1 = LayerNormalization(epsilon=1e-5)
self.add_0 = Add()
self.add_1 = Add()
def call(self, inputs, training):
# Attention block
x = self.norm_0(inputs)
x = self.mha(
query=x,
value=x,
key=x,
training=training,
)
x = self.dropout_0(x, training=training)
x = self.add_0([x, inputs])
# MLP block
y = self.norm_1(x)
y = self.dense_0(y)
y = self.dense_1(y)
y = self.dropout_1(y, training=training)
return self.add_1([x, y])
class Transformer(Model):
def __init__(
self,
num_layers,
embed_dim,
mlp_dim,
num_heads,
num_classes,
dropout_rate,
attention_dropout_rate,
**kwargs
):
super(Transformer, self).__init__(**kwargs)
# Input (normalization of RAW measurements)
self.input_norm = Normalization()
# Input
self.pos_embs = PositionalEmbedding(embed_dim, dropout_rate)
# Encoder
self.e_layers = [
Encoder(embed_dim, mlp_dim, num_heads, dropout_rate, attention_dropout_rate)
for _ in range(num_layers)
]
# Output
self.norm = LayerNormalization(epsilon=1e-5)
self.final_layer = Dense(num_classes, kernel_initializer="zeros")
def call(self, inputs, training):
x = self.input_norm(inputs)
x = self.pos_embs(x, training=training)
for layer in self.e_layers:
x = layer(x, training=training)
x = self.norm(x)
x = self.final_layer(x)
return x
def smoothed_sparse_categorical_crossentropy(label_smoothing: float = 0.0):
def loss_fn(y_true, y_pred):
num_classes = tf.shape(y_pred)[-1]
y_true = tf.one_hot(y_true, num_classes)
loss = tf.keras.losses.categorical_crossentropy(y_true, y_pred, from_logits=True, label_smoothing=label_smoothing)
return tf.reduce_mean(loss)
return loss_fn
def cosine_schedule(base_lr, total_steps, warmup_steps):
def step_fn(epoch):
lr = base_lr
epoch += 1
progress = (epoch - warmup_steps) / float(total_steps - warmup_steps)
progress = tf.clip_by_value(progress, 0.0, 1.0)
lr = lr * 0.5 * (1.0 + tf.cos(math.pi * progress))
if warmup_steps:
lr = lr * tf.minimum(1.0, epoch / warmup_steps)
return lr
return step_fn
class PrintLR(Callback):
def on_epoch_end(self, epoch, logs=None):
wandb.log({"lr": self.model.optimizer.lr.numpy()}, commit=False)