-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
207 lines (172 loc) · 6.82 KB
/
Copy pathtrain.py
File metadata and controls
207 lines (172 loc) · 6.82 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# import gymnasium as gym
# from stable_baselines3 import DQN
# from stable_baselines3.dqn.policies import CnnPolicy
# import os
# # Create Pong environment
# env = gym.make("ALE/Pong-v5", render_mode="rgb_array")
# # Define the model using a Convolutional Neural Network (CNN) policy
# model = DQN(
# policy=CnnPolicy,
# env=env,
# learning_rate=0.0001, # Fine-tune for better performance
# gamma=0.99, # Discount factor
# batch_size=32, # Mini-batch size
# exploration_initial_eps=1.0, # Start with full exploration
# exploration_final_eps=0.1, # Minimum exploration
# exploration_fraction=0.1, # Fraction of training over which exploration decreases
# verbose=1,
# )
# # Train the model
# TIMESTEPS = 500_000 # Adjust based on computational power
# model.learn(total_timesteps=TIMESTEPS)
# # Save the trained model
# model_path = "dqn_pong_model.zip"
# model.save(model_path)
# print(f"Model saved to {model_path}")
# # Close environment
# env.close()
#!/usr/bin/env python3
"""Training an agent"""
import os
import gymnasium as gym
from stable_baselines3 import DQN
from stable_baselines3.common.callbacks import CheckpointCallback, EvalCallback
from stable_baselines3.common.monitor import Monitor
from stable_baselines3.common.vec_env import DummyVecEnv
import ale_py
class BreakoutAgent:
"""Handling the setup, execution"""
def __init__(self, model_directory="models", log_dir="logs", total_timesteps=50000):
"""Setting up the agent, including environment and model loading"""
self.model_directory = model_directory
self.log_dir = log_dir
self.total_timesteps = total_timesteps
# Creating the directories
os.makedirs(self.model_directory, exist_ok=True)
os.makedirs(self.log_dir, exist_ok=True)
# Initializing environments
self.env = self._create_wrapped_env()
self.eval_env = self._create_wrapped_env()
# DQN model
self.model = self._initialize_model()
@staticmethod
def _create_env(render_mode=None):
"""Creating the Breakout environment"""
env = gym.make("ALE/Breakout-v5", render_mode=render_mode)
env = Monitor(env)
return env
def _create_wrapped_env(self):
"""Vectorized environment"""
return DummyVecEnv([lambda: self._create_env()])
def _initialize_model(self):
"""Initializing the model"""
return DQN(
"CnnPolicy",
self.env,
learning_rate=1e-4,
buffer_size=10000,
learning_starts=1000,
batch_size=32,
gamma=0.99,
exploration_fraction=0.1,
exploration_initial_eps=1.0,
exploration_final_eps=0.05,
train_freq=4,
gradient_steps=1,
target_update_interval=1000,
verbose=1,
tensorboard_log=self.log_dir,
)
def train(self):
"""Training the DQN agent"""
checkpoint_callback = CheckpointCallback(
save_freq=10000, save_path=self.model_directory, name_prefix="dqn_breakout"
)
eval_callback = EvalCallback(
self.eval_env,
best_model_save_path=f"{self.model_directory}/best_model",
log_path=self.log_dir,
eval_freq=10000,
deterministic=True,
render=False,
)
# Training
self.model.learn(
total_timesteps=self.total_timesteps,
callback=[checkpoint_callback, eval_callback],
progress_bar=True,
)
# Saving the model
self.model.save(f"{self.model_directory}/policy.zip")
print(f"Training completed! Model saved as '{self.model_directory}/policy.zip'")
# def load_model(self):
# """Loading the DQN agent's trained model"""
# try:
# loaded_model = DQN.load(self.model_file) #Loading model from .zip file
# print(f"Model loaded successfully from: {self.model_file}")
# return loaded_model
# except Exception as e:
# print(f"Failed to load model: {e}")
# raise RuntimeError("Check the model file")
def execute(self, episodes=5):
"""Allowing the agent to play the game for 5 episodes"""
for ep in range(episodes):
observation = self.eval_env.reset()
total_points = 0
done = False
# move_count = 0
while not done:
# Selecting the action using the pre-trained model
action, _ = self.model.predict(observation, deterministic=True)
observation, reward, done, _ = self.eval_env.step(action)
total_points += reward
# move_count += 1
# done = terminated or truncated
print(
f"Episode {ep + 1}: Total Score: {total_points}"
)
self.env.close()
# def convert_to_h5(self, h5_filepath="models/agent_policy.h5"):
# """Converting the trained model (PyTorch) to an HDF5 (.h5) file for compatibility to avoid errors"""
# print("Initiating PyTorch to HDF5 model conversion...")
# # Accessing the PyTorch layers
# torch_model = self.agent_model.policy.q_net
# # Creating a Keras sequential model and converting each layer
# keras_model = tf.keras.Sequential()
# for layer in torch_model.children():
# if isinstance(layer, torch.nn.Conv2d):
# keras_model.add(
# tf.keras.layers.Conv2D(
# filters=layer.out_channels,
# kernel_size=layer.kernel_size,
# strides=layer.stride,
# activation="relu",
# padding="valid",
# input_shape=(210, 160, 3),
# )
# )
# elif isinstance(layer, torch.nn.Linear):
# keras_model.add(
# tf.keras.layers.Dense(
# units=layer.out_features,
# activation="relu"
# if layer.weight.shape[0] > layer.weight.shape[1]
# else None,
# )
# )
# elif isinstance(layer, torch.nn.Flatten):
# keras_model.add(tf.keras.layers.Flatten())
# # Saving the Keras model
# keras_model.save(h5_filepath)
# print(f"HDF5 model saved successfully at: {h5_filepath}")
def run_agent():
"""Initialization and gameplay of the Breakout agent"""
gym.register_envs(ale_py)
# Initialize the trainer
trainer = BreakoutAgent(total_timesteps=50000)
# Train the agent
trainer.train()
# Evaluate the agent
trainer.evaluate(episodes=5)
if __name__ == "__main__":
run_agent()