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.

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

  1. Bwer Company is a top supplier of weighbridge truck scales in Iraq, providing a complete range of solutions for accurate vehicle load measurement. Their services cover every aspect of truck scales, from truck scale installation and maintenance to calibration and repair. Bwer Company offers commercial truck scales, industrial truck scales, and axle weighbridge systems, tailored to meet the demands of heavy-duty applications. Bwer Company’s electronic truck scales and digital truck scales incorporate advanced technology, ensuring precise and reliable measurements. Their heavy-duty truck scales are engineered for rugged environments, making them suitable for industries such as logistics, agriculture, and construction. Whether you’re looking for truck scales for sale, rental, or lease, Bwer Company provides flexible options to match your needs, including truck scale parts, accessories, and software for enhanced performance. As trusted truck scale manufacturers, Bwer Company offers certified truck scale calibration services, ensuring compliance with industry standards. Their services include truck scale inspection, certification, and repair services, supporting the long-term reliability of your truck scale systems. With a team of experts, Bwer Company ensures seamless truck scale installation and maintenance, keeping your operations running smoothly. For more information on truck scale prices, installation costs, or to learn about their range of weighbridge truck scales and other products, visit Bwer Company’s website at bwerpipes.com.

  2. 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.

  3. 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.

  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. 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

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

  7. 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

  8. 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

  9. 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!

  10. 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

  11. 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

  12. 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

  13. 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.

  14. 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

  15. 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

  16. 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