Install textgenrnn via pip
masterYou can install textgenrnn from PyPI using pip3.
Requirement: You must have a minimum TensorFlow version of 2.1.0.
pip3 install textgenrnnrepository·master·Indexed 26 days ago
https://github.com/minimaxir/textgenrnnA Python 3 module built on Keras/TensorFlow for creating character-level or word-level recurrent neural networks (char-rns). It features a modern architecture with attention-weighting and skip-embeddings to accelerate training and improve text quality. The library supports fine-tuning pretrained models, training new architectures with bidirectional RNN layers, and generating sentence vectors from the Attention layer output. Requires TensorFlow 2.1.0 or higher.
You can install textgenrnn from PyPI using pip3.
Requirement: You must have a minimum TensorFlow version of 2.1.0.
pip3 install textgenrnnYou can create a dataset of the top Hacker News submissions by running a query in BigQuery.
Important Formatting Requirement: When exporting your data, save it as a .tsv file. Do not use .csv, as CSVs enclose sentences with quotes that contain commas, which will corrupt the dataset structure for textgenrnn.
#standardSQL
SELECT title
FROM `bigquery-public-data.hacker_news.full`
WHERE type = 'story'
ORDER BY score DESC
LIMIT 2000textgenrnn to generate text based on specific datasets. These weights are trained for 500 epochs on their respective datasets located in the /datasets directory. To use them, load the weight file into your model instance.You can create a dataset of top Reddit submissions for specific subreddits using BigQuery.
Important Formatting Requirement: When exporting your data, save it as a .tsv file. Do not use .csv, as CSVs enclose sentences with quotes that contain commas, which will corrupt the dataset structure for textgenrnn.
Constraints: Ensure the total number of rows does not exceed 10,000.
#standardSQL
SELECT title FROM (
SELECT title,
ROW_NUMBER() OVER (PARTITION BY subreddit ORDER BY score DESC) as score_rank
FROM `fh-bigquery.reddit_posts.*`
WHERE (_TABLE_SUFFIX BETWEEN '2017_01' AND '2017_06')
AND LOWER(subreddit) IN ("legaladvice", "relationship_advice")
)
WHERE score_rank <= 1000You can perform transfer learning by training a textgenrnn instance on one dataset and then training the same instance on a second dataset.
train_from_file with new_model=True to start training from scratch on your initial dataset.train_from_file again on the second dataset without specifying new_model=True.Best Practice: When performing the second stage of training (the transfer), use fewer num_epochs than the initial training to avoid overwriting the previous knowledge too aggressively.
To prevent the neural network from learning exact sequences (overfitting), use the following parameters in train_from_file():
train_size: A float representing the proportion of sequences used for training. The remaining data is used as a validation set. Monitor validation loss; it should ideally not increase after each epoch.dropout: A float representing the proportion of characters to drop out of a sequence for a given epoch. This forces the model to weigh remaining characters more efficiently.Warning on dropout:
max_length is low.0.2 or the model may fail to converge.from textgenrnn import textgenrnn
textgen = textgenrnn()
file_path = "../datasets/reddit_rarepuppers_politics_2000.txt"
textgen.reset()
# Using train_size=0.8 and dropout=0.2 to mitigate overfitting
textgen.train_from_file(
file_path,
new_model=True,
num_epochs=5,
gen_epochs=5,
train_size=0.8,
dropout=0.2
)You can control the influence of different models in synthesize by manipulating the input list:
model1, 1/4 model2, 1/4 model3), pass a list containing the models in those proportions: [model1, model1, model2, model3].models_list * 3).stop_tokens=[] to force a model switch after every single character.To use textgenrnn, import the class and instantiate it. By default, it uses a pretrained recurrent neural network.
from textgenrnn import textgenrnn
textgen = textgenrnn()To use the default pretrained model, import textgenrnn and instantiate the class. Calling .generate() will produce text based on the internal pretrained weights.
from textgenrnn import textgenrnn
textgen = textgenrnn()
textgen.generate()You can fine-tune the model on a new dataset by using the train_from_file method. Use the num_epochs parameter to control how many passes the model makes through the data. For small datasets, you may need to increase num_epochs to achieve better results.
textgen.train_from_file('hacker_news_2000.txt', num_epochs=1)Interactive mode allows you to manually pick from the top N options for the next character or word, providing a human-in-the-loop experience. Pass interactive=True and top_n=N to the generate method.
from textgenrnn import textgenrnn
textgen = textgenrnn()
textgen.generate(interactive=True, top_n=5)new_model=True to any training function.