The Poet class

class tflyrics.Poet(name: str = None, vocabulary: list = None, embedding_dim: int = 256, rnn_units: int = 1024, batch_size: int = 32)

An artificial poet.

A Poet object is an object that wraps a recurrent predictive TensorFlow model, and with it can predict the next character in a sequence of unicode characters extracted from a possibly large text corpus.

property batch_size

Get the batch size of the poet’s internal model.

Get the model’s batch size, i.e. the number of inputs that can be fed at once to the model for traning or evaluation.

Returns

the current batch size

build_model(batch_size: int = 1) → None

Build the Poet’s internal model.

Parameters

batch_size – the number of inputs to be fed at once to the model

generate(start_string: str, n_gen_chars: int = 1000, temperature: float = 1.0) → str

Generate text using the poet’s internal model.

Generate a specified number of characters by feeding an initial string to the model and using this string as a starting point to repeatedly sample a character that is likely to appear after the previously generated character.

Parameters
  • start_string – initial string fed to the model

  • n_gen_chars – number of characters to generate

  • temperature – how surprising vs. predictable characters should be

Returns

a new, generated text

restore() → None

Restore the state of the Poet’s model from a checkpoint.

If checkpoints have been saved during training, set the Poet’s model parameters (weights) to the state of the latest checkpoint saved.

Raises

ResourceWarning

train_on(train_data: object, val_data: object = None, n_epochs: int = 1, checkpoints: bool = False) → dict

Train the Poet’s internal model on a dataset.

Train the Poet’s internal model on a TensorFlow Dataset containing batches of sequences of text (encoded as integers). You can also add a validation dataset as a separate argument. Optionally specify the number of epochs (by default just 1 epoch), and whether a checkpoint of the model should be saved at the end of each training epoch. Returns the training history, as a dictionary whose values are lists; each list represents a metric, and each element in the list is the value of that metric at a certain epoch.

Parameters
  • train_data – LyricsGenerator or TensorFlow Dataset

  • val_data – LyricsGenerator or TensorFlow Dataset

  • n_epochs – number of training epochs

  • checkpoints – whether or not to save checkpoints of the model

Returns

the training history, as a dictionary of lists