|
| 1 | +import numpy as np |
| 2 | +import os |
| 3 | +import pickle |
| 4 | +from keras.models import Sequential |
| 5 | +from keras.layers import Dense, LSTM, Dropout |
| 6 | +from keras.callbacks import ModelCheckpoint |
| 7 | +from string import punctuation |
| 8 | + |
| 9 | +# commented because already downloaded |
| 10 | +# import requests |
| 11 | +# content = requests.get("http://www.gutenberg.org/cache/epub/11/pg11.txt").text |
| 12 | +# open("data/wonderland.txt", "w", encoding="utf-8").write(content) |
| 13 | + |
| 14 | +# read the data |
| 15 | +text = open("data/wonderland.txt", encoding="utf-8").read() |
| 16 | +# remove caps |
| 17 | +text = text.lower() |
| 18 | +# remove punctuation |
| 19 | +text = text.translate(str.maketrans("", "", punctuation)) |
| 20 | +# print some stats |
| 21 | +n_chars = len(text) |
| 22 | +unique_chars = ''.join(sorted(set(text))) |
| 23 | +print("unique_chars:", unique_chars) |
| 24 | +n_unique_chars = len(unique_chars) |
| 25 | +print("Number of characters:", n_chars) |
| 26 | +print("Number of unique characters:", n_unique_chars) |
| 27 | + |
| 28 | +# dictionary that converts characters to integers |
| 29 | +char2int = {c: i for i, c in enumerate(unique_chars)} |
| 30 | +# dictionary that converts integers to characters |
| 31 | +int2char = {i: c for i, c in enumerate(unique_chars)} |
| 32 | + |
| 33 | +# save these dictionaries for later generation |
| 34 | +pickle.dump(char2int, open("wonderland-char2int.pickle", "wb")) |
| 35 | +pickle.dump(int2char, open("wonderland-int2char.pickle", "wb")) |
| 36 | + |
| 37 | +# hyper parameters |
| 38 | +sequence_length = 100 |
| 39 | +step = 1 |
| 40 | +batch_size = 128 |
| 41 | +epochs = 40 |
| 42 | + |
| 43 | +sentences = [] |
| 44 | +y_train = [] |
| 45 | +for i in range(0, len(text) - sequence_length, step): |
| 46 | + sentences.append(text[i: i + sequence_length]) |
| 47 | + y_train.append(text[i+sequence_length]) |
| 48 | +print("Number of sentences:", len(sentences)) |
| 49 | + |
| 50 | +# vectorization |
| 51 | +X = np.zeros((len(sentences), sequence_length, n_unique_chars)) |
| 52 | +y = np.zeros((len(sentences), n_unique_chars)) |
| 53 | + |
| 54 | +for i, sentence in enumerate(sentences): |
| 55 | + for t, char in enumerate(sentence): |
| 56 | + X[i, t, char2int[char]] = 1 |
| 57 | + y[i, char2int[y_train[i]]] = 1 |
| 58 | + |
| 59 | +print("X.shape:", X.shape) |
| 60 | + |
| 61 | +# building the model |
| 62 | +# model = Sequential([ |
| 63 | +# LSTM(128, input_shape=(sequence_length, n_unique_chars)), |
| 64 | +# Dense(n_unique_chars, activation="softmax"), |
| 65 | +# ]) |
| 66 | + |
| 67 | +# a better model (slower to train obviously) |
| 68 | +model = Sequential([ |
| 69 | + LSTM(256, input_shape=(sequence_length, n_unique_chars), return_sequences=True), |
| 70 | + Dropout(0.3), |
| 71 | + LSTM(256), |
| 72 | + Dense(n_unique_chars, activation="softmax"), |
| 73 | +]) |
| 74 | + |
| 75 | +# model.load_weights("results/wonderland-v2-2.48.h5") |
| 76 | + |
| 77 | +model.summary() |
| 78 | +model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"]) |
| 79 | + |
| 80 | +if not os.path.isdir("results"): |
| 81 | + os.mkdir("results") |
| 82 | + |
| 83 | +checkpoint = ModelCheckpoint("results/wonderland-v2-{loss:.2f}.h5", verbose=1) |
| 84 | + |
| 85 | +# train the model |
| 86 | +model.fit(X, y, batch_size=batch_size, epochs=epochs, callbacks=[checkpoint]) |
0 commit comments