-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
101 lines (76 loc) · 2.61 KB
/
Copy pathtrain.py
File metadata and controls
101 lines (76 loc) · 2.61 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
import dqn
import gymnasium as gym
import numpy as np
import os
from datetime import datetime
import matplotlib.pyplot as plt
# HYPERPARAMETERS
EPSILON = 0.9
LR = 0.0001
GAMMA = 0.5
TARGET_NET_UPDATE_FREQ = 50
MAX_EPOCHS = 10000
MAX_EPISODES = 2000
MAX_EPISODES = 1000
# instantiate environment
env = gym.make("ALE/SpaceInvaders-v5", obs_type="grayscale")
observation, info = env.reset()
# instantiate dqn agent
agent = dqn.DQN(epsilon=EPSILON, lr=LR, gamma=GAMMA, update_freq=TARGET_NET_UPDATE_FREQ, num_actions=6)
# load trained weights
# agent.load()
# record training session time and date
now = datetime.now()
year = now.strftime("%Y")
month = now.strftime("%m")
day = now.strftime("%d")
time = now.strftime("%H%M%S")
date_time = now.strftime(" [%m_%d_%Y @ %H_%M_%S]")
# create folder to save training session information
dir_name = "results//Training Session" + str(date_time)
fig_dir_name = dir_name + "//figures"
weights_dir_name = dir_name + "//weights"
os.mkdir(dir_name)
os.mkdir(fig_dir_name)
os.mkdir(weights_dir_name)
# total reward tracker
rewards = []
# previous observation to tack onto the new one
prev_prev_observation = np.zeros(shape=(210, 160), dtype=np.uint8)
prev_observation = np.zeros(shape=(210, 160), dtype=np.uint8)
# main training loop
for episode in range(MAX_EPISODES):
# start the environment
observation, info = env.reset()
# episode total reward
episode_total_reward = 0
for epoch in range(MAX_EPOCHS):
# get action selection from DQN
markov_state = np.stack((prev_prev_observation, prev_observation, observation), axis=0)
action = agent.select_action(markov_state)
# save the previous observation
prev_prev_observation = prev_observation
prev_observation = observation
# take a step in the environment
observation, reward, terminated, truncated, info = env.step(action)
# reset the environment
if terminated or truncated:
final_epoch = epoch
break
# increment episode total reward
episode_total_reward += reward
# update the dqn
agent.train(markov_state, reward, epoch)
# save total episode reward
rewards.append(episode_total_reward)
# reset the environment
env.reset()
# save plot
plot_name = fig_dir_name + "//Total Reward vs Episode.png"
plt.plot(rewards)
plt.ylabel('Reward')
plt.xlabel('Episode')
plt.savefig(plot_name)
plt.close(plot_name)
# save agent weights
agent.save(weights_dir_name + "//dqn.pth")