-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAgent.py
More file actions
184 lines (137 loc) · 5.75 KB
/
Agent.py
File metadata and controls
184 lines (137 loc) · 5.75 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
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import gymnasium as gym
import numpy as np
import pickle
class Agent(nn.Module):
def __init__(self, name='model', input_num=None, output_num=None):
super().__init__()
assert input_num is not None
assert output_num is not None
self.input_num = input_num
self.output_num = output_num
self.conv1 = nn.Conv2d(1,4,5)
self.norm1 = nn.BatchNorm2d(4)
self.conv2 = nn.Conv2d(4,8,5)
self.norm2 = nn.BatchNorm2d(8)
self.conv3 = nn.Conv2d(8,16,6)
self.norm3 = nn.BatchNorm2d(16)
self.lin0 = nn.Linear(1024,768)
self.lin1 = nn.Linear(768,64)
self.lin2 = nn.Linear(64,16)
self.lin3 = nn.Linear(16,3)
self.maxpool = nn.MaxPool2d(2)
self.dropout_conv = nn.Dropout2d(0.4) # For convolutional layers
self.dropout_fc = nn.Dropout(0.4) # For fully connected layers
self.relu = nn.ReLU()
self.flatten = nn.Flatten()
def forward(self,x):
x = self.conv1(x)
x = self.norm1(x)
x = self.maxpool(x)
x = self.relu(x)
x = self.dropout_conv(x)
x = self.conv2(x)
x = self.norm2(x)
x = self.maxpool(x)
x = self.relu(x)
x = self.dropout_conv(x)
x = self.conv3(x)
x = self.norm3(x)
x = self.maxpool(x)
x = self.relu(x)
x = self.dropout_conv(x)
x = self.flatten(x)
x = self.lin0(x)
x = self.relu(x)
x = self.dropout_fc(x)
x = self.lin1(x)
x = self.relu(x)
x = self.dropout_fc(x)
x = self.lin2(x)
x = self.relu(x)
x = self.dropout_fc(x)
x = self.lin3(x)
x = self.relu(x)
return x
def train_model(self, x, y, n_epoch=100, batch=32, session_id=None):
"""Train the network"""
loss_fn = nn.MSELoss()
optimizer = optim.Adam(self.parameters(), lr=0.001)
#x = torch.tensor(x, dtype=torch.float32)
#y = torch.tensor(y, dtype=torch.float32)
x = torch.stack(x)
y = torch.stack(y)
print(f'x shape: {x.shape}, y shape: {y.shape}')
dataset = torch.utils.data.TensorDataset(x,y)
dataloader = torch.utils.data.DataLoader(dataset, batch_size=batch, shuffle= True)
Avg_epoch_loss_list = []
#Training Loop
for epoch in range(n_epoch):
epoch_loss = 0.0
self.train()
n = 0
for batc_x, batch_y in dataloader:
optimizer.zero_grad()
prediction = self(batc_x)
loss = loss_fn(prediction, batch_y)
print(f'prediction{prediction[1]} true value: {batch_y[1]}') if n == 50 else None
loss.backward()
optimizer.step()
epoch_loss += loss.item()
n += 1
Avg_Epoch_loss = epoch_loss/n
Avg_epoch_loss_list.append(Avg_Epoch_loss)
print(f"Epoch {epoch + 1}/{n_epoch}, Loss: {epoch_loss:.4f}, Avg. Epoc Loss:{Avg_Epoch_loss}")
#torch.save(Avg_epoch_loss_list, f'./Data/{session_id}.pt')
with open(f'./Data/{session_id}.pkl', 'wb') as file:
pickle.dump(Avg_epoch_loss_list, file)
def predict(self, x):
self.eval()
with torch.no_grad():
x = x.reshape(1, 1, 96, 96)
predictions = self(x) #is equivalent to self.forward(batc_x)
return predictions.numpy()
def load(self, path):
self.load_state_dict(torch.load(path))
def save(self, path):
torch.save(self.state_dict(), path)
def test_model(self, model_path=None, n_episodes=10):
if model_path:
self.load(model_path) # Load the trained model
self.eval() # Set the model to evaluation mode
# Initialize the environment
env = gym.make("CarRacing-v3", render_mode="human")
total_rewards = []
for episode in range(n_episodes):
print(f"Starting episode {episode + 1}/{n_episodes}")
# Reset the environment
observation, info = env.reset()
episode_reward = 0
negative_reward = 0
terminated = False
while not (terminated):
# Preprocess observation (convert to grayscale and normalize)
gray_observation = np.dot(observation[...,:3], [0.2126, 0.7152, 0.0722])
gray_observation = torch.tensor(gray_observation[np.newaxis, :, :], dtype=torch.float32)
# Predict the action
with torch.no_grad():
action = self.predict(gray_observation).reshape(3,)#.squeeze(0).numpy()
# Step in the environment
observation, reward, _, _, info = env.step(action)
# Accumulate rewards
episode_reward += reward
if reward < 0:
negative_reward = negative_reward + reward
if negative_reward < -20:
terminated = True
total_rewards.append(episode_reward)
print(f"Episode {episode + 1} finished with reward: {episode_reward}")
env.close()
# Print summary statistics
avg_reward = sum(total_rewards) / len(total_rewards)
print(f"Average Reward over {n_episodes} episodes: {avg_reward:.2f}")
print(f"Total Rewards: {total_rewards}")
return total_rewards