示例#1
0
# serialize model to JSON
model_save = model.to_json()
with open("save/NNmodel1.json", "w") as json_file:
    json_file.write(model_save)

print("Saved model to disk!")



# Finally, we configure and compile our agent. You can use every built-in Keras optimizer and
# even the metrics!
memory = SequentialMemory(limit=10000, window_length=1)
#policy1 = BoltzmannQPolicy()
policy1 = EpsDisGreedyQPolicy(eps=0.0001, eps_decay=0.999)
policy2 = BoltzmannQPolicy()
callback1 = FileLogger(filepath='save/nhistory1_{}'.format(timenow), interval=1)
callback2 = FileLogger(filepath='save/nhistory2_{}'.format(timenow), interval=1)
callback3 = FileLogger(filepath='save/nhistory3_{}'.format(timenow), interval=1)
callback4 = FileLogger(filepath='save/nhistory4_{}'.format(timenow), interval=1)



#dqn1 = DQNAgent(model=model, nb_actions=nb_actions, memory=memory, batch_size=64, nb_steps_warmup=2000,
#               target_model_update=1e-2, policy=policy2)
#dqn1.compile(Adam(lr=1e-3), metrics=['mae'])
#dqn1.fit(env, nb_steps=300000, visualize=False, callbacks=[callback1], verbose=2)

#dqn2 = DQNAgent(model=model, nb_actions=nb_actions, memory=memory, batch_size=64, gamma=0.5, nb_steps_warmup=2000,
#               target_model_update=1e-2, policy=policy2)
#dqn2.compile(Adam(lr=1e-3), metrics=['mae'])
#dqn2.fit(env, nb_steps=300000, visualize=False, callbacks=[callback2], verbose=2)
x = Dense(24)(x)
x = Activation('relu')(x)
x = Dense(1)(x)
x = Activation('linear')(x)
critic = Model(inputs=[action_input, observation_input], outputs=x)
print(critic.summary())

model_actor = actor.to_json()
with open("save/Actor.json", "w") as json_file:
    json_file.write(model_actor)
model_critic = critic.to_json()
with open("save/Critic.json", "w") as json_file:
    json_file.write(model_critic)
print("Saved model to disk!")

callback1 = FileLogger(filepath='save/history1_{}'.format(timenow), interval=1)
#callback2 = FileLogger(filepath='save/history2_{}'.format(timenow), interval=1)
#callback3 = FileLogger(filepath='save/history3_{}'.format(timenow), interval=1)
#callback4 = FileLogger(filepath='save/history4_{}'.format(timenow), interval=1)

memory = SequentialMemory(limit=50000, window_length=1)
random_process = OrnsteinUhlenbeckProcess(size=nb_actions,
                                          theta=.15,
                                          mu=0.,
                                          sigma=.3)
agent = DDPGAgent(nb_actions=nb_actions,
                  actor=actor,
                  critic=critic,
                  critic_action_input=action_input,
                  memory=memory,
                  nb_steps_warmup_critic=1000,