model.add(FullyConnectedLayer(len(chars))) model.add(ActivationLayer(Activations.softmax)) def sample(a, temperature=1.0): # helper function to sample an index from a probability array a = np.log(a) / temperature a = np.exp(a) / np.sum(np.exp(a)) return np.argmax(np.random.multinomial(1, a, 1)) # train the model, output generated text after each iteration for iteration in range(1, 60): print() print('-' * 50) print('Iteration', iteration) model.train(X, y, num_epochs=1) start_index = random.randint(0, len(text) - maxlen - 1) for diversity in [0.2, 0.5, 1.0, 1.2]: print() print('----- diversity:', diversity) generated = '' sentence = text[start_index: start_index + maxlen] generated += sentence print('----- Generating with seed: "' + sentence + '"') sys.stdout.write(generated) for iteration in range(400): x = np.zeros((1, maxlen, len(chars)))
for iteration in range(40): x = np.zeros((1, maxlen, len(chars))) for t, char in enumerate(sentence): x[0, t, char_indices[char]] = 1. preds = model.predict(x)[0] try: next_index = sample(preds, diversity) next_char = indices_char[next_index] generated += next_char sentence = sentence[1:] + next_char sys.stdout.write(next_char) sys.stdout.flush() except ValueError: print("Value Error") model.train(X_train, y_train, num_epochs=num_epochs, epoch_callback=predict) end_time_1 = time.clock() t1 = end_time_1 - start_time_1 ok.save_model(model) del model model = ok.load_model() model.reinforce(X_train, y_train, num_epochs=num_epochs, epoch_callback=predict) '''start_time_2 = time.clock() model_keras = Sequential() model_keras.add(GRU(h_layer_size, input_dim=len(chars), init='normal', return_sequences=False)) model_keras.add(BatchNormalization(mode=1))