TensorFlow 包装器,用于使用 RNN/LSTM 在字符或单词级别上生成深度神经文本
项目描述
用 4 行代码生成莎士比亚诗歌。
安装
tensorlm是用 / 为 Python 3.4+ 和 TensorFlow 1.1+ 编写的
pip3 install tensorlm
基本用法
使用CharLM或WordLM类:
import tensorflow as tf
from tensorlm import CharLM
with tf.Session() as session:
# Create a new model. You can also use WordLM
model = CharLM(session, "datasets/sherlock/tinytrain.txt", max_vocab_size=96,
neurons_per_layer=100, num_layers=3, num_timesteps=15)
# Train it
model.train(session, max_epochs=5, max_steps=500)
# Let it generate a text
generated = model.sample(session, "The ", num_steps=100)
print("The " + generated)
这应该输出如下内容:
The ee e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e
命令行用法
训练: python3 -m tensorlm.cli --train=True --level=char --train_text_path=datasets/sherlock/tinytrain.txt --max_vocab_size=96 --neurons_per_layer=100 --num_layers=2 --batch_size=10 - -num_timesteps=15 --save_dir=out/model --max_epochs=300 --save_interval_hours=0.5
示例: python3 -m tensorlm.cli --sample=True --level=char --neurons_per_layer=400 --num_layers=3 --num_timesteps=160 --save_dir=out/model
评估: python3 -m tensorlm.cli --evaluate=True --level=char --evaluate_text_path=datasets/sherlock/tinyvalid.txt --neurons_per_layer=400 --num_layers=3 --batch_size=10 --num_timesteps=160 - -save_dir=out/模型
有关所有选项,请参阅python3 -m tensorlm.cli --help。
高级用法
自定义输入数据
输入和目标不必是文本。GeneratingLSTM只需要令牌 id,因此您可以对序列使用任何数据类型,只要您可以将数据编码为整数 id。
# We use integer ids from 0 to 19, so the vocab size is 20. The range of ids must always start
# at zero.
batch_inputs = np.array([[1, 2, 3, 4], [15, 16, 17, 18]]) # 2 batches, 4 time steps each
batch_targets = np.array([[2, 3, 4, 5], [16, 17, 18, 19]])
# Create the model in a TensorFlow graph
model = GeneratingLSTM(vocab_size=20, neurons_per_layer=10, num_layers=2, max_batch_size=2)
# Initialize all defined TF Variables
session.run(tf.global_variables_initializer())
for _ in range(5000):
model.train_step(session, batch_inputs, batch_targets)
sampled = model.sample_ids(session, [15], num_steps=3)
print("Sampled: " + str(sampled))
这应该输出如下内容:
Sampled: [16, 18, 19]
自定义训练、Dropout 等
直接使用GenerateLSTM类。此类与数据集类型无关。它需要整数 id 并返回整数 id。
import tensorflow as tf
from tensorlm import Vocabulary, Dataset, GeneratingLSTM
BATCH_SIZE = 20
NUM_TIMESTEPS = 15
with tf.Session() as session:
# Generate a token -> id vocabulary based on the text
vocab = Vocabulary.create_from_text("datasets/sherlock/tinytrain.txt", max_vocab_size=96,
level="char")
# Obtain input and target batches from the text file
dataset = Dataset("datasets/sherlock/tinytrain.txt", vocab, BATCH_SIZE, NUM_TIMESTEPS)
# Create the model in a TensorFlow graph
model = GeneratingLSTM(vocab_size=vocab.get_size(), neurons_per_layer=100, num_layers=2,
max_batch_size=BATCH_SIZE, output_keep_prob=0.5)
# Initialize all defined TF Variables
session.run(tf.global_variables_initializer())
# Do the training
epoch = 1
step = 1
for epoch in range(20):
for inputs, targets in dataset:
loss = model.train_step(session, inputs, targets)
if step % 100 == 0:
# Evaluate from time to time
dev_dataset = Dataset("datasets/sherlock/tinyvalid.txt", vocab,
batch_size=BATCH_SIZE, num_timesteps=NUM_TIMESTEPS)
dev_loss = model.evaluate(session, dev_dataset)
print("Epoch: %d, Step: %d, Train Loss: %f, Dev Loss: %f" % (
epoch, step, loss, dev_loss))
# Sample from the model from time to time
print("Sampled: \"The " + model.sample_text(session, vocab, "The ") + "\"")
step += 1
这应该输出如下内容:
Epoch: 3, Step: 100, Train Loss: 3.824941, Dev Loss: 3.778008 Sampled: "The " Epoch: 7, Step: 200, Train Loss: 2.832825, Dev Loss: 2.896187 Sampled: "The " Epoch: 11, Step: 300, Train Loss: 2.778579, Dev Loss: 2.830176 Sampled: "The eee " Epoch: 15, Step: 400, Train Loss: 2.655153, Dev Loss: 2.684828 Sampled: "The ee e e e e e e e e e e e e e e e e e e e e e e e e e e e " Epoch: 19, Step: 500, Train Loss: 2.444502, Dev Loss: 2.479753 Sampled: "The an an an on on on on on on on on on on on on on on on on on on on on on o"