Este código es un ejemplo básico de cómo construir y entrenar una RNN para tareas de procesamiento de lenguaje natural, en este caso, la clasificación de sentimientos basados en datos de reseñas de películas de IMDB.
import tensorflow as tf
from tensorflow.keras.datasets import imdb
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding, LSTM, Dense
from tensorflow.keras.preprocessing.text import Tokenizer
import numpy as np
# Cargar el conjunto de datos IMDB
num_words = 10000 # Seleccionar las 10,000 palabras más frecuentes
(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=num_words)
# Preprocesamiento de datos
max_sequence_length = 100 # Limitar la longitud de las secuencias
x_train = pad_sequences(x_train, maxlen=max_sequence_length)
x_test = pad_sequences(x_test, maxlen=max_sequence_length)
# Construir el modelo
model = Sequential([
Embedding(input_dim=num_words, output_dim=128, input_length=max_sequence_length),
LSTM(128),
Dense(1, activation='sigmoid')
])
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# Entrenar el modelo
model.fit(x_train, y_train, epochs=3, batch_size=64, validation_split=0.2)
# Evaluar el modelo
test_loss, test_acc = model.evaluate(x_test, y_test)
print(f'Precisión en el conjunto de prueba: {test_acc * 100:.2f}%')
model.save('rnn_sentiment_model.h5')
Probar modelo:
# Cargar el modelo previamente entrenado
model = tf.keras.models.load_model('/content/rnn_sentiment_model.h5') # Reemplaza 'rnn_sentiment_model.h5' con el nombre de tu archivo de modelo
# Imprimir las 10 primeras listas del conjunto de entrenamiento
for i in range(10):
print(f"Secuencia {i + 1}: {x_train[i]}")
# Tus propias revisiones de películas
reviews = [
"This movie was fantastic",
"solid performances in this straightforward adaption",
"brilliant documentary",
"worst mistake of my life",
]
# Tokeniza tus revisiones utilizando el mismo Tokenizer que usaste para el modelo
tokenizer = Tokenizer(num_words=10000) # Asegúrate de que coincida con el valor que usaste para entrenar el modelo
tokenizer.fit_on_texts(reviews)
sequences = tokenizer.texts_to_sequences(reviews)
# Asegúrate de que todas las secuencias tengan la misma longitud
max_sequence_length = 100 # Asegúrate de que coincida con el valor que usaste para entrenar el modelo
sequences = pad_sequences(sequences, maxlen=max_sequence_length)
# Realiza predicciones en tus propias revisiones
predictions = model.predict(sequences)
# Muestra las predicciones
for i, prediction in enumerate(predictions):
sentiment = "positivo" if prediction > 0.5 else "negativo"
print(f"Sentimiento de tu revisión {i + 1}: {sentiment} (Puntuación: {prediction[0]:.4f})")
El código anterior muestra cómo crear, entrenar y evaluar un modelo de análisis de sentimientos utilizando una red neuronal recurrente (RNN) con una capa LSTM en TensorFlow. Aquí una explicación con mas detalles:
- Importación de bibliotecas: Se importan las bibliotecas necesarias, incluyendo TensorFlow y sus componentes, como
datasets
,preprocessing
,models
, ylayers
. También se importaTokenizer
de TensorFlow para preprocesar los datos y NumPy para manipulación numérica.
- Carga del conjunto de datos IMDB: Se carga el conjunto de datos IMDB, que contiene revisiones de películas etiquetadas como positivas o negativas. Se limita el número de palabras a las 10,000 palabras más frecuentes utilizando el parámetro
num_words
.
- Preprocesamiento de datos: Las secuencias de palabras en el conjunto de datos se ajustan a una longitud máxima de 100 utilizando
pad_sequences
. Esto asegura que todas las secuencias tengan la misma longitud, lo que es necesario para entrenar el modelo.
- Construcción del modelo: Se crea un modelo secuencial, que es una secuencia lineal de capas. El modelo consta de las siguientes capas:
- –
Embedding
: Esta capa se encarga de transformar las secuencias de palabras en vectores densos de palabras. Tiene 10,000 palabras de entrada, cada una representada por un vector de 128 dimensiones. - –
LSTM
: La capa LSTM es una capa de memoria a largo plazo que procesa la secuencia de entrada. Tiene 128 unidades (neuronas). - –
Dense
: Capa de salida con una sola neurona y función de activación sigmoide, utilizada para predecir la polaridad de la revisión (positiva o negativa).
- Compilación del modelo: Se compila el modelo especificando el optimizador (‘adam’), la función de pérdida (‘binary_crossentropy’ para problemas de clasificación binaria) y la métrica de precisión.
- Entrenamiento del modelo: El modelo se entrena en el conjunto de entrenamiento durante 3 épocas con un tamaño de lote de 64. También se utiliza un 20% de los datos como conjunto de validación para evaluar el modelo durante el entrenamiento.
- Evaluación del modelo: Una vez que el modelo ha sido entrenado, se evalúa su rendimiento en el conjunto de prueba y se muestra la precisión en el conjunto de prueba.
- Guardado del modelo: El modelo entrenado se guarda en un archivo llamado ‘rnn_sentiment_model.h5’ para su posterior uso.
Es obvio tener en cuenta que el modelo entrenado es una versión básica con fines de demostración y es posible mejorar su precisión mediante varias estrategias, como entrenar con más muestras, ajustar hiperparámetros y utilizar modelos más complejos. Aquí hay algunas formas de mejorar la precisión:
- Aumentar el Tamaño del Conjunto de Datos: Entrenar con un conjunto de datos más grande generalmente mejora el rendimiento del modelo.
- Ajustar Hiperparámetros: Experimentar con diferentes valores de hiperparámetros, como la tasa de aprendizaje, el tamaño del lote, el número de unidades LSTM y las dimensiones de los vectores de palabras en la capa
Embedding
. - Usar Modelos más Complejos: Considerar el uso de arquitecturas de modelos más avanzadas, como redes neuronales convolucionales (CNN) o modelos de atención, que pueden capturar características más ricas en los textos.
- Pre-entrenamiento de Embeddings: Utilizar embeddings pre-entrenados, como Word2Vec, GloVe o BERT, puede mejorar el rendimiento del modelo.
- Más Épocas de Entrenamiento: Entrenar durante más épocas puede permitir que el modelo converja a una solución óptima.
Sportsurge Great information shared.. really enjoyed reading this post thank you author for sharing this post .. appreciated
Tech Learner Very well presented. Every quote was awesome and thanks for sharing the content. Keep sharing and keep motivating others.
Jinx Manga very informative articles or reviews at this time.
Mangaclash This was beautiful Admin. Thank you for your reflections.
Mangaclash I am truly thankful to the owner of this web site who has shared this fantastic piece of writing at at this place.
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
GlobalBllog 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!
SocialMediaGirls I very delighted to find this internet site on bing, just what I was searching for as well saved to fav
Usually I do not read article on blogs however I would like to say that this writeup very compelled me to take a look at and do it Your writing style has been amazed me Thank you very nice article
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
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
Thank you I have just been searching for information approximately this topic for a while and yours is the best I have found out so far However what in regards to the bottom line Are you certain concerning the supply
My brother suggested I might like this website He was totally right This post actually made my day You cannt imagine just how much time I had spent for this information Thanks
Tech to Force I am truly thankful to the owner of this web site who has shared this fantastic piece of writing at at this place.
Usually I do not read article on blogs however I would like to say that this writeup very compelled me to take a look at and do so Your writing taste has been amazed me Thanks quite nice post
Baddiehubs I very delighted to find this internet site on bing, just what I was searching for as well saved to fav
Real Estate For the reason that the admin of this site is working, no uncertainty very quickly it will be renowned, due to its quality contents.
Real Estate This was beautiful Admin. Thank you for your reflections.
startup talky I just like the helpful information you provide in your articles
Your blog has quickly become one of my favorites. Your writing is both insightful and thought-provoking, and I always come away from your posts feeling inspired. Keep up the phenomenal work!
I am not sure where youre getting your info but good topic I needs to spend some time learning much more or understanding more Thanks for magnificent info I was looking for this information for my mission
certainly like your website but you need to take a look at the spelling on quite a few of your posts Many of them are rife with spelling problems and I find it very troublesome to inform the reality nevertheless I will definitely come back again
obviously like your website but you need to test the spelling on quite a few of your posts Several of them are rife with spelling problems and I to find it very troublesome to inform the reality on the other hand Ill certainly come back again
Your work has captivated me just as much as it has you. The sketch you’ve created is tasteful, and the material you’ve written is impressive. However, you seem anxious about the prospect of presenting something that could be considered questionable. I believe you’ll be able to rectify this situation in a timely manner.
Thank you for the auspicious writeup It in fact was a amusement account it Look advanced to far added agreeable from you However how can we communicate
Your articles never fail to captivate me. Each one is a testament to your expertise and dedication to your craft. Thank you for sharing your wisdom with the world.
Your blog is a beacon of light in the often murky waters of online content. Your thoughtful analysis and insightful commentary never fail to leave a lasting impression. Keep up the amazing work!
Excellent blog here Also your website loads up very fast What web host are you using Can I get your affiliate link to your host I wish my web site loaded up as quickly as yours lol
I loved as much as you will receive carried out right here The sketch is attractive your authored material stylish nonetheless you command get got an impatience 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
Hi my loved one I wish to say that this post is amazing nice written and include approximately all vital infos Id like to peer more posts like this
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
Wonderful beat I wish to apprentice while you amend your web site how could i subscribe for a blog web site The account aided me a acceptable deal I had been a little bit acquainted of this your broadcast provided bright clear idea
Usually I do not read article on blogs however I would like to say that this writeup very compelled me to take a look at and do so Your writing taste has been amazed me Thanks quite nice post
Hi i think that i saw you visited my web site thus i came to Return the favore I am attempting to find things to improve my web siteI suppose its ok to use some of your ideas
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.
I do not even know how I ended up here but I thought this post was great I dont know who you are but definitely youre going to a famous blogger if you arent already Cheers
you are in reality a just right webmaster The site loading velocity is incredible It seems that you are doing any unique trick In addition The contents are masterwork you have performed a wonderful task on this topic
I loved as much as you will receive carried out right here The sketch is tasteful your authored subject matter stylish nonetheless you command get got an edginess over that you wish be delivering the following unwell unquestionably come further formerly again as exactly the same nearly very often inside case you shield this hike
you are in reality a just right webmaster The site loading velocity is incredible It seems that you are doing any unique trick In addition The contents are masterwork you have performed a wonderful task on this topic
I loved as much as youll receive carried out right here The sketch is attractive 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 as exactly the same nearly a lot often inside case you shield this hike
I was recommended this website by my cousin I am not sure whether this post is written by him as nobody else know such detailed about my difficulty You are wonderful Thanks
Hi there to all, for the reason that I am genuinely keen of reading this website’s post to be updated on a regular basis. It carries pleasant stuff.
I like the efforts you have put in this, regards for all the great content.
This is my first time pay a quick visit at here and i am really happy to read everthing at one place
I like the efforts you have put in this, regards for all the great content.
Awesome! Its genuinely remarkable post, I have got much clear idea regarding from this post
I appreciate your creativity and the effort you put into every post. Keep up the great work!
Wow, this blogger is seriously impressive!
I do not even understand how I ended up here, but I assumed this publish used to be great
Great information shared.. really enjoyed reading this post thank you author for sharing this post .. appreciated