Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/C5 - Sequence Models/Week 1/Dinosaur Island -- Character-level language model/shakespeare_utils.py
Views: 4819
# Load Packages1from __future__ import print_function2from tensorflow.keras.callbacks import LambdaCallback3from tensorflow.keras.models import Model, load_model, Sequential4from tensorflow.keras.layers import Dense, Activation, Dropout, Input, Masking5from tensorflow.keras.layers import LSTM6from tensorflow.keras.utils import get_file7from tensorflow.keras.preprocessing.sequence import pad_sequences8import numpy as np9import random10import sys11import io1213def build_data(text, Tx = 40, stride = 3):14"""15Create a training set by scanning a window of size Tx over the text corpus, with stride 3.1617Arguments:18text -- string, corpus of Shakespearian poem19Tx -- sequence length, number of time-steps (or characters) in one training example20stride -- how much the window shifts itself while scanning2122Returns:23X -- list of training examples24Y -- list of training labels25"""2627X = []28Y = []2930### START CODE HERE ### (≈ 3 lines)31for i in range(0, len(text) - Tx, stride):32X.append(text[i: i + Tx])33Y.append(text[i + Tx])34### END CODE HERE ###3536print('number of training examples:', len(X))3738return X, Y394041def vectorization(X, Y, n_x, char_indices, Tx = 40):42"""43Convert X and Y (lists) into arrays to be given to a recurrent neural network.4445Arguments:46X --47Y --48Tx -- integer, sequence length4950Returns:51x -- array of shape (m, Tx, len(chars))52y -- array of shape (m, len(chars))53"""5455m = len(X)56x = np.zeros((m, Tx, n_x), dtype=np.bool)57y = np.zeros((m, n_x), dtype=np.bool)58for i, sentence in enumerate(X):59for t, char in enumerate(sentence):60x[i, t, char_indices[char]] = 161y[i, char_indices[Y[i]]] = 16263return x, y646566def sample(preds, temperature=1.0):67# helper function to sample an index from a probability array68preds = np.asarray(preds).astype('float64')69preds = np.log(preds) / temperature70exp_preds = np.exp(preds)71preds = exp_preds / np.sum(exp_preds)72probas = np.random.multinomial(1, preds, 1)73out = np.random.choice(range(len(chars)), p = probas.ravel())74return out75#return np.argmax(probas)7677def on_epoch_end(epoch, logs):78# Function invoked at end of each epoch. Prints generated text.79None80#start_index = random.randint(0, len(text) - Tx - 1)8182#generated = ''83#sentence = text[start_index: start_index + Tx]84#sentence = '0'*Tx85#usr_input = input("Write the beginning of your poem, the Shakespearian machine will complete it.")86# zero pad the sentence to Tx characters.87#sentence = ('{0:0>' + str(Tx) + '}').format(usr_input).lower()88#generated += sentence89#90#sys.stdout.write(usr_input)9192#for i in range(400):93"""94#x_pred = np.zeros((1, Tx, len(chars)))9596for t, char in enumerate(sentence):97if char != '0':98x_pred[0, t, char_indices[char]] = 1.99100preds = model.predict(x_pred, verbose=0)[0]101next_index = sample(preds, temperature = 1.0)102next_char = indices_char[next_index]103104generated += next_char105sentence = sentence[1:] + next_char106107sys.stdout.write(next_char)108sys.stdout.flush()109110if next_char == '\n':111continue112113# Stop at the end of a line (4 lines)114print()115"""116print("Loading text data...")117text = io.open('shakespeare.txt', encoding='utf-8').read().lower()118#print('corpus length:', len(text))119120Tx = 40121chars = sorted(list(set(text)))122char_indices = dict((c, i) for i, c in enumerate(chars))123indices_char = dict((i, c) for i, c in enumerate(chars))124#print('number of unique characters in the corpus:', len(chars))125126print("Creating training set...")127X, Y = build_data(text, Tx, stride = 3)128print("Vectorizing training set...")129x, y = vectorization(X, Y, n_x = len(chars), char_indices = char_indices)130print("Loading model...")131model = load_model('models/model_shakespeare_kiank_350_epoch.h5')132133134def generate_output():135generated = ''136#sentence = text[start_index: start_index + Tx]137#sentence = '0'*Tx138usr_input = input("Write the beginning of your poem, the Shakespeare machine will complete it. Your input is: ")139# zero pad the sentence to Tx characters.140sentence = ('{0:0>' + str(Tx) + '}').format(usr_input).lower()141generated += usr_input142143sys.stdout.write("\n\nHere is your poem: \n\n")144sys.stdout.write(usr_input)145for i in range(400):146147x_pred = np.zeros((1, Tx, len(chars)))148149for t, char in enumerate(sentence):150if char != '0':151x_pred[0, t, char_indices[char]] = 1.152153preds = model.predict(x_pred, verbose=0)[0]154next_index = sample(preds, temperature = 1.0)155next_char = indices_char[next_index]156157generated += next_char158sentence = sentence[1:] + next_char159160sys.stdout.write(next_char)161sys.stdout.flush()162163if next_char == '\n':164continue165166