-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
282 lines (190 loc) · 8.02 KB
/
main.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
from csuite.environments import dancing_catch
from csuite.environments import catch
from torch import nn
import torch
from collections import deque
import numpy as np
import random
import itertools
import pickle
import warnings
warnings.filterwarnings('ignore')
# Hyperparameters
GAMMA = 0.9 #Care more about recent steps
BATCH_SIZE = 32
BUFFER_SIZE = 1000
MIN_REPLAY_SIZE = 32 #To Do #1000 => Care less about previous ones
MAX_STEPS = 2000000
TARGET_UPDATE_FREQ = 128
HIDDEN_LAYERS_SIZE = 64 #To Do 32 for crelu
LEARNING_RATE = 0.01
SWAP_EVERY = 10000
SEED = 6555 #should be loop
AVERAGE_WINDOW_SIZE = 1000
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
#huber loss for now
criterion = nn.functional.smooth_l1_loss
def set_random_seed():
random_number = np.random.randint(SEED)
print("Random Seed is: ", random_number)
random.seed(random_number)
np.random.seed(random_number)
torch.random.manual_seed(random_number)
torch.cuda.manual_seed(random_number)
# When running on the CuDNN backend, two further options must be set
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
class DQN(nn.Module):
def __init__(self, env, hidden_size=128,name=''):
super(DQN, self).__init__()
in_features = int(np.product(env.observation_spec().shape))
n_actions = env.action_spec().num_values
self.fc1 = nn.Linear(in_features=in_features, out_features=hidden_size).to(device)
self.fc2 = nn.Linear(in_features=hidden_size, out_features=n_actions).to(device) # multiply by 2 because of the CRelu
self.plate = None
self.name=name
self.fc1_input=None
self.fc1_output=None
self.act_output=None
self.output_output=None
self.to(device)
def crelu(self, x):
# Apply ReLU separately to positive and negative parts
return torch.cat((torch.relu(x), torch.relu(-x)), dim=1) # Concatenate along dim=1
def forward(self, x):
self.fc1_input=x
output = self.fc1(x)
self.fc1_output=output
# Relu
output = nn.ReLU()(output)
self.act_output=output
# Leaky Relu
# output = nn.LeakyReLU(0.1)(output)
# CRelu
# output = self.crelu(output)
self.plate = output
output = self.fc2(output)
self.output_output=output
return output
def act(self, obs):
with torch.no_grad():
obs = torch.tensor(obs, dtype=torch.float32).unsqueeze(0).to(device)
q_values = self(obs)
max_q_idx = torch.argmax(q_values, dim=1).squeeze() # this needs random tie breaking
action = max_q_idx.detach().item()
return action
def sample_action(env):
actions = np.arange(env.action_spec().num_values)
return np.random.choice(actions)
env = dancing_catch.DancingCatch(swap_every=SWAP_EVERY)
replay_memory = deque(maxlen=BUFFER_SIZE)
online_network = DQN(env, hidden_size=HIDDEN_LAYERS_SIZE, name='online')
target_network = DQN(env, hidden_size=HIDDEN_LAYERS_SIZE, name='target')
target_network.load_state_dict(online_network.state_dict())
optimizer = torch.optim.Adam(online_network.parameters(), lr=LEARNING_RATE) # change to regular beta1: 0.9, beta2 0.99
#initilize Replay Memory
obs = env.start().flatten()
for _ in range(MIN_REPLAY_SIZE):
action = sample_action(env)
new_obs, reward = env.step(action)
new_obs = new_obs.flatten()
transition = (obs, action, reward, new_obs)
replay_memory.append(transition)
obs = new_obs
from tqdm import tqdm
def write_list_to_file(my_list, file_name):
try:
with open(file_name, 'w') as file:
for item in my_list:
file.write(f"{item}\n")
print(f"List has been written to {file_name} successfully.")
except IOError:
print("Error: Unable to write to file.")
hidden_layer_weight = []
hidden_layer_gradient =[]
activations = []
train_losses = []
train_losses2 = []
l2train_losses=[]
rewards_log = []
catch_or_miss = [0]
p_bar = tqdm(range(MAX_STEPS))
gloss = 0
total_reward = 0
set_random_seed()
for step in p_bar:
#Epsilon greedy with decaying epsilon between [EPSILON_START, EPSILON_END]
epsilon = 0.1 #fix
rnd_sample = random.random()
if rnd_sample <= epsilon:
action = sample_action(env)
else:
action = online_network.act(obs)
new_obs, reward = env.step(action)
new_obs = new_obs.flatten()
transition = (obs, action, reward, new_obs)
replay_memory.append(transition)
obs = new_obs
if (reward == 1):
catch_or_miss.append(1)
elif(reward == -1):
catch_or_miss.append(0)
avg_window = catch_or_miss[max(0, len(catch_or_miss) - AVERAGE_WINDOW_SIZE): ]
reward_avg = sum(avg_window) / len(avg_window)
rewards_log.append(reward_avg)
transitions = random.sample(replay_memory, BATCH_SIZE)
# Deep Learning part
observations = np.asarray([t[0] for t in transitions])
actions = np.asarray([t[1] for t in transitions])
rewards = np.asarray([t[2] for t in transitions])
new_observations = np.asarray([t[3] for t in transitions])
observations = torch.tensor(observations, dtype=torch.float32).to(device) #(BATCH_SIZE, 50)
actions = torch.tensor(actions, dtype=torch.int64).unsqueeze(1).to(device) #(BATCH_SIZE, 1)
rewards = torch.tensor(rewards, dtype=torch.float32).unsqueeze(1).to(device) #(BATCH_SIZE, 1)
new_observations = torch.tensor(new_observations, dtype=torch.float32).to(device) #(BATCH_SIZE, 50)
target_q = target_network(new_observations) #(BATCH_SIZE, 3)
max_target_q = target_q.max(dim=1, keepdim=True)[0]
targets = rewards + GAMMA * max_target_q
#Compute loss
q_values = online_network(observations)
action_q_values = torch.gather(q_values, dim=1, index=actions).to(device)
loss = criterion(action_q_values, targets)
mean_loss = loss.detach().cpu().item() / BATCH_SIZE
gloss += mean_loss
train_losses2.append(mean_loss)
if (step + 1 ) % 5000 == 0:
train_losses.append(mean_loss)
p_bar.set_description(f"gLoss: {gloss / (step + 1) * 1000:0.5f}, reward: {reward_avg:0.5f}, mean loss: {mean_loss:.5f}")
#Gradient Descent
optimizer.zero_grad()
loss.backward()
#Clip gradients
for param in online_network.parameters():
param.grad.data.clamp_(-1, 1)
optimizer.step()
# Update target net
if (step + 1) % TARGET_UPDATE_FREQ == 0:
target_network.load_state_dict(online_network.state_dict())
if (step + 1 ) % 5000 == 0:
hidden_layer_weight.append(online_network.fc1.weight.data.detach().clone())
# Get gradients of hidden layer weights
hidden_layer_gradient.append(online_network.fc1.weight.grad.detach().clone())
activations.append(online_network.plate)
# print("online_network.fc1_input: ", online_network.fc1_input.shape)
# print("online_network.fc1_input: ", online_network.fc1_output.shape)
# print("online_network.fc1_input: ",online_network.act_output.shape)
# print("online_network.output_output: ", online_network.output_output.shape)
activationFunction ='Relu'
myseed =1
path = f"seed{myseed}_maxsteps{MAX_STEPS}_swap{SWAP_EVERY}_{activationFunction}_"
with open(path + 'activations.pkl', 'wb') as f:
pickle.dump(activations, f)
with open(path + 'hidden_layer_gradient.pkl', 'wb') as f:
pickle.dump(hidden_layer_gradient, f)
with open(path + 'hidden_layer_weight.pkl', 'wb') as f:
pickle.dump(hidden_layer_weight, f)
with open(path + 'loss.pkl', 'wb') as f:
pickle.dump(train_losses, f)
with open(path + 'reward.pkl', 'wb') as f:
pickle.dump(rewards_log, f)
print('Saved!')