Character-level text generation
RNNs are not only powerful to and classify text. RNNs can also be used to generate text. In it's simplest form, text is generated on character level. More specifically, the text is generated character per character. Before we can generate text, we need to train a decoder on full sentences. By including a GRU layer in our decoder, the model does not only depend on the previous input but does try to predict the next character based on the context around it. In the following recipe, we will demonstrate how to implement a character-level text generator with PyTorch.
How to do it...
- Let's start with importing the libraries as follows:
import unidecode import string import random import math import torch import torch.nn as nn from torch.autograd import Variable
- As input and output, we can use any character:
all_characters = string.printable input_size = len(all_characters) output_size = input_size print(input_size)
- We will be using a dataset with speeches from Obama...