-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain_VDQN.py
172 lines (133 loc) · 4.76 KB
/
main_VDQN.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
"""
Implement Variational DQN in Edward and Tensorflow
"""
import tensorflow as tf
import edward as ed
from edward.models import Normal
import numpy as np
import gym
import copy
import os
import argparse
from models_VDQN import VariationalQNetwork,update_target,NoiseSampler
from utils import ReplayBuffer
import HardMDP
def main():
parser = argparse.ArgumentParser(description='VDQN')
parser.add_argument('--seed',type=int,default=100)
parser.add_argument('--env', type=str, default='CartPole-v0',
help='Name of the OpenAI Gym environment')
parser.add_argument('--logdir',type=str,default='')
parser.add_argument('--episodes',type=int,default=200)
parser.add_argument('--target-update-period',type=int,default=100)
parser.add_argument('--lr',type=float,default=1e-3)
parser.add_argument('--gamma',type=float,default=.99)
args = parser.parse_args()
#### HYPERPARAMETERS
episodes = args.episodes #1000
envname = args.env
seed = args.seed
tf.set_random_seed(seed)
np.random.seed(seed)
hiddendict = [100,100]
sigma = 0.01
Wpriorsigma = [10000] * 2
bpriorsigma = [10000] * 2
batchsize = 64
buffersize = 1000000
initialsize = 500
tau = 1.0
target_update_period = args.target_update_period
lr_VI = args.lr
gamma = args.gamma
totalstep = 0
reward_scale = 1
############
#### MAIN ITERATIONS
###########
logdir = args.logdir + 'VDQN/' + envname + '/lr_' + str(args.lr) + '_episodes' + str(args.episodes)
if not os.path.exists(logdir):
os.makedirs(logdir)
with tf.Session() as sess:
### INITIALIZATION
env = gym.make(envname)
obssize = env.observation_space.low.size
actsize = env.action_space.n
replaybuffer = ReplayBuffer(buffersize)
Qactionnet = VariationalQNetwork(obssize,actsize,hiddendict,sess=sess,scope='principle',optimizer=tf.train.AdamOptimizer(lr_VI))
Qtargetnet = VariationalQNetwork(obssize,actsize,hiddendict,sess=sess,scope='target')
noisesampler = NoiseSampler(Qactionnet.Wshape,Qactionnet.bshape)
sess.run(tf.global_variables_initializer())
update_target(Qtargetnet,Qactionnet)
### RECORD
VIlossrecord = []
Bellmanlossrecord = []
rewardrecord = []
### ITERATIONS
for episode in range(episodes):
# start
obs = env.reset()
done = False
rsum = 0
while not done:
# sample a noise and compute
Wnoise,bnoise = noisesampler.sample(1)
# compuet Q value
Qvalue = Qactionnet.compute_Qvalue(obs[None],Wnoise,bnoise)
# select action
action = np.argmax(Qvalue.flatten())
# step
nextobs,reward,done,_ = env.step(action)
# record experience
done_ = 1 if done else 0
reward_ = reward * reward_scale
experience = [(obs,action,reward_,done_,nextobs)]
# append experience to buffer
replaybuffer.append(experience)
replaybuffer.popleft()
# update
obs = nextobs
totalstep += 1
rsum += reward
if replaybuffer.currentsize >= initialsize:
# sample minibatch
batch_obs,batch_act,batch_reward,batch_done,batch_nextobs = replaybuffer.sample(batchsize)
# sample noise for computing target
Wnoise,bnoise = noisesampler.sample(batchsize)
# compute target value
Qall = Qtargetnet.compute_Qvalue(batch_nextobs,Wnoise,bnoise)
Qtarget = gamma * np.max(Qall,axis=1) * (1-batch_done) + batch_reward
# udpate principle network by VI
VIloss = Qactionnet.train_on_sample(batch_obs,batch_act,Qtarget)
# comptue bellman error loss
#Wnoise_new,bnoise_new = noisesampler.sample(batchsize)
Wnoise_new,bnoise_new = Wnoise,bnoise
Qpred = Qactionnet.compute_Qvalue(batch_obs,Wnoise_new,bnoise_new)
Qpredact = Qpred[np.arange(batchsize),batch_act]
Bellmanloss = np.mean((Qpredact - Qtarget)**2)
#a,b,c,d = Qactionnet.get_variables()
#print('Wmu',a,'Wrho',b,'bmu',c,'brho',d)
#print(Qpredact,Qtarget)
# record
#print('bellmanerror',Bellmanloss)
#raise ValueError
VIlossrecord.append(VIloss['loss'])
Bellmanlossrecord.append(Bellmanloss)
if (totalstep+1) % target_update_period == 0:
update_target(Qtargetnet,Qactionnet)
print("update target")
if done:
break
# record
rewardrecord.append(rsum)
### TRAIN
meanVIloss = np.mean(VIlossrecord[-10:]) if len(VIlossrecord)>10 else np.float('nan')
meanbellmanloss = np.mean(Bellmanlossrecord[-10:]) if len(Bellmanlossrecord)>10 else np.float('nan')
meanreward = np.mean(rewardrecord[-10:])
print("episode %d buffer size %d meanVIloss %f meanbellmanloss %f meanreward %f" %(episode,replaybuffer.currentsize,meanVIloss,meanbellmanloss,meanreward))
if (1+episode) % 5 == 0:
np.save(logdir+'/VIloss_'+str(seed),VIlossrecord)
np.save(logdir+'/bellmanloss_'+str(seed),Bellmanlossrecord)
np.save(logdir+'/reward_'+str(seed),rewardrecord)
if __name__ == '__main__':
main()