Last active
October 17, 2024 15:09
-
-
Save tehZevo/48fda3a94fd1abbbb25b237b78dabd24 to your computer and use it in GitHub Desktop.
Fix bugs in GeeksforGeeks A2C example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import tensorflow as tf | |
import gymnasium as gym | |
# Create the CartPole Environment | |
env = gym.make('CartPole-v1') | |
# Define the actor and critic networks | |
actor = tf.keras.Sequential([ | |
tf.keras.layers.Dense(32, activation='relu'), | |
tf.keras.layers.Dense(env.action_space.n, activation='softmax') | |
]) | |
critic = tf.keras.Sequential([ | |
tf.keras.layers.Dense(32, activation='relu'), | |
tf.keras.layers.Dense(1) | |
]) | |
# Define optimizer and loss functions | |
actor_optimizer = tf.keras.optimizers.Adam(learning_rate=0.001) | |
critic_optimizer = tf.keras.optimizers.Adam(learning_rate=0.001) | |
# Main training loop | |
num_episodes = 1000 | |
gamma = 0.99 | |
n_episodes_rewards = [] | |
for episode in range(num_episodes): | |
state, reset_info = env.reset() | |
episode_reward = 0 | |
for t in range(1, 10000): # Limit the number of time steps | |
with tf.GradientTape(persistent=True) as tape: | |
# Choose an action using the actor | |
action_probs = actor(np.array([state])) | |
action = np.random.choice(env.action_space.n, p=action_probs.numpy()[0]) | |
# Take the chosen action and observe the next state and reward | |
next_state, reward, done, terminated, _ = env.step(action) | |
# Compute the advantage | |
state_value = critic(np.array([state]))[0, 0] | |
next_state_value = critic(np.array([next_state]))[0, 0] | |
advantage = reward + gamma * next_state_value - state_value | |
# Compute actor and critic losses | |
actor_loss = -tf.math.log(action_probs[0, action]) * advantage | |
critic_loss = tf.square(advantage) | |
episode_reward += reward | |
state = next_state | |
# Update actor and critic | |
actor_gradients = tape.gradient(actor_loss, actor.trainable_variables) | |
critic_gradients = tape.gradient(critic_loss, critic.trainable_variables) | |
actor_optimizer.apply_gradients(zip(actor_gradients, actor.trainable_variables)) | |
critic_optimizer.apply_gradients(zip(critic_gradients, critic.trainable_variables)) | |
if done or terminated: | |
break | |
n_episodes_rewards.append(episode_reward) | |
if episode % 10 == 0: | |
print(f"Episode {episode}, Average episode reward: {np.mean(n_episodes_rewards)}") | |
n_episodes_rewards = [] | |
env.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Program output: