TensorFlow / Python: Generación de Texto con Redes Neuronales Long Short-Term Memory (LSTM)

La Generación de Texto con Redes Neuronales LSTM, hacen referencia a «Long Short-Term Memory» (Memoria a Largo Plazo y Corto Plazo). Es un tipo de red neuronal recurrente (RNN) que se utiliza comúnmente para tareas relacionadas con el procesamiento del lenguaje natural, como la generación de texto, la traducción automática y el análisis de sentimientos. A diferencia de las RNN tradicionales, las LSTM pueden aprender y recordar dependencias a largo plazo en secuencias de datos, lo que las hace especialmente útiles para tareas que involucran datos secuenciales.

import tensorflow as tf
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.layers import Embedding, LSTM, Dense
import numpy as np

# Leer el archivo de texto
with open('tu_texto.txt', 'r', encoding='utf-8') as file:
    text = file.read()

# Preprocesar el texto y crear secuencias
tokenizer = Tokenizer()
tokenizer.fit_on_texts([text])
total_words = len(tokenizer.word_index) + 1

# Crear secuencias de entrada y objetivo
input_sequences = []
for i in range(1, len(text)):
    n_gram_sequence = text[:i+1]
    input_sequences.append(n_gram_sequence)

# Tokenizar las secuencias y rellenarlas
input_sequences = tokenizer.texts_to_sequences(input_sequences)
input_sequences = pad_sequences(input_sequences)

# Crear datos de entrada y objetivo
X = input_sequences[:, :-1]
y = input_sequences[:, -1]

# Modelo LSTM
model = tf.keras.Sequential([
    Embedding(total_words, 64, input_length=X.shape[1]),
    LSTM(100),
    Dense(total_words, activation='softmax')
])

model.compile(loss='sparse_categorical_crossentropy', optimizer='adam')

# Entrenar el modelo
model.fit(X, y, epochs=5, verbose=1)

Probar Modelo:

# Generar texto de prueba
seed_text = "El sol brilla en"
next_words = 10

for _ in range(next_words):
    token_list = tokenizer.texts_to_sequences([seed_text])[0]
    token_list = pad_sequences([token_list], maxlen=X.shape[1], padding='pre')
    predicted = model.predict(token_list, verbose=0)
    predicted_word = tokenizer.index_word[np.argmax(predicted)]
    seed_text += " " + predicted_word

print(seed_text)

El resultado que arrojará podría ser parecido al que resultó con el modelo:
El sol brilla en la nieve

El código anterior carga un archivo de texto, lo pre-procesa, construye un modelo LSTM simple, lo entrena y genera texto a partir de una semilla inicial. A medida que se ejecuta el bucle final, el modelo genera texto caracter por caracter.

El archivo tu_texto.txt mencionado en el código debe contener el texto de entrenamiento en el que deseas que el modelo LSTM se base para generar texto. Puede ser cualquier archivo de texto que desees, como una novela, un conjunto de artículos, poesía o cualquier otro texto. El modelo aprenderá de ese texto y generará secuencias de caracteres similares.

El código carga este archivo, lo preprocesa dividiéndolo en fragmentos más cortos, mapea los caracteres a números para que la red neuronal pueda entenderlos y, finalmente, utiliza esos fragmentos para entrenar el modelo LSTM. Luego, el modelo genera texto basado en la semilla inicial proporcionada en generated_text.

Es importante mencionar que, para obtener resultados más largos, significativos y coherentes, se necesita una cantidad suficientemente grande de texto de entrenamiento y una arquitectura de modelo LSTM más compleja. El ejemplo proporcionado es simplificado y se puede utilizar como punto de partida para comprender cómo funcionan las LSTM en la generación de texto.

38 thoughts on “TensorFlow / Python: Generación de Texto con Redes Neuronales Long Short-Term Memory (LSTM)

  1. Blue Techker naturally like your web site however you need to take a look at the spelling on several of your posts. A number of them are rife with spelling problems and I find it very bothersome to tell the truth on the other hand I will surely come again again.

  2. Every time I visit your website, I’m greeted with thought-provoking content and impeccable writing. You truly have a gift for articulating complex ideas in a clear and engaging manner.

  3. of course like your website but you have to check the spelling on several of your posts A number of them are rife with spelling issues and I in finding it very troublesome to inform the reality on the other hand I will certainly come back again

  4. of course like your website but you have to check the spelling on several of your posts A number of them are rife with spelling issues and I in finding it very troublesome to inform the reality on the other hand I will certainly come back again

  5. أنابيب البولي إيثيلين في العراق يعد مصنع إيليت بايب رائدًا في إنتاج أنابيب البولي إيثيلين، التي تشتهر بمرونتها العالية ومقاومتها للمواد الكيميائية وعمرها الطويل. هذه الأنابيب مناسبة لمجموعة واسعة من التطبيقات، بما في ذلك إمدادات المياه والري الزراعي. كأحد أبرز المصانع في العراق، يلتزم مصنع إيليت بايب بتقديم أنابيب البولي إيثيلين التي تتجاوز توقعات الصناعة. لمزيد من المعلومات المفصلة حول أنابيب البولي إيثيلين، قم بزيارة elitepipeiraq.com.

  6. you are truly a just right webmaster The site loading speed is incredible It kind of feels that youre doing any distinctive trick In addition The contents are masterwork you have done a great activity in this matter

  7. I loved as much as youll receive carried out right here The sketch is tasteful your authored material stylish nonetheless you command get bought an nervousness over that you wish be delivering the following unwell unquestionably come more formerly again since exactly the same nearly a lot often inside case you shield this hike

  8. Vitazen Keto You’re so awesome! I don’t believe I have read a single thing like that before. So great to find someone with some original thoughts on this topic. Really.. thank you for starting this up. This website is something that is needed on the internet, someone with a little originality!

  9. I do not even know how I ended up here but I thought this post was great I do not know who you are but certainly youre going to a famous blogger if you are not already Cheers

  10. What i do not realize is in fact how you are no longer actually much more wellfavored than you might be right now Youre very intelligent You recognize thus considerably in relation to this topic made me in my view believe it from numerous numerous angles Its like men and women are not fascinated until it is one thing to do with Lady gaga Your own stuffs excellent All the time handle it up

  11. My brother suggested I might like this blog He was totally right This post actually made my day You can not imagine simply how much time I had spent for this info Thanks

  12. I thought you did a great job on this. Although your language is excellent and the picture is enticing, you come across as nervous about what you might be giving next. If you save this walk, I hope you will come back here often.

  13. I do believe all the ideas youve presented for your post They are really convincing and will certainly work Nonetheless the posts are too short for novices May just you please lengthen them a little from subsequent time Thanks for the post

  14. helloI really like your writing so a lot share we keep up a correspondence extra approximately your post on AOL I need an expert in this house to unravel my problem May be that is you Taking a look ahead to see you

  15. What i do not realize is in fact how you are no longer actually much more wellfavored than you might be right now Youre very intelligent You recognize thus considerably in relation to this topic made me in my view believe it from numerous numerous angles Its like men and women are not fascinated until it is one thing to do with Lady gaga Your own stuffs excellent All the time handle it up

Deja un comentario