This repository contains a Python Jupyter notebook demonstrating the implementation of a Long Short-Term Memory (LSTM) neural network for text generation. The model is trained on a dataset of news articles to predict the next word in a sequence of words. Additionally, a text generation function is provided to generate new text based on an initial input.
- Python 3.x
- Required Python libraries:
numpy,pandas,matplotlib,nltk, andtensorflow
- Ensure you have the required Python libraries installed. You can install them using the following command:
pip install numpy pandas matplotlib nltk tensorflow- Download the news dataset (
news.csv) and place it in the same directory as this notebook.
-
Open the Jupyter notebook (
text_generation.ipynb) in your Jupyter environment. -
Run the notebook cell by cell.
-
If you want to start training the model from scratch, execute all cells. If you want to load a pre-trained model and generate text, skip to the "Loading the Model" section.
-
Once the model is trained or loaded, you can use the provided functions for text prediction and generation. See the "Example Usage" section for demonstrations.
text_generation.ipynb: The Jupyter notebook containing the code.news.csv: The dataset containing news articles (not provided).
-
Training the Model: Execute cells under the "Training the Model" section to train the LSTM model.
-
Text Prediction: Utilize the
predict_next_wordfunction to predict the next word(s) given an input text.
possible = predict_next_word("She will have to look into this thing and she", 5)
for idx in possible:
print(unique_tokens[idx])- Text Generation: Use the
generate_textfunction to generate text based on an initial input.
generated_text = generate_text("He must have one thing that I am into the", 100, 10)
print(generated_text)text_gen_model1.h5: The trained LSTM model.history1.p: Pickled file containing the training history.
- Make sure to adjust the file paths if your dataset is stored in a different location.
Feel free to experiment with different parameters and dataset sizes for training the model.