-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvanilla_policy_gradient.py
169 lines (123 loc) · 3.85 KB
/
vanilla_policy_gradient.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
# /usr/bin/env python 3.6
# -*-coding:utf-8-*-
'''
tensorflow in cart
Reference link:
https://github.com/gxnk/reinforcement-learning-code/blob/master/
Author: Jing Wang
'''
import numpy as np
import gym
import matplotlib.pyplot as plt
import math
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torch.distributions import Categorical
from torch.autograd import Variable
import model
class PolicyGradient(object):
def __init__(self, state_size, action_size, lr, gamma):
self.state_size = state_size
self.action_size = action_size
self.lr = lr
self.gamma = gamma
self.observations, self.actions, self.rewards = [],[],[]
self.action_probs = []
self.net = model.Actor(self.state_size, self.action_size)
self.optimizer = optim.Adam(self.net.parameters(), lr = self.lr)
def learn(self):
rewards = self.get_discounted_reward()
rewards = torch.Tensor(rewards)
self.optimizer.zero_grad()
loss = self.get_loss(rewards)
loss.backward()
self.optimizer.step()
self.observations, self.actions, self.rewards = [],[],[]
self.action_probs = []
return rewards.numpy()
def get_loss(self, rewards):
policy_losses = []
for log_prob, reward in zip(self.action_probs, rewards):
policy_losses.append(-log_prob * reward)
return torch.cat(policy_losses).mean() # torch cat need torch.Tensor
def select_action(self, state):
state = torch.from_numpy(state[np.newaxis, :]).to(torch.float32)
state = Variable(state)
prob = self.net(state)
prob = Categorical(prob) # for categorical actions
action = prob.sample()
self.action_probs.append(prob.log_prob(action))
return action.item()
def greedy(self, state):
state = torch.from_numpy(state[np.newaxis, :]).to(torch.float32)
state = Variable(state)
prob = self.net(state)
prob = prob.data.numpy()
action = np.argmax(prob.ravel())
return action
def get_discounted_reward(self):
# sum of discounted reward
discounted_reward = np.zeros_like(self.rewards)
mediate_sum = 0
for t in reversed(range(len(self.rewards))):
mediate_sum = mediate_sum * self.gamma + self.rewards[t]
discounted_reward[t] = mediate_sum
# normalize
discounted_reward -= np.mean(discounted_reward)
discounted_reward /= np.std(discounted_reward)
return discounted_reward
def store_transition(self, state, action, reward):
self.observations.append(state)
self.actions.append(action)
self.rewards.append(reward)
if __name__ == '__main__':
env = gym.make("CartPole-v0")
# env.wrapper()
max_episodes = 800
global state_size, action_size
state_size = int(np.product(env.observation_space.shape))
action_size = int(env.action_space.n)
lr = 0.001
gamma = 0.99
pg = PolicyGradient(state_size, action_size, lr, gamma)
reward_list = []
for t in range(max_episodes):
state = env.reset()
done = False
reward_sum = 0
while not done:
action = pg.select_action(state)
next_state, reward, done, _ = env.step(action)
pg.store_transition(state, action, reward)
state = next_state
reward_sum += reward
discounted_reward = pg.learn()
reward_list.append(reward_sum)
average_reward = np.mean(reward_list[-100:])
if t % 100 == 0:
print("Iteration: {}, last 100 average reward: {}, average reward: {}".format(t + 1, \
average_reward, np.mean(reward_list)))
if average_reward > env.spec.reward_threshold:
print("Solved!")
break
running_rewards = reward_list
rewards = np.array(running_rewards)
rewards_mean = np.mean(rewards)
rewards_std = np.std(rewards)
plt.plot(running_rewards)
plt.fill_between(
range(len(rewards)),
rewards-rewards_std,
rewards+rewards_std,
color='orange',
alpha=0.2
)
plt.title(
'Vanilla Policy Gradient Rewards Mean: {:.2f}, Standard Deviation: {:.2f}'.format(
np.mean(running_rewards),
np.std(running_rewards)
)
)
plt.show()