Created
March 11, 2018 08:55
-
-
Save rouseguy/f1e715d34fec12287e6333519eb79125 to your computer and use it in GitHub Desktop.
This file contains 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
X = np.zeros((int(len(data)/SEQ_LENGTH), SEQ_LENGTH, VOCAB_SIZE)) | |
y = np.zeros((int(len(data)/SEQ_LENGTH), SEQ_LENGTH, VOCAB_SIZE)) | |
for i in range(0, int(len(data)/SEQ_LENGTH)): | |
X_sequence = data[i*SEQ_LENGTH:(i+1)*SEQ_LENGTH] | |
X_sequence_ix = [char_to_ix[value] for value in X_sequence] | |
input_sequence = np.zeros((SEQ_LENGTH, VOCAB_SIZE)) | |
for j in range(SEQ_LENGTH): | |
input_sequence[j][X_sequence_ix[j]] = 1. | |
X[i] = input_sequence | |
y_sequence = data[i*SEQ_LENGTH+1:(i+1)*SEQ_LENGTH+1] | |
y_sequence_ix = [char_to_ix[value] for value in y_sequence] | |
target_sequence = np.zeros((SEQ_LENGTH, VOCAB_SIZE)) | |
for j in range(SEQ_LENGTH): | |
target_sequence[j][y_sequence_ix[j]] = 1. | |
y[i] = target_sequence | |
def generate_text(model, length): | |
ix = [np.random.randint(VOCAB_SIZE)] | |
y_char = [ix_to_char[ix[-1]]] | |
X = np.zeros((1, length, VOCAB_SIZE)) | |
for i in range(length): | |
X[0, i, :][ix[-1]] = 1 | |
print(ix_to_char[ix[-1]], end="") | |
ix = np.argmax(model.predict(X[:, :i+1, :])[0], 1) | |
y_char.append(ix_to_char[ix[-1]]) | |
return ('').join(y_char) | |
nb_epoch = 0 | |
while nb_epoch < 10: | |
print('\n\n') | |
model.fit(X, y, batch_size=BATCH_SIZE, verbose=1, nb_epoch=1) | |
nb_epoch += 1 | |
generate_text(model, GENERATE_LENGTH) | |
if nb_epoch % 10 == 0: | |
model.save_weights('checkpoint_{}_epoch_{}.hdf5'.format(HIDDEN_DIM, nb_epoch)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment