text stringlengths 0 4.99k |
|---|
layer2 = layers.Conv2D(64, 4, strides=2, activation=\"relu\")(layer1) |
layer3 = layers.Conv2D(64, 3, strides=1, activation=\"relu\")(layer2) |
layer4 = layers.Flatten()(layer3) |
layer5 = layers.Dense(512, activation=\"relu\")(layer4) |
action = layers.Dense(num_actions, activation=\"linear\")(layer5) |
return keras.Model(inputs=inputs, outputs=action) |
# The first model makes the predictions for Q-values which are used to |
# make a action. |
model = create_q_model() |
# Build a target model for the prediction of future rewards. |
# The weights of a target model get updated every 10000 steps thus when the |
# loss between the Q-values is calculated the target Q-value is stable. |
model_target = create_q_model() |
Train |
# In the Deepmind paper they use RMSProp however then Adam optimizer |
# improves training time |
optimizer = keras.optimizers.Adam(learning_rate=0.00025, clipnorm=1.0) |
# Experience replay buffers |
action_history = [] |
state_history = [] |
state_next_history = [] |
rewards_history = [] |
done_history = [] |
episode_reward_history = [] |
running_reward = 0 |
episode_count = 0 |
frame_count = 0 |
# Number of frames to take random action and observe output |
epsilon_random_frames = 50000 |
# Number of frames for exploration |
epsilon_greedy_frames = 1000000.0 |
# Maximum replay length |
# Note: The Deepmind paper suggests 1000000 however this causes memory issues |
max_memory_length = 100000 |
# Train the model after 4 actions |
update_after_actions = 4 |
# How often to update the target network |
update_target_network = 10000 |
# Using huber loss for stability |
loss_function = keras.losses.Huber() |
while True: # Run until solved |
state = np.array(env.reset()) |
episode_reward = 0 |
for timestep in range(1, max_steps_per_episode): |
# env.render(); Adding this line would show the attempts |
# of the agent in a pop up window. |
frame_count += 1 |
# Use epsilon-greedy for exploration |
if frame_count < epsilon_random_frames or epsilon > np.random.rand(1)[0]: |
# Take random action |
action = np.random.choice(num_actions) |
else: |
# Predict action Q-values |
# From environment state |
state_tensor = tf.convert_to_tensor(state) |
state_tensor = tf.expand_dims(state_tensor, 0) |
action_probs = model(state_tensor, training=False) |
# Take best action |
action = tf.argmax(action_probs[0]).numpy() |
# Decay probability of taking random action |
epsilon -= epsilon_interval / epsilon_greedy_frames |
epsilon = max(epsilon, epsilon_min) |
# Apply the sampled action in our environment |
state_next, reward, done, _ = env.step(action) |
state_next = np.array(state_next) |
episode_reward += reward |
# Save actions and states in replay buffer |
action_history.append(action) |
state_history.append(state) |
state_next_history.append(state_next) |
done_history.append(done) |
rewards_history.append(reward) |
state = state_next |
# Update every fourth frame and once batch size is over 32 |
if frame_count % update_after_actions == 0 and len(done_history) > batch_size: |
# Get indices of samples for replay buffers |
indices = np.random.choice(range(len(done_history)), size=batch_size) |
# Using list comprehension to sample from replay buffer |
state_sample = np.array([state_history[i] for i in indices]) |
state_next_sample = np.array([state_next_history[i] for i in indices]) |
rewards_sample = [rewards_history[i] for i in indices] |
action_sample = [action_history[i] for i in indices] |
done_sample = tf.convert_to_tensor( |
[float(done_history[i]) for i in indices] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.