-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjax_dqn_cartpole.py
189 lines (149 loc) · 6.28 KB
/
jax_dqn_cartpole.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import jax
import jax.numpy as jnp
import numpy as np
import gym
from collections import deque
import random
# Define the Q-network
def jax_q_network(states, num_actions, hidden_size):
"""
Q-network model to predict Q-values for given states.
Parameters:
states (jax.numpy.DeviceArray): The input states
num_actions (int): Number of possible actions
hidden_size (int): Number of units in the hidden layer
Returns:
jax.numpy.DeviceArray: Q-values for each action
"""
x = jax.nn.dense(states, hidden_size)
x = jax.nn.relu(x)
q_values = jax.nn.dense(x, num_actions)
return q_values
# Define the loss function
def jax_loss_fn(params, states, actions, rewards, next_states, dones, gamma):
"""
Compute the loss for Q-network.
Parameters:
params (dict): Model parameters
states (jax.numpy.DeviceArray): Batch of states
actions (jax.numpy.DeviceArray): Batch of actions
rewards (jax.numpy.DeviceArray): Batch of rewards
next_states (jax.numpy.DeviceArray): Batch of next states
dones (jax.numpy.DeviceArray): Batch of done flags
gamma (float): Discount factor
Returns:
jax.numpy.DeviceArray: Computed loss
"""
q_values = jax_q_network(states, num_actions=2, hidden_size=128)
next_q_values = jax_q_network(next_states, num_actions=2, hidden_size=128)
q_values = jnp.take_along_axis(q_values, actions.reshape(-1, 1), axis=1).squeeze()
next_q_values = jnp.max(next_q_values, axis=1)
targets = rewards + gamma * next_q_values * (1 - dones)
loss = jnp.mean(jax.lax.square(targets - q_values))
return loss
# Experience replay buffer
class ReplayBuffer:
def __init__(self, capacity):
"""
Initialize the replay buffer.
Parameters:
capacity (int): Maximum size of the buffer
"""
self.buffer = deque(maxlen=capacity)
def push(self, state, action, reward, next_state, done):
"""
Add experience to the buffer.
Parameters:
state (numpy.ndarray): Current state
action (int): Action taken
reward (float): Reward received
next_state (numpy.ndarray): Next state
done (bool): Whether the episode is done
"""
self.buffer.append((state, action, reward, next_state, done))
def sample(self, batch_size):
"""
Sample a batch of experiences from the buffer.
Parameters:
batch_size (int): Number of experiences to sample
Returns:
tuple: Batch of states, actions, rewards, next states, and done flags
"""
experiences = random.sample(self.buffer, batch_size)
states, actions, rewards, next_states, dones = zip(*experiences)
return np.array(states), np.array(actions), np.array(rewards), np.array(next_states), np.array(dones)
def __len__(self):
return len(self.buffer)
# Train the agent
def jax_train(env, params, optimizer, replay_buffer, num_episodes, batch_size, gamma, epsilon):
"""
Train the DQN agent.
Parameters:
env (gym.Env): The environment
params (dict): Model parameters
optimizer (jax.experimental.optimizers.Optimizer): Optimizer
replay_buffer (ReplayBuffer): Experience replay buffer
num_episodes (int): Number of episodes to train
batch_size (int): Batch size for training
gamma (float): Discount factor
epsilon (float): Exploration rate
Returns:
dict: Trained model parameters
"""
for episode in range(num_episodes):
state = env.reset()
done = False
episode_reward = 0
while not done:
if random.random() < epsilon:
action = env.action_space.sample()
else:
q_values = jax_q_network(state[np.newaxis], num_actions=2, hidden_size=128)
action = jnp.argmax(q_values)
next_state, reward, done, _ = env.step(action)
replay_buffer.push(state, action, reward, next_state, done)
state = next_state
episode_reward += reward
if len(replay_buffer) >= batch_size:
states, actions, rewards, next_states, dones = replay_buffer.sample(batch_size)
loss_value, grads = jax.value_and_grad(jax_loss_fn)(params, states, actions, rewards, next_states, dones, gamma)
params = optimizer.update(grads, params)
print(f"Episode {episode + 1}, Reward: {episode_reward}")
return params
# Main function
def main():
env = gym.make('CartPole-v1')
# Initialize Q-network parameters
rng = jax.random.PRNGKey(0)
params = jax.random.normal(rng, (4, 128))
# Initialize optimizer
optimizer = jax.optim.Adam(learning_rate=0.001)
# Initialize replay buffer
replay_buffer = ReplayBuffer(capacity=10000)
# Train the agent
params = jax_train(env, params, optimizer, replay_buffer, num_episodes=200, batch_size=64, gamma=0.99, epsilon=0.1)
# Evaluate the trained agent
state = env.reset()
done = False
episode_reward = 0
while not done:
q_values = jax_q_network(state[np.newaxis], num_actions=2, hidden_size=128)
action = jnp.argmax(q_values)
state, reward, done, _ = env.step(action)
episode_reward += reward
env.render()
print(f"Evaluation Reward: {episode_reward}")
env.close()
if __name__ == "__main__":
main()
# Possible Errors and Solutions:
# 1. ImportError: No module named 'gym'.
# Solution: Ensure that you have the gym library installed. Use `pip install gym`.
# 2. TypeError: 'DeviceArray' object is not callable.
# Solution: Ensure that all operations involving JAX arrays are correctly implemented using JAX functions.
# 3. ValueError: operands could not be broadcast together with shapes.
# Solution: Check the dimensions of your input data and model parameters to ensure they are compatible.
# 4. AttributeError: module 'jax.nn' has no attribute 'dense'.
# Solution: Make sure you are using the correct syntax for defining dense layers. You might need to implement custom dense layers if not using Flax.
# 5. IndexError: index out of bounds.
# Solution: Check the action selection logic and ensure valid actions are chosen within the range defined by the environment.