-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPPO.py
More file actions
176 lines (146 loc) · 6.14 KB
/
PPO.py
File metadata and controls
176 lines (146 loc) · 6.14 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
import argparse
import pickle
from collections import namedtuple
from itertools import count
import os, time
import numpy as np
import matplotlib.pyplot as plt
import gym
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.distributions import Normal, Categorical
from torch.utils.data.sampler import BatchSampler, SubsetRandomSampler
from tensorboardX import SummaryWriter
# Parameters
env_name = 'MountainCar-v0'
gamma = 0.99
render = True
seed = 1
log_interval = 10
env = gym.make(env_name).unwrapped
num_state = env.observation_space.shape[0]
num_action = env.action_space.n
torch.manual_seed(seed)
env.seed(seed)
Transition = namedtuple('Transition', ['state', 'action', 'a_log_prob', 'reward', 'next_state'])
class Actor(nn.Module):
def __init__(self):
super(Actor, self).__init__()
self.fc1 = nn.Linear(num_state, 128)
self.action_head = nn.Linear(128, num_action)
def forward(self, x):
x = F.relu(self.fc1(x))
action_prob = F.softmax(self.action_head(x), dim=1)
return action_prob
class Critic(nn.Module):
def __init__(self):
super(Critic, self).__init__()
self.fc1 = nn.Linear(num_state, 128)
self.state_value = nn.Linear(128, 1)
def forward(self, x):
x = F.relu(self.fc1(x))
value = self.state_value(x)
return value
class PPO():
clip_param = 0.2
max_grad_norm = 0.5
ppo_update_time = 10
buffer_capacity = 8000
batch_size = 32
def __init__(self):
super(PPO, self).__init__()
self.actor_net = Actor()
self.critic_net = Critic()
self.buffer = []
self.counter = 0
self.training_step = 0
self.writer = SummaryWriter('../exp')
self.actor_optimizer = optim.Adam(self.actor_net.parameters(), 1e-3)
self.critic_net_optimizer = optim.Adam(self.critic_net.parameters(), 3e-3)
if not os.path.exists('../param'):
os.makedirs('../param/net_param')
os.makedirs('../param/img')
def select_action(self, state):
state = torch.from_numpy(state).float().unsqueeze(0)
with torch.no_grad():
action_prob = self.actor_net(state)
c = Categorical(action_prob)
action = c.sample()
return action.item(), action_prob[:, action.item()].item()
def get_value(self, state):
state = torch.from_numpy(state)
with torch.no_grad():
value = self.critic_net(state)
return value.item()
def save_param(self):
torch.save(self.actor_net.state_dict(), '../param/net_param/actor_net' + str(time.time())[:10], +'.pkl')
torch.save(self.critic_net.state_dict(), '../param/net_param/critic_net' + str(time.time())[:10], +'.pkl')
def store_transition(self, transition):
self.buffer.append(transition)
self.counter += 1
def update(self, i_ep):
state = torch.tensor([t.state for t in self.buffer], dtype=torch.float)
action = torch.tensor([t.action for t in self.buffer], dtype=torch.long).view(-1, 1)
reward = [t.reward for t in self.buffer]
# update: don't need next_state
# reward = torch.tensor([t.reward for t in self.buffer], dtype=torch.float).view(-1, 1)
# next_state = torch.tensor([t.next_state for t in self.buffer], dtype=torch.float)
old_action_log_prob = torch.tensor([t.a_log_prob for t in self.buffer], dtype=torch.float).view(-1, 1)
R = 0
Gt = []
for r in reward[::-1]:
R = r + gamma * R
Gt.insert(0, R)
Gt = torch.tensor(Gt, dtype=torch.float)
# print("The agent is updateing....")
for i in range(self.ppo_update_time):
for index in BatchSampler(SubsetRandomSampler(range(len(self.buffer))), self.batch_size, False):
if self.training_step % 1000 == 0:
print('I_ep {} ,train {} times'.format(i_ep, self.training_step))
# with torch.no_grad():
Gt_index = Gt[index].view(-1, 1)
V = self.critic_net(state[index])
delta = Gt_index - V
advantage = delta.detach()
# epoch iteration, PPO core!!!
action_prob = self.actor_net(state[index]).gather(1, action[index]) # new policy
ratio = (action_prob / old_action_log_prob[index])
surr1 = ratio * advantage
surr2 = torch.clamp(ratio, 1 - self.clip_param, 1 + self.clip_param) * advantage
# update actor network
action_loss = -torch.min(surr1, surr2).mean() # MAX->MIN desent
self.writer.add_scalar('loss/action_loss', action_loss, global_step=self.training_step)
self.actor_optimizer.zero_grad()
action_loss.backward()
nn.utils.clip_grad_norm_(self.actor_net.parameters(), self.max_grad_norm)
self.actor_optimizer.step()
# update critic network
value_loss = F.mse_loss(Gt_index, V)
self.writer.add_scalar('loss/value_loss', value_loss, global_step=self.training_step)
self.critic_net_optimizer.zero_grad()
value_loss.backward()
nn.utils.clip_grad_norm_(self.critic_net.parameters(), self.max_grad_norm)
self.critic_net_optimizer.step()
self.training_step += 1
del self.buffer[:] # clear experience
def main():
agent = PPO()
for i_epoch in range(1000):
state = env.reset()
if render: env.render()
for t in count():
action, action_prob = agent.select_action(state)
next_state, reward, done, _ = env.step(action)
trans = Transition(state, action, action_prob, reward, next_state)
if render: env.render()
agent.store_transition(trans)
state = next_state
if done:
if len(agent.buffer) >= agent.batch_size: agent.update(i_epoch)
agent.writer.add_scalar('Steptime/steptime', t, global_step=i_epoch)
break
if __name__ == '__main__':
main()
print("end")