![]() | ![]() | ![]() | ![]() |
Este cuaderno entrena un modelo de secuencia a secuencia (seq2seq) para la traducción del español al inglés. Este es un ejemplo avanzado que asume cierto conocimiento de secuencia a modelos de secuencia.
Después de entrenar el modelo en este cuaderno, podrá ingresar una oración en español, como "¿todavia estan en casa?" , y devuelva la traducción al inglés: "¿todavía está en casa?"
La calidad de la traducción es razonable para un ejemplo de juguete, pero la trama de atención generada es quizás más interesante. Esto muestra qué partes de la oración de entrada atraen la atención del modelo mientras traduce:
import tensorflow as tf
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
from sklearn.model_selection import train_test_split
import unicodedata
import re
import numpy as np
import os
import io
import time
Descargue y prepare el conjunto de datos
Usaremos un conjunto de datos de idiomas proporcionado por http://www.manythings.org/anki/ Este conjunto de datos contiene pares de traducción de idiomas en el formato:
May I borrow this book? ¿Puedo tomar prestado este libro?
Hay una variedad de idiomas disponibles, pero usaremos el conjunto de datos inglés-español. Para mayor comodidad, hemos alojado una copia de este conjunto de datos en Google Cloud, pero también puede descargar su propia copia. Después de descargar el conjunto de datos, estos son los pasos que seguiremos para preparar los datos:
- Agrega una ficha de inicio y finalización a cada oración.
- Limpia las oraciones eliminando caracteres especiales.
- Cree un índice de palabras y un índice de palabras inverso (asignación de diccionarios de palabra → id e id → palabra).
- Rellena cada oración a una longitud máxima.
# Download the file
path_to_zip = tf.keras.utils.get_file(
'spa-eng.zip', origin='http://storage.googleapis.com/download.tensorflow.org/data/spa-eng.zip',
extract=True)
path_to_file = os.path.dirname(path_to_zip)+"/spa-eng/spa.txt"
Downloading data from http://storage.googleapis.com/download.tensorflow.org/data/spa-eng.zip 2646016/2638744 [==============================] - 0s 0us/step
# Converts the unicode file to ascii
def unicode_to_ascii(s):
return ''.join(c for c in unicodedata.normalize('NFD', s)
if unicodedata.category(c) != 'Mn')
def preprocess_sentence(w):
w = unicode_to_ascii(w.lower().strip())
# creating a space between a word and the punctuation following it
# eg: "he is a boy." => "he is a boy ."
# Reference:- https://stackoverflow.com/questions/3645931/python-padding-punctuation-with-white-spaces-keeping-punctuation
w = re.sub(r"([?.!,¿])", r" \1 ", w)
w = re.sub(r'[" "]+', " ", w)
# replacing everything with space except (a-z, A-Z, ".", "?", "!", ",")
w = re.sub(r"[^a-zA-Z?.!,¿]+", " ", w)
w = w.strip()
# adding a start and an end token to the sentence
# so that the model know when to start and stop predicting.
w = '<start> ' + w + ' <end>'
return w
en_sentence = u"May I borrow this book?"
sp_sentence = u"¿Puedo tomar prestado este libro?"
print(preprocess_sentence(en_sentence))
print(preprocess_sentence(sp_sentence).encode('utf-8'))
<start> may i borrow this book ? <end> b'<start> \xc2\xbf puedo tomar prestado este libro ? <end>'
# 1. Remove the accents
# 2. Clean the sentences
# 3. Return word pairs in the format: [ENGLISH, SPANISH]
def create_dataset(path, num_examples):
lines = io.open(path, encoding='UTF-8').read().strip().split('\n')
word_pairs = [[preprocess_sentence(w) for w in l.split('\t')] for l in lines[:num_examples]]
return zip(*word_pairs)
en, sp = create_dataset(path_to_file, None)
print(en[-1])
print(sp[-1])
<start> if you want to sound like a native speaker , you must be willing to practice saying the same sentence over and over in the same way that banjo players practice the same phrase over and over until they can play it correctly and at the desired tempo . <end> <start> si quieres sonar como un hablante nativo , debes estar dispuesto a practicar diciendo la misma frase una y otra vez de la misma manera en que un musico de banjo practica el mismo fraseo una y otra vez hasta que lo puedan tocar correctamente y en el tiempo esperado . <end>
def tokenize(lang):
lang_tokenizer = tf.keras.preprocessing.text.Tokenizer(
filters='')
lang_tokenizer.fit_on_texts(lang)
tensor = lang_tokenizer.texts_to_sequences(lang)
tensor = tf.keras.preprocessing.sequence.pad_sequences(tensor,
padding='post')
return tensor, lang_tokenizer
def load_dataset(path, num_examples=None):
# creating cleaned input, output pairs
targ_lang, inp_lang = create_dataset(path, num_examples)
input_tensor, inp_lang_tokenizer = tokenize(inp_lang)
target_tensor, targ_lang_tokenizer = tokenize(targ_lang)
return input_tensor, target_tensor, inp_lang_tokenizer, targ_lang_tokenizer
Limite el tamaño del conjunto de datos para experimentar más rápido (opcional)
La formación sobre el conjunto de datos completo de> 100.000 oraciones llevará mucho tiempo. Para entrenar más rápido, podemos limitar el tamaño del conjunto de datos a 30,000 oraciones (por supuesto, la calidad de la traducción se degrada con menos datos):
# Try experimenting with the size of that dataset
num_examples = 30000
input_tensor, target_tensor, inp_lang, targ_lang = load_dataset(path_to_file, num_examples)
# Calculate max_length of the target tensors
max_length_targ, max_length_inp = target_tensor.shape[1], input_tensor.shape[1]
# Creating training and validation sets using an 80-20 split
input_tensor_train, input_tensor_val, target_tensor_train, target_tensor_val = train_test_split(input_tensor, target_tensor, test_size=0.2)
# Show length
print(len(input_tensor_train), len(target_tensor_train), len(input_tensor_val), len(target_tensor_val))
24000 24000 6000 6000
def convert(lang, tensor):
for t in tensor:
if t!=0:
print ("%d ----> %s" % (t, lang.index_word[t]))
print ("Input Language; index to word mapping")
convert(inp_lang, input_tensor_train[0])
print ()
print ("Target Language; index to word mapping")
convert(targ_lang, target_tensor_train[0])
Input Language; index to word mapping 1 ----> <start> 6379 ----> dese 395 ----> vuelta 32 ----> , 22 ----> por 50 ----> favor 3 ----> . 2 ----> <end> Target Language; index to word mapping 1 ----> <start> 56 ----> please 205 ----> turn 197 ----> over 3 ----> . 2 ----> <end>
Crea un conjunto de datos tf.data
BUFFER_SIZE = len(input_tensor_train)
BATCH_SIZE = 64
steps_per_epoch = len(input_tensor_train)//BATCH_SIZE
embedding_dim = 256
units = 1024
vocab_inp_size = len(inp_lang.word_index)+1
vocab_tar_size = len(targ_lang.word_index)+1
dataset = tf.data.Dataset.from_tensor_slices((input_tensor_train, target_tensor_train)).shuffle(BUFFER_SIZE)
dataset = dataset.batch(BATCH_SIZE, drop_remainder=True)
example_input_batch, example_target_batch = next(iter(dataset))
example_input_batch.shape, example_target_batch.shape
(TensorShape([64, 16]), TensorShape([64, 11]))
Escribe el modelo de codificador y decodificador
Implementa un modelo de codificador-decodificador con atención, sobre el cual puedes leer en el tutorial de traducción automática neuronal de TensorFlow (seq2seq) . Este ejemplo utiliza un conjunto de API más reciente. Este cuaderno implementa las ecuaciones de atención del tutorial seq2seq. El siguiente diagrama muestra que a cada palabra de entrada se le asigna un peso por el mecanismo de atención que luego es utilizado por el decodificador para predecir la siguiente palabra en la oración. La siguiente imagen y fórmulas son un ejemplo del mecanismo de atención del artículo de Luong .
La entrada se pasa a través de un modelo de codificador que nos da la salida del codificador de forma (batch_size, max_length, hidden_size) y el estado de forma oculta del codificador (batch_size, hidden_size) .
Aquí están las ecuaciones que se implementan:
Este tutorial utiliza la atención de Bahdanau para el codificador. Decidamos la notación antes de escribir la forma simplificada:
- FC = Capa completamente conectada (densa)
- EO = Salida del codificador
- H = estado oculto
- X = entrada al decodificador
Y el pseudocódigo:
-
score = FC(tanh(FC(EO) + FC(H)))
-
attention weights = softmax(score, axis = 1)
. Softmax por defecto se aplica en el último eje pero aquí queremos aplicarlo en el 1er eje , ya que la forma de la puntuación es (batch_size, max_length, hidden_size) .Max_length
es la longitud de nuestra entrada. Dado que estamos tratando de asignar un peso a cada entrada, se debe aplicar softmax en ese eje. -
context vector = sum(attention weights * EO, axis = 1)
. La misma razón que la anterior para elegir el eje 1. -
embedding output
= La entrada al decodificador X se pasa a través de una capa de incrustación. -
merged vector = concat(embedding output, context vector)
- Este vector combinado luego se le da al GRU
Las formas de todos los vectores en cada paso se han especificado en los comentarios del código:
class Encoder(tf.keras.Model):
def __init__(self, vocab_size, embedding_dim, enc_units, batch_sz):
super(Encoder, self).__init__()
self.batch_sz = batch_sz
self.enc_units = enc_units
self.embedding = tf.keras.layers.Embedding(vocab_size, embedding_dim)
self.gru = tf.keras.layers.GRU(self.enc_units,
return_sequences=True,
return_state=True,
recurrent_initializer='glorot_uniform')
def call(self, x, hidden):
x = self.embedding(x)
output, state = self.gru(x, initial_state = hidden)
return output, state
def initialize_hidden_state(self):
return tf.zeros((self.batch_sz, self.enc_units))
encoder = Encoder(vocab_inp_size, embedding_dim, units, BATCH_SIZE)
# sample input
sample_hidden = encoder.initialize_hidden_state()
sample_output, sample_hidden = encoder(example_input_batch, sample_hidden)
print ('Encoder output shape: (batch size, sequence length, units) {}'.format(sample_output.shape))
print ('Encoder Hidden state shape: (batch size, units) {}'.format(sample_hidden.shape))
Encoder output shape: (batch size, sequence length, units) (64, 16, 1024) Encoder Hidden state shape: (batch size, units) (64, 1024)
class BahdanauAttention(tf.keras.layers.Layer):
def __init__(self, units):
super(BahdanauAttention, self).__init__()
self.W1 = tf.keras.layers.Dense(units)
self.W2 = tf.keras.layers.Dense(units)
self.V = tf.keras.layers.Dense(1)
def call(self, query, values):
# query hidden state shape == (batch_size, hidden size)
# query_with_time_axis shape == (batch_size, 1, hidden size)
# values shape == (batch_size, max_len, hidden size)
# we are doing this to broadcast addition along the time axis to calculate the score
query_with_time_axis = tf.expand_dims(query, 1)
# score shape == (batch_size, max_length, 1)
# we get 1 at the last axis because we are applying score to self.V
# the shape of the tensor before applying self.V is (batch_size, max_length, units)
score = self.V(tf.nn.tanh(
self.W1(query_with_time_axis) + self.W2(values)))
# attention_weights shape == (batch_size, max_length, 1)
attention_weights = tf.nn.softmax(score, axis=1)
# context_vector shape after sum == (batch_size, hidden_size)
context_vector = attention_weights * values
context_vector = tf.reduce_sum(context_vector, axis=1)
return context_vector, attention_weights
attention_layer = BahdanauAttention(10)
attention_result, attention_weights = attention_layer(sample_hidden, sample_output)
print("Attention result shape: (batch size, units) {}".format(attention_result.shape))
print("Attention weights shape: (batch_size, sequence_length, 1) {}".format(attention_weights.shape))
Attention result shape: (batch size, units) (64, 1024) Attention weights shape: (batch_size, sequence_length, 1) (64, 16, 1)
class Decoder(tf.keras.Model):
def __init__(self, vocab_size, embedding_dim, dec_units, batch_sz):
super(Decoder, self).__init__()
self.batch_sz = batch_sz
self.dec_units = dec_units
self.embedding = tf.keras.layers.Embedding(vocab_size, embedding_dim)
self.gru = tf.keras.layers.GRU(self.dec_units,
return_sequences=True,
return_state=True,
recurrent_initializer='glorot_uniform')
self.fc = tf.keras.layers.Dense(vocab_size)
# used for attention
self.attention = BahdanauAttention(self.dec_units)
def call(self, x, hidden, enc_output):
# enc_output shape == (batch_size, max_length, hidden_size)
context_vector, attention_weights = self.attention(hidden, enc_output)
# x shape after passing through embedding == (batch_size, 1, embedding_dim)
x = self.embedding(x)
# x shape after concatenation == (batch_size, 1, embedding_dim + hidden_size)
x = tf.concat([tf.expand_dims(context_vector, 1), x], axis=-1)
# passing the concatenated vector to the GRU
output, state = self.gru(x)
# output shape == (batch_size * 1, hidden_size)
output = tf.reshape(output, (-1, output.shape[2]))
# output shape == (batch_size, vocab)
x = self.fc(output)
return x, state, attention_weights
decoder = Decoder(vocab_tar_size, embedding_dim, units, BATCH_SIZE)
sample_decoder_output, _, _ = decoder(tf.random.uniform((BATCH_SIZE, 1)),
sample_hidden, sample_output)
print ('Decoder output shape: (batch_size, vocab size) {}'.format(sample_decoder_output.shape))
Decoder output shape: (batch_size, vocab size) (64, 4935)
Definir el optimizador y la función de pérdida.
optimizer = tf.keras.optimizers.Adam()
loss_object = tf.keras.losses.SparseCategoricalCrossentropy(
from_logits=True, reduction='none')
def loss_function(real, pred):
mask = tf.math.logical_not(tf.math.equal(real, 0))
loss_ = loss_object(real, pred)
mask = tf.cast(mask, dtype=loss_.dtype)
loss_ *= mask
return tf.reduce_mean(loss_)
Puntos de control (ahorro basado en objetos)
checkpoint_dir = './training_checkpoints'
checkpoint_prefix = os.path.join(checkpoint_dir, "ckpt")
checkpoint = tf.train.Checkpoint(optimizer=optimizer,
encoder=encoder,
decoder=decoder)
Capacitación
- Pase la entrada a través del codificador que devuelve la salida del codificador y el estado oculto del codificador .
- La salida del codificador, el estado oculto del codificador y la entrada del decodificador (que es el token de inicio ) se pasan al decodificador.
- El decodificador devuelve las predicciones y el estado oculto del decodificador .
- El estado oculto del decodificador se devuelve al modelo y las predicciones se utilizan para calcular la pérdida.
- Utilice la fuerza del maestro para decidir la siguiente entrada al decodificador.
- La imposición del profesor es la técnica en la que la palabra de destino se pasa como la siguiente entrada al decodificador.
- El paso final es calcular los gradientes y aplicarlo al optimizador y retropropagar.
@tf.function
def train_step(inp, targ, enc_hidden):
loss = 0
with tf.GradientTape() as tape:
enc_output, enc_hidden = encoder(inp, enc_hidden)
dec_hidden = enc_hidden
dec_input = tf.expand_dims([targ_lang.word_index['<start>']] * BATCH_SIZE, 1)
# Teacher forcing - feeding the target as the next input
for t in range(1, targ.shape[1]):
# passing enc_output to the decoder
predictions, dec_hidden, _ = decoder(dec_input, dec_hidden, enc_output)
loss += loss_function(targ[:, t], predictions)
# using teacher forcing
dec_input = tf.expand_dims(targ[:, t], 1)
batch_loss = (loss / int(targ.shape[1]))
variables = encoder.trainable_variables + decoder.trainable_variables
gradients = tape.gradient(loss, variables)
optimizer.apply_gradients(zip(gradients, variables))
return batch_loss
EPOCHS = 10
for epoch in range(EPOCHS):
start = time.time()
enc_hidden = encoder.initialize_hidden_state()
total_loss = 0
for (batch, (inp, targ)) in enumerate(dataset.take(steps_per_epoch)):
batch_loss = train_step(inp, targ, enc_hidden)
total_loss += batch_loss
if batch % 100 == 0:
print('Epoch {} Batch {} Loss {:.4f}'.format(epoch + 1,
batch,
batch_loss.numpy()))
# saving (checkpoint) the model every 2 epochs
if (epoch + 1) % 2 == 0:
checkpoint.save(file_prefix = checkpoint_prefix)
print('Epoch {} Loss {:.4f}'.format(epoch + 1,
total_loss / steps_per_epoch))
print('Time taken for 1 epoch {} sec\n'.format(time.time() - start))
Epoch 1 Batch 0 Loss 4.7113 Epoch 1 Batch 100 Loss 2.1051 Epoch 1 Batch 200 Loss 1.9095 Epoch 1 Batch 300 Loss 1.7646 Epoch 1 Loss 2.0334 Time taken for 1 epoch 26.513352870941162 sec Epoch 2 Batch 0 Loss 1.4994 Epoch 2 Batch 100 Loss 1.4381 Epoch 2 Batch 200 Loss 1.3774 Epoch 2 Batch 300 Loss 1.1783 Epoch 2 Loss 1.3686 Time taken for 1 epoch 15.74858546257019 sec Epoch 3 Batch 0 Loss 0.9827 Epoch 3 Batch 100 Loss 1.0305 Epoch 3 Batch 200 Loss 0.9073 Epoch 3 Batch 300 Loss 0.8466 Epoch 3 Loss 0.9339 Time taken for 1 epoch 15.360853910446167 sec Epoch 4 Batch 0 Loss 0.5953 Epoch 4 Batch 100 Loss 0.6024 Epoch 4 Batch 200 Loss 0.6550 Epoch 4 Batch 300 Loss 0.6959 Epoch 4 Loss 0.6273 Time taken for 1 epoch 15.659878015518188 sec Epoch 5 Batch 0 Loss 0.4362 Epoch 5 Batch 100 Loss 0.4403 Epoch 5 Batch 200 Loss 0.5202 Epoch 5 Batch 300 Loss 0.3749 Epoch 5 Loss 0.4293 Time taken for 1 epoch 15.344685077667236 sec Epoch 6 Batch 0 Loss 0.3615 Epoch 6 Batch 100 Loss 0.2462 Epoch 6 Batch 200 Loss 0.2649 Epoch 6 Batch 300 Loss 0.3645 Epoch 6 Loss 0.2965 Time taken for 1 epoch 15.627461910247803 sec Epoch 7 Batch 0 Loss 0.2720 Epoch 7 Batch 100 Loss 0.1868 Epoch 7 Batch 200 Loss 0.2354 Epoch 7 Batch 300 Loss 0.2372 Epoch 7 Loss 0.2145 Time taken for 1 epoch 15.387472867965698 sec Epoch 8 Batch 0 Loss 0.1477 Epoch 8 Batch 100 Loss 0.1718 Epoch 8 Batch 200 Loss 0.1659 Epoch 8 Batch 300 Loss 0.1612 Epoch 8 Loss 0.1623 Time taken for 1 epoch 15.627415657043457 sec Epoch 9 Batch 0 Loss 0.0871 Epoch 9 Batch 100 Loss 0.1062 Epoch 9 Batch 200 Loss 0.1450 Epoch 9 Batch 300 Loss 0.1639 Epoch 9 Loss 0.1268 Time taken for 1 epoch 15.357704162597656 sec Epoch 10 Batch 0 Loss 0.0960 Epoch 10 Batch 100 Loss 0.0805 Epoch 10 Batch 200 Loss 0.1251 Epoch 10 Batch 300 Loss 0.1206 Epoch 10 Loss 0.1037 Time taken for 1 epoch 15.646350383758545 sec
Traducir
- La función de evaluación es similar al ciclo de entrenamiento, excepto que aquí no usamos el forzado del maestro . La entrada al decodificador en cada paso de tiempo son sus predicciones previas junto con el estado oculto y la salida del codificador.
- Deje de predecir cuándo el modelo predice el token final .
- Y almacene los pesos de atención para cada paso de tiempo .
def evaluate(sentence):
attention_plot = np.zeros((max_length_targ, max_length_inp))
sentence = preprocess_sentence(sentence)
inputs = [inp_lang.word_index[i] for i in sentence.split(' ')]
inputs = tf.keras.preprocessing.sequence.pad_sequences([inputs],
maxlen=max_length_inp,
padding='post')
inputs = tf.convert_to_tensor(inputs)
result = ''
hidden = [tf.zeros((1, units))]
enc_out, enc_hidden = encoder(inputs, hidden)
dec_hidden = enc_hidden
dec_input = tf.expand_dims([targ_lang.word_index['<start>']], 0)
for t in range(max_length_targ):
predictions, dec_hidden, attention_weights = decoder(dec_input,
dec_hidden,
enc_out)
# storing the attention weights to plot later on
attention_weights = tf.reshape(attention_weights, (-1, ))
attention_plot[t] = attention_weights.numpy()
predicted_id = tf.argmax(predictions[0]).numpy()
result += targ_lang.index_word[predicted_id] + ' '
if targ_lang.index_word[predicted_id] == '<end>':
return result, sentence, attention_plot
# the predicted ID is fed back into the model
dec_input = tf.expand_dims([predicted_id], 0)
return result, sentence, attention_plot
# function for plotting the attention weights
def plot_attention(attention, sentence, predicted_sentence):
fig = plt.figure(figsize=(10,10))
ax = fig.add_subplot(1, 1, 1)
ax.matshow(attention, cmap='viridis')
fontdict = {'fontsize': 14}
ax.set_xticklabels([''] + sentence, fontdict=fontdict, rotation=90)
ax.set_yticklabels([''] + predicted_sentence, fontdict=fontdict)
ax.xaxis.set_major_locator(ticker.MultipleLocator(1))
ax.yaxis.set_major_locator(ticker.MultipleLocator(1))
plt.show()
def translate(sentence):
result, sentence, attention_plot = evaluate(sentence)
print('Input: %s' % (sentence))
print('Predicted translation: {}'.format(result))
attention_plot = attention_plot[:len(result.split(' ')), :len(sentence.split(' '))]
plot_attention(attention_plot, sentence.split(' '), result.split(' '))
Restaurar el último punto de control y prueba
# restoring the latest checkpoint in checkpoint_dir
checkpoint.restore(tf.train.latest_checkpoint(checkpoint_dir))
<tensorflow.python.training.tracking.util.CheckpointLoadStatus at 0x7f3f4a04af60>
translate(u'hace mucho frio aqui.')
Input: <start> hace mucho frio aqui . <end> Predicted translation: it s very cold here . <end>
translate(u'esta es mi vida.')
Input: <start> esta es mi vida . <end> Predicted translation: this is my life . <end>
translate(u'¿todavia estan en casa?')
Input: <start> ¿ todavia estan en casa ? <end> Predicted translation: are you still at home ? <end>
# wrong translation
translate(u'trata de averiguarlo.')
Input: <start> trata de averiguarlo . <end> Predicted translation: try to figure it out . <end>
Próximos pasos
- Descargue un conjunto de datos diferente para experimentar con traducciones, por ejemplo, del inglés al alemán o del inglés al francés.
- Experimente con el entrenamiento en un conjunto de datos más grande o use más épocas