Created
January 21, 2022 05:57
-
-
Save eileen-code4fun/d273191792afc943abb546360e24d836 to your computer and use it in GitHub Desktop.
Translation Train
This file contains hidden or 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
import matplotlib.pyplot as plt | |
import numpy as np | |
def train(epochs, model, batch=64, shuffle=1000): | |
loss_fcn = tf.keras.losses.SparseCategoricalCrossentropy( | |
from_logits=True, | |
reduction=tf.keras.losses.Reduction.NONE) | |
opt = tf.keras.optimizers.Adam() | |
losses = [] | |
ds = dataset.shuffle(shuffle).batch(batch).cache() | |
for epoch in range(epochs): | |
epoch_losses = [] | |
for eng_text, spa_text in ds: | |
with tf.GradientTape() as tape: | |
logits, expected, mask = model(eng_text, spa_text) | |
loss = loss_fcn(expected, logits) | |
loss = tf.ragged.boolean_mask(loss, mask) | |
loss = tf.reduce_sum(loss) * (1. / batch) | |
epoch_losses.append(loss.numpy()) | |
grads = tape.gradient(loss, model.trainable_weights) | |
opt.apply_gradients(zip(grads, model.trainable_weights)) | |
losses.append(np.mean(epoch_losses)) | |
print('Trained epoch: {}; loss: {}'.format(epoch, losses[epoch])) | |
plt.plot(losses) | |
plt.xlabel('Epoch') | |
plt.ylabel('Losses') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment