This will learn how to generate Obama speeches based on the data here.

    import unidecode
    import string
    import random
    import math

    import torch
    import torch.nn as nn
    from torch.autograd import Variable

    all_characters = string.printable
    input_size = len(all_characters)
    output_size = input_size
    print(input_size)

    n_steps = 2000
    batch_size = 512
    hidden_size = 100
    n_layers = 2
    learning_rate = 0.01
    len_text = 200
    print_every = 50

    filename = '../Data/obama.txt'
    data = unidecode.unidecode(open(filename).read())
    len_data = len(data)

    def char_to_tensor(string):
        tensor = torch.zeros(len(string)).long()
        for c in range(len(string)):
            try:
                tensor[c] = all_characters.index(string[c])
            except:
                continue
        return tensor

    def batch_gen(length_text, batch_size):
        X = torch.LongTensor(batch_size, length_text)
        y = torch.LongTensor(batch_size, length_text)
        for i in range(batch_size):
            start_index = random.randint(0, len_data - length_text)
            end_index = start_index + length_text + 1
            text = data[start_index:end_index]
            X[i] = char_to_tensor(text[:-1])
            y[i] = char_to_tensor(text[1:])
        X = Variable(X)
        y = Variable(y)
        X = X.cuda()
        y = y.cuda()
        return X, y

    class create_model(nn.Module):
        def __init__(self, input_size, hidden_size, output_size, n_layers=1):
            super(create_model, self).__init__()
            self.input_size = input_size
            self.hidden_size = hidden_size
            self.output_size = output_size
            self.n_layers = n_layers

            self.encoder = nn.Embedding(input_size, hidden_size)
            self.rnn = nn.GRU(hidden_size, hidden_size, n_layers)
            self.decoder = nn.Linear(hidden_size, output_size)

        def forward(self, input, hidden):
            batch_size = input.size(0)
            encoded = self.encoder(input)
            output, hidden = self.rnn(encoded.view(1, batch_size, -1), hidden)
            output = self.decoder(output.view(batch_size, -1))
            return output, hidden

        def init_hidden(self, batch_size):
            return Variable(torch.zeros(self.n_layers, batch_size, self.hidden_size))


    decoder_model = create_model(
        input_size,
        hidden_size,
        output_size,
        n_layers=n_layers,
    )

    opt = torch.optim.Adam(decoder_model.parameters(), lr=learning_rate)
    loss = nn.CrossEntropyLoss()
    decoder_model.cuda()

    def generate_text(decoder, start='The', predict_len=100):
        hidden = decoder.init_hidden(1).cuda()
        prime_input = Variable(char_to_tensor(start).unsqueeze(0)).cuda()
        predicted = start

        for p in range(len(start) - 1):
            _, hidden = decoder(prime_input[:, p], hidden)

        x = prime_input[:,-1]

        for p in range(predict_len):
            output, hidden = decoder(x, hidden)
            output_dist = output.data.view(-1).div(0.8).exp()
            # Add some randomness
            top_i = torch.multinomial(output_dist, 1)[0]
            predicted_char = all_characters[top_i]
            predicted += predicted_char
            x = Variable(char_to_tensor(predicted_char).unsqueeze(0)).cuda()
        return predicted

    loss_avg = 0

    for i in range(n_steps):
        X, y = batch_gen(len_text, batch_size)
        hidden = decoder_model.init_hidden(batch_size).cuda()
        decoder_model.zero_grad()
        loss_total = 0

        for c in range(len_text):
            output, hidden = decoder_model(X[:,c], hidden)
            loss_total += loss(output.view(batch_size, -1), y[:,c])

        loss_total.backward()
        opt.step()
        loss_value = loss_total.data[0] / len_text

        loss_avg += loss_value

        if i % print_every == 0:
            print('Epoch {}: loss {}'.format(i, loss_avg))
            print(generate_text(decoder_model, 'The', 100), '\n')

Epoch 400: loss 620.1863698577881
The down debated, deain topmsted and the right taxes to start that purstain of our committed. No demove

Epoch 450: loss 680.1121346282961
The cost of a tate. I’ve working who finally spending working our respondable place studence of the roe

Epoch 500: loss 739.0341670989992
The uply, and we have the serious new storythen companies have also an investments to the hear innegowi

Epoch 550: loss 797.0386454772951
These only seople should seady. It will get it done, and we will look about here their hands and put in

Epoch 600: loss 854.3123697662357
The communities, strugge all of the first to be at combat control, the rules, it’s not never been in th

Epoch 650: loss 911.0346323394779
There is the number for the lead of same in 30 years. That’s what I would hard the kinds. It will an oi

Epoch 700: loss 967.0773259735106
The bringed in the strength and important to Al Asia strees, and we know you security and workers, and

Epoch 750: loss 1022.4571331787109
The business of is change and faces? Orle that in comes, the world businesses we need that would have o

Epoch 800: loss 1077.2675802612307
They speak, the world standard because of working when our person to storid with the same and 20 bill b