Last active
October 15, 2019 02:37
-
-
Save bryanlimy/2cf6f05a06dc9283d3905f71813d6b1d to your computer and use it in GitHub Desktop.
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
def loss_function(y_true, y_pred): | |
y_true = tf.reshape(y_true, shape=(-1, MAX_LENGTH - 1)) | |
loss = tf.keras.losses.SparseCategoricalCrossentropy( | |
from_logits=True, reduction='none')(y_true, y_pred) | |
mask = tf.cast(tf.not_equal(y_true, 0), tf.float32) | |
loss = tf.multiply(loss, mask) | |
return tf.reduce_mean(loss) | |
class CustomSchedule(tf.keras.optimizers.schedules.LearningRateSchedule): | |
def __init__(self, d_model, warmup_steps=4000): | |
super(CustomSchedule, self).__init__() | |
self.d_model = d_model | |
self.d_model = tf.cast(self.d_model, tf.float32) | |
self.warmup_steps = warmup_steps | |
def __call__(self, step): | |
arg1 = tf.math.rsqrt(step) | |
arg2 = step * (self.warmup_steps**-1.5) | |
return tf.math.rsqrt(self.d_model) * tf.math.minimum(arg1, arg2) | |
learning_rate = CustomSchedule(D_MODEL) | |
optimizer = tf.keras.optimizers.Adam( | |
learning_rate, beta_1=0.9, beta_2=0.98, epsilon=1e-9) | |
def accuracy(y_true, y_pred): | |
# ensure labels have shape (batch_size, MAX_LENGTH - 1) | |
y_true = tf.reshape(y_true, shape=(-1, MAX_LENGTH - 1)) | |
accuracy = tf.metrics.SparseCategoricalAccuracy()(y_true, y_pred) | |
return accuracy | |
model.compile(optimizer=optimizer, loss=loss_function, metrics=[accuracy]) | |
EPOCHS = 20 | |
model.fit(dataset, epochs=EPOCHS) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment