This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| from matplotlib import animation, cm | |
| from mpl_toolkits.mplot3d import Axes3D | |
| # create a figure | |
| fig = plt.figure() | |
| # initialise 3D Axes | |
| ax = Axes3D(fig) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import requests | |
| import re | |
| # import Meditations | |
| response = requests.get('http://classics.mit.edu/Antoninus/meditations.mb.txt') | |
| data = response.text | |
| # clean the text | |
| data = data.split("Translated by George Long")[1].replace("-", "").split("THE END")[0] | |
| data = re.sub("BOOK [A-Z]+\n", "", data) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| char2idx = {c:i for i, c in enumerate(vocab)} | |
| idx2char = np.array(vocab) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| model = tf.keras.Sequential([ | |
| tf.keras.layers.Embedding(len(vocab), EMBED_DIM, | |
| batch_input_shape=[BATCH_SIZE, None]), | |
| tf.keras.layers.GRU(UNITS, return_sequences=True, | |
| stateful=True, | |
| dropout=0.1), | |
| tf.keras.layers.Dense(len(vocab)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| checkpoint = tf.keras.callbacks.ModelCheckpoint( | |
| filepath='./training_checkpoints/ckpt_{epoch}', | |
| save_weights_only=True | |
| ) | |
| history = model.fit(dataset, epochs=EPOCHS, callbacks=[checkpoint_callback]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| model = build_model(len(vocab), EMBED_DIM, UNITS, 1) | |
| model.load_weights(tf.train.latest_checkpoint('./training_checkpoints') | |
| model.build(tf.TensorShape([1, None])) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| meditations = "From " # initialize our meditations text | |
| # convert to indices | |
| input_eval = tf.expand_dims([char2idx[c] for x in meditations], 0) | |
| # initialize states | |
| model.reset_states() | |
| # loop through, generating 100K characters | |
| for i in range(100000): | |
| y_hat = model(input_eval) # make a prediction | |
| y_hat = tf.squeeze(y_hat, 0) # remove batch dimension | |
| predicted_idx = tf.random.categorical(y_hat, num_samples=1)[-1,0].numpy() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| char_dataset = tf.data.Dataset.from_tensor_slices(data_idx) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| sequences = char_dataset.batch(SEQ_LEN+1, drop_remainder=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def split_input_output(chunk): | |
| return chunk[:-1], chunk[1:] | |
| dataset = sequences.map(split_input_output) |