from rl.agents.dqn import DQNAgent from rl.memory import SequentialMemory from rl.policy import EpsGreedyQPolicy from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense, Flatten, Conv2D from tensorflow.keras.optimizers import Adam import gym env = gym.make('SpaceInvaders-v0') nb_actions = env.action_space.n model = Sequential() model.add(Conv2D(32, kernel_size=8, strides=4, activation='relu', input_shape=env.observation_space.shape)) model.add(Conv2D(64, kernel_size=4, strides=2, activation='relu')) model.add(Conv2D(64, kernel_size=3, strides=1, activation='relu')) model.add(Flatten()) model.add(Dense(512, activation='relu')) model.add(Dense(nb_actions, activation='linear')) memory = SequentialMemory(limit=1000000, window_length=4) policy = EpsGreedyQPolicy() dqn = DQNAgent(model=model, nb_actions=nb_actions, memory=memory, nb_steps_warmup=50000, target_model_update=10000, policy=policy) dqn.compile(Adam(lr=0.00025), metrics=['mae']) dqn.fit(env, nb_steps=1000000, visualize=False, verbose=2)
from rl.agents.dqn import DQNAgent from rl.memory import SequentialMemory from rl.policy import EpsGreedyQPolicy from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense from tensorflow.keras.optimizers import Adam import gym import gym_robot_arm env = gym.make('RobotArm-v0') nb_actions = env.action_space.n model = Sequential() model.add(Dense(32, activation='relu', input_shape=env.observation_space.shape)) model.add(Dense(32, activation='relu')) model.add(Dense(nb_actions, activation='linear')) memory = SequentialMemory(limit=1000000, window_length=1) policy = EpsGreedyQPolicy() dqn = DQNAgent(model=model, nb_actions=nb_actions, memory=memory, nb_steps_warmup=5000, target_model_update=10000, policy=policy) dqn.compile(Adam(lr=0.001), metrics=['mae']) dqn.fit(env, nb_steps=100000, visualize=False, verbose=2)These examples show how the `rl.agents.dqn` DQNAgent can be used to apply reinforcement learning to a variety of problems, from playing games to controlling robots.