Skip to content

Instantly share code, notes, and snippets.

@rouseguy
Created March 11, 2018 08:55
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/f1e715d34fec12287e6333519eb79125 to your computer and use it in GitHub Desktop.
Save rouseguy/f1e715d34fec12287e6333519eb79125 to your computer and use it in GitHub Desktop.
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