Beispiel #1
0
from x.agent import DiscreteAgent

num_actions = 3
nb_filters, nb_rows, nb_cols = 32, 3, 3

# keras model
keras_model = Sequential()
keras_model.add(Dense(args.hidden, activation="relu", input_dim=args.grid**2))
keras_model.add(Dense(args.hidden, activation="relu"))
keras_model.add(Dense(num_actions))

# X wrapper for Keras
model = KerasModel(keras_model)

# Memory
M = ExperienceReplay(memory_length=args.memory)

# Agent
A = DiscreteAgent(model, M)
# SGD optimizer + MSE cost + MAX policy = Q-learning as we know it
A.compile(optimizer=SGD(lr=0.2), loss="mse", policy_rule="max")

# To run an experiment, the Agent needs an Enviroment to iteract with
catcher = Catcher(grid_size=args.grid)
A.learn(catcher, epoch=args.epoch, batch_size=args.batch)

# Test the agent following the learned policy
A.play(catcher,
       epoch=100,
       visualize={
           'filepath': args.output,
Beispiel #2
0
    # learning model
    keras_model = Sequential()
    keras_model.add(
        Dense(hidden_dim,
              activation="softplus",
              input_dim=agent_env.n_features))
    keras_model.add(Dropout(0.5))
    keras_model.add(Dense(hidden_dim, activation="softplus"))
    keras_model.add(Dropout(0.5))
    keras_model.add(Dense(hidden_dim, activation="softplus"))
    keras_model.add(Dropout(0.5))
    keras_model.add(Dense(agent_env.n_actions, activation="softmax"))
    agent_model = KerasModel(keras_model)

    # experience memory
    agent_mem = ExperienceReplay(memory_length=memory_len)

    # compile agent
    agent = DiscreteAgent(agent_model,
                          agent_mem,
                          epsilon=lambda *args: epsilon)
    # SGD optimizer + MSE cost + MAX policy = Q-learning as we know it
    #agent.compile(optimizer=RMSprop(lr=0.001), loss='mse', policy_rule='max')
    agent.compile(optimizer=RMSprop(lr=0.001),
                  loss='categorical_crossentropy',
                  policy_rule='max')

    # train agent
    agent.learn(agent_env, epoch=epochs, batch_size=batch_size, gamma=gamma)

    # save trained model and weights