Last active
March 11, 2018 07:41
-
-
Save rouseguy/5c8221359d0d7f6e3f59817b3e3b5547 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
import numpy as np | |
def input_generate_data(data, SEQ_LENGTH=20, VOCAB_SIZE=30, char_to_ix = {}): | |
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 | |
return X, y | |
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment