Skip to content

Instantly share code, notes, and snippets.

@rouseguy
Last active March 11, 2018 07:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rouseguy/231a935fc74c6a75966df326f6564cbb to your computer and use it in GitHub Desktop.
Save rouseguy/231a935fc74c6a75966df326f6564cbb to your computer and use it in GitHub Desktop.
import numpy as np
def input_generate_data(data, SEQ_LENGTH=SEQ_LENGTH, VOCAB_SIZE=VOCAB_SIZE, char_to_ix = 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