-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrun_a3c.py
174 lines (164 loc) · 7.27 KB
/
run_a3c.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
import os
import numpy as np
import argparse
import gym
from gym import wrappers
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.autograd as autograd
from torch.autograd import Variable
import torch.multiprocessing as mp
from async_rmsprop import AsyncRMSprop
from policy import Policy
from wrapper_env import StackEnv, AtariEnv
import logger
def train(rank, global_policy, local_policy, optimizer, env, global_t, args):
o = env.reset()
step = 0
sum_rewards = 0
max_sum_rewards = 0
vs = []
entropies = []
sum_rewards = 0
while global_t[0] < args.epoch:
local_policy.sync(global_policy)
observations = []
actions = []
values = []
rewards = []
probs = []
R = 0
for i in range(args.local_t_max):
global_t += 1
step += 1
p, v = local_policy(Variable(torch.from_numpy(o).float()).unsqueeze(0))
a = p.multinomial()
o, r, done, _ = env.step(a.data.squeeze()[0])
if rank == 0:
sum_rewards += r
if args.render:
env.render()
observations.append(o)
actions.append(a)
values.append(v)
rewards.append(r)
probs.append(p)
if done:
o = env.reset()
if rank == 0:
print('----------------------------------')
print('total reward of the episode:', sum_rewards)
print('----------------------------------')
if args.save_mode == 'all':
torch.save(local_policy, os.path.join(args.log_dir, args.save_name+"_{}.pkl".format(global_t[0])))
elif args.save_mode == 'last':
torch.save(local_policy, os.path.join(args.log_dir, args.save_name+'.pkl'))
elif args.save_mode == 'max':
if max_sum_rewards < sum_rewards:
torch.save(local_policy, os.path.join(args.log_dir, args.save_name+'.pkl'))
max_sum_rewards = sum_rewards
step = 0
break
else:
_, v = local_policy(Variable(torch.from_numpy(o).unsqueeze(0).float()))
R += v.data.squeeze()[0]
returns = []
for r in rewards[::-1]:
R = r + 0.99 * R
returns.insert(0, R)
returns = torch.Tensor(returns)
#if len(returns) > 1:
# returns = (returns-returns.mean()) / (returns.std()+args.eps)
v_loss = 0
entropy = 0
for a, v, p, r in zip(actions, values, probs, returns):
a.reinforce(r - v.data.squeeze())
_v_loss = nn.MSELoss()(v, Variable(torch.Tensor([r])))
v_loss += _v_loss
entropy += -(p * (p + args.eps).log()).sum()
v_loss = v_loss * 0.5 * args.v_loss_coeff
entropy = entropy * args.entropy_beta
loss = v_loss - entropy
vs.append(v_loss.data.numpy())
entropies.append(entropy.data.numpy())
if rank == 0 and done:
logger.record_tabular_misc_stat('Entropy', entropies)
logger.record_tabular_misc_stat('V', vs)
logger.record_tabular('reward', sum_rewards)
logger.record_tabular('step', global_t[0])
logger.dump_tabular()
del vs[:]
del entropies[:]
sum_rewards = 0
optimizer.zero_grad()
final_node = [loss] + actions
gradients = [torch.ones(1)] + [None] * len(actions)
autograd.backward(final_node, gradients)
new_lr = (args.epoch - global_t[0]) / args.epoch * args.lr
optimizer.step(new_lr)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='PyTorch a3c')
parser.add_argument('--gamma', type=float, default=0.99, metavar='G',
help='discount factor (default: 0.99)')
parser.add_argument('--seed', type=int, default=543, metavar='N',
help='random seed (default: 543)')
parser.add_argument('--render', action='store_true',
help='render the environment')
parser.add_argument('--monitor', action='store_true',
help='save the rendered video')
parser.add_argument('--log_dir', type=str, default='./log_dir',
help='save dir')
parser.add_argument('--epoch', type=int, default=10000000, metavar='N',
help='training epoch number')
parser.add_argument('--local_t_max', type=int, default=5, metavar='N',
help='bias variance control parameter')
parser.add_argument('--entropy_beta', type=float, default=0.01, metavar='E',
help='coefficient of entropy')
parser.add_argument('--v_loss_coeff', type=float, default=0.5, metavar='V',
help='coefficient of value loss')
parser.add_argument('--frame_num', type=int, default=4, metavar='N',
help='number of frames you use as observation')
parser.add_argument('--lr', type=float, default=7e-4, metavar='L',
help='learning rate')
parser.add_argument('--env', type=str, default='Breakout-v0',
help='Environment')
parser.add_argument('--atari', action='store_true',
help='atari environment')
parser.add_argument('--num_process', type=int, default=8, metavar='n',
help='number of processes')
parser.add_argument('--eps', type=float, default=0.01, metavar='E',
help='epsilon minimum log or std')
parser.add_argument('--save_name', type=str, default='exp', metavar='N',
help='define save name')
parser.add_argument('--save_mode', type=str, default='max', metavar='S',
help='save mode. all or last or max')
args = parser.parse_args()
logger.add_tabular_output(os.path.join(args.log_dir, 'progress.csv'))
assert not (args.env == 'Breakout-v0' and not args.atari), 'You should use --atari option'
logger.log_parameters_lite(os.path.join(args.log_dir, 'params.json'), args)
if not os.path.exists(args.log_dir):
os.mkdir(args.log_dir)
env = gym.make(args.env)
if args.monitor:
env = wrappers.Monitor(env, args.log_dir, force=True)
if args.atari:
env = AtariEnv(env)
env = StackEnv(env, args.frame_num)
env.seed(args.seed)
torch.manual_seed(args.seed)
global_policy = Policy(env.action_space.n, atari=args.atari,
dim_obs=env.observation_space.shape[0], out_dim=512, frame_num=args.frame_num)
global_policy.share_memory()
local_policy = Policy(env.action_space.n, atari=args.atari,
dim_obs=env.observation_space.shape[0], out_dim=512, frame_num=args.frame_num)
optimizer = AsyncRMSprop(global_policy.parameters(), local_policy.parameters(), lr=args.lr, eps=args.eps)
global_t = torch.LongTensor(1).share_memory_()
global_t.zero_()
processes = []
for rank in range(args.num_process):
p = mp.Process(target=train, args=(rank, global_policy, local_policy, optimizer, env, global_t, args))
p.start()
processes.append(p)
for p in processes:
p.join()