-
Notifications
You must be signed in to change notification settings - Fork 85
/
martin_fierro_tensorflow.py
170 lines (140 loc) · 5.93 KB
/
martin_fierro_tensorflow.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
169
170
import tensorflow as tf
import numpy as np
import os
import time
# Descargar martin_fierro
# !wget https://www.gutenberg.org/cache/epub/14765/pg14765.txt -O martin_fierro.txt
# Aca se genera el encoder y se arma el dataset
# Leer el archivo y arma el vocabulario(todos los caracteres que aparecen)
text = open("data/martin_fierro.txt", 'rb').read().decode(encoding='latin-1')
# Limpia para que solo quede el texto del martin fierro
text = text[4945:70218]
vocab = sorted(set(text))
print(f'{len(vocab)} caracteres unicos')
# Arma un lookup para traducir de string a un numero y viceversa
ids_from_chars = tf.keras.layers.StringLookup(
vocabulary=list(vocab), mask_token=None)
chars_from_ids = tf.keras.layers.StringLookup(
vocabulary=ids_from_chars.get_vocabulary(), invert=True, mask_token=None)
# Funcion helper para traducir un conjunto de ids a texto
def text_from_ids(ids):
return tf.strings.reduce_join(chars_from_ids(ids), axis=-1)
# Traduce el texto a ids
all_ids = ids_from_chars(tf.strings.unicode_split(text, 'UTF-8'))
ids_dataset = tf.data.Dataset.from_tensor_slices(all_ids)
seq_length = 100
examples_per_epoch = len(text)//(seq_length+1)
sequences = ids_dataset.batch(seq_length+1, drop_remainder=True)
def split_input_target(sequence):
input_text = sequence[:-1]
target_text = sequence[1:]
return input_text, target_text
dataset = sequences.map(split_input_target)
# Batch size
BATCH_SIZE = 64
# Buffer size to shuffle the dataset
# (TF data is designed to work with possibly infinite sequences,
# so it doesn't attempt to shuffle the entire sequence in memory. Instead,
# it maintains a buffer in which it shuffles elements).
BUFFER_SIZE = 10000
# Arma el dataset
dataset = (
dataset
.shuffle(BUFFER_SIZE)
.batch(BATCH_SIZE, drop_remainder=True)
.prefetch(tf.data.experimental.AUTOTUNE))
# Aca se arma el modelo
# Length of the vocabulary in chars
vocab_size = len(vocab)
# The embedding dimension
embedding_dim = 256
# Number of RNN units
rnn_units = 1024
# Arma un modelo de tres capas
class MyModel(tf.keras.Model):
def __init__(self, vocab_size, embedding_dim, rnn_units):
super(MyModel, self).__init__(name='my_model')
# Transforma el id en un vector de tamaño fijo
self.embedding = tf.keras.layers.Embedding(vocab_size, embedding_dim)
# Red neuronal recurrente
self.lstm = tf.keras.layers.LSTM(rnn_units,
return_sequences=True,
return_state=True)
# Red neuronal normal
self.dense = tf.keras.layers.Dense(vocab_size)
def call(self, inputs, states=None, return_state=False, training=False):
x = inputs
x = self.embedding(x, training=training)
if states is None:
states = 1 #self.lstm.get_initial_state(x)
x, final_state,carry_state = self.lstm(x, initial_state=states, training=training)
x = self.dense(x, training=training)
if return_state:
return x, [final_state,carry_state]
else:
return x
# Se instancia el modelo y se le agrega la funcion de perdida y la forma en la que se optimizara
model = MyModel(
vocab_size=len(ids_from_chars.get_vocabulary()),
embedding_dim=embedding_dim,
rnn_units=rnn_units)
loss = tf.losses.SparseCategoricalCrossentropy(from_logits=True)
# Configura el modelo para ser entrenado
model.compile(optimizer='adam', loss=loss)
# Configuracion para guardar el modelo en un archivo
checkpoint_dir = './training_checkpoints'
checkpoint_prefix = os.path.join(checkpoint_dir, "ckpt_{epoch}.weights.h5")
checkpoint_callback = tf.keras.callbacks.ModelCheckpoint(
filepath=checkpoint_prefix,
save_weights_only=True)
# Aca se entrena el modelo
EPOCHS = 100
history = model.fit(dataset, epochs=EPOCHS, callbacks=[checkpoint_callback])
# Se genera un modelo que permita generar texto a partir de otro modelo
class OneStep(tf.keras.Model):
def __init__(self, model, chars_from_ids, ids_from_chars, temperature=1.0):
super().__init__()
self.temperature = temperature
self.model = model
self.chars_from_ids = chars_from_ids
self.ids_from_chars = ids_from_chars
# Create a mask to prevent "[UNK]" from being generated.
skip_ids = self.ids_from_chars(['[UNK]'])[:, None]
sparse_mask = tf.SparseTensor(
values=[-float('inf')]*len(skip_ids),
indices=skip_ids,
dense_shape=[len(ids_from_chars.get_vocabulary())])
self.prediction_mask = tf.sparse.to_dense(sparse_mask)
@tf.function
def generate_one_step(self, inputs, states=None):
# Convert strings to token IDs.
input_chars = tf.strings.unicode_split(inputs, 'UTF-8')
input_ids = self.ids_from_chars(input_chars).to_tensor()
# Run the model.
# predicted_logits.shape is [batch, char, next_char_logits]
predicted_logits, states = self.model(inputs=input_ids, states=states,
return_state=True)
# Only use the last prediction.
predicted_logits = predicted_logits[:, -1, :]
predicted_logits = predicted_logits/self.temperature
# Apply the prediction mask: prevent "[UNK]" from being generated.
predicted_logits = predicted_logits + self.prediction_mask
# Sample the output logits to generate token IDs.
predicted_ids = tf.random.categorical(predicted_logits, num_samples=1)
predicted_ids = tf.squeeze(predicted_ids, axis=-1)
# Convert from token ids to characters
predicted_chars = self.chars_from_ids(predicted_ids)
# Return the characters and model state.
return predicted_chars, states
# Se genera texto a partir del modelo previamente entrenado
# instancia lo necesario para generar el texto
one_step_model = OneStep(model, chars_from_ids, ids_from_chars)
states = None
next_char = tf.constant(['hermanos:'])
result = [next_char]
# Genera 1000 predicciones
for n in range(1000):
next_char, states = one_step_model.generate_one_step(next_char, states=states)
result.append(next_char)
result = tf.strings.join(result)
print(result[0].numpy().decode('utf-8'), '\n\n' + '_'*80)