-
Notifications
You must be signed in to change notification settings - Fork 0
/
p53_A2C.py
125 lines (105 loc) · 4.57 KB
/
p53_A2C.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
import os
import gym
import torch
import numpy as np
import matplotlib.pyplot as plt
# Implementation of Advantage Actor Critic (A2C), no papers
# p52_A2C can converge but the value estimated through
# ActorCriticNet is V(st)-γV(st') rather than the state value
class ActorCriticNet(torch.nn.Module):
def __init__(self,s_dim,a_dim):
super(ActorCriticNet, self).__init__()
self.fcl = torch.nn.Linear(s_dim, 128)
self.actor = torch.nn.Linear(128, a_dim)
self.critic = torch.nn.Linear(128, 1)
def forward(self, x):
hidden = torch.relu(self.fcl(x))
action = torch.softmax(self.actor(hidden),dim=-1)
value = self.critic(hidden)
return action, value
class A2C(object):
def __init__(self, a_dim, s_dim, model_path, load_pretrained=True):
self.a_dim, self.s_dim = a_dim, s_dim
self.actor_critic = ActorCriticNet(s_dim,a_dim)
if load_pretrained and os.path.exists(model_path):
print('------------load the model----------------')
self.actor_critic.load_state_dict(torch.load(model_path))
self.optimizer = torch.optim.Adam(self.actor_critic.parameters(),lr=LR)
self.loss_func = torch.nn.SmoothL1Loss()
self.state_values, self.rewards, self.loglikelihood = [], [], []
def choose_action(self, s):
self.actor_critic.eval()
s = torch.unsqueeze(torch.FloatTensor(s), 0)
action, value = self.actor_critic(s)
actions = torch.distributions.Categorical(action[0])
action = actions.sample()
loglikelihood = actions.log_prob(action)
self.loglikelihood.append(loglikelihood)
self.state_values.append(value[0][0])
return action.data.item()
def store_transition(self, r):
self.rewards.append(r)
def train(self, final_state):
self.choose_action(final_state)
self.actor_critic.train()
reward = torch.FloatTensor(self._discount_and_norm_rewards()).detach()
self.loglikelihood = torch.stack(self.loglikelihood[:-1])
self.state_values = torch.stack(self.state_values)
self.state_values = self.state_values[:-1] - GAMMA*self.state_values[1:]
value_loss = self.loss_func(reward, self.state_values)
actor_loss = torch.mean(-self.loglikelihood * (reward-self.state_values))
self.optimizer.zero_grad()
loss = value_loss + actor_loss
loss.backward()
self.optimizer.step()
self.state_values, self.rewards, self.loglikelihood = [], [], []
return loss.data.item()
def _discount_and_norm_rewards(self):
discount = np.zeros_like(self.rewards)
tmp = 0
for i in reversed(range(len(self.rewards))):
tmp = tmp * GAMMA + self.rewards[i]
discount[i] = tmp
discount = discount - np.mean(discount)
discount = discount / (np.std(discount)+1e-8)
return discount
# hyperparameters
env_name = 'CartPole-v0' # you can try 'LunarLander-v2'
LR = 0.003 # learning rate
GAMMA = 0.9 # reward discount
env = gym.make(env_name).unwrapped # unwrapped gym simulation CartPole
env.seed(1) # reproducible, Policy gradient has high variance
N_ACTIONS = env.action_space.n # discrete action space with 2 actions
N_STATES = env.observation_space.shape[0] # continuous state
print(env.action_space.n,env.observation_space)
model_path = './checkpoint/a2c.pth'
a2c = A2C(N_ACTIONS, N_STATES, model_path)
losses, rewards = [], []
for i_episode in range(300):
s = env.reset()
while True:
env.render()
a = a2c.choose_action(s)
s_, r, done, _ = env.step(a)
a2c.store_transition(r)
s = s_
if done:
ep_rs_sum = sum(a2c.rewards)
if 'episode_reward_sum' not in globals():
episode_reward_sum = ep_rs_sum
else:
episode_reward_sum = episode_reward_sum * 0.99 + ep_rs_sum * 0.01
print("episode: %d, reward: %.2f"%(i_episode, episode_reward_sum))
rewards.append(episode_reward_sum)
losses.append(a2c.train(s))
break
torch.save(a2c.actor_critic.state_dict(),model_path)
plt.subplot(1,2,1)
plt.title('training loss')
plt.plot(losses)
plt.grid()
plt.subplot(1,2,2)
plt.title('long-term reward')
plt.plot(rewards)
plt.grid()
plt.show()