-
Notifications
You must be signed in to change notification settings - Fork 1
/
ddqn_random_batch.py
172 lines (151 loc) · 6.77 KB
/
ddqn_random_batch.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
# implemented using sum_tree
import os
import random
import gym
import numpy as np
from collections import deque
import tensorflow as tf
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
env = gym.make("MountainCar-v0")
env.reset()
model_save_path = "C:/Users/sanka/codes/mountain car openai/mc_save"
class dqn(object):
def __init__(self):
self.flag = 0
self.batch_size = 64
self.episodes = 20000
self.input_size = env.observation_space.sample().size
self.output_size = env.action_space.n
self.gamma = 0.99
self.epsilon = 1.0
self.step = 0
self.learning_rate = 0.0001
self.lambda1 = 0.001
self.initial_epsilon = self.epsilon
self.final_epsilon = 0.01
self.weights = {}
self.biases = {}
self.target_weights = {}
self.target_biases = {}
self.create_nn()
self.create_training_network()
self.max_size = 10000
self.cur_size=0
self.memory = deque()
self.sess = tf.InteractiveSession()
self.sess.run(tf.global_variables_initializer())
self.saver = tf.train.Saver()
def create_nn(self):
s1 = {1: [self.input_size, 30], 2: [30, 100], 3: [100, 30], 4: [30, self.output_size]}
s2 = {1: [30], 2: [100], 3: [30], 4: [self.output_size]}
for i in s1:
self.weights[i] = tf.Variable(tf.truncated_normal(s1[i]), name='w{0}'.format(i))
self.biases[i] = tf.Variable(tf.truncated_normal(s2[i]), name='b{0}'.format(i))
self.target_weights[i] = tf.Variable(tf.truncated_normal(s1[i]), name='tw{0}'.format(i))
self.target_biases[i] = tf.Variable(tf.truncated_normal(s2[i]), name='tb{0}'.format(i))
def feed_forward(self, z):
q = tf.nn.tanh(tf.matmul(z, self.weights[1]) + self.biases[1])
for i in range(2, len(self.weights), 1):
q = tf.nn.tanh(tf.matmul(q, self.weights[i]) + self.biases[i])
q = tf.matmul(q, self.weights[len(self.weights)]) + self.biases[len(self.biases)]
return q
def feed_forward_target(self, z):
q = tf.nn.tanh(tf.matmul(z, self.target_weights[1]) + self.target_biases[1])
for i in range(2, len(self.weights), 1):
q = tf.nn.tanh(tf.matmul(q, self.target_weights[i]) + self.target_biases[i])
q = tf.matmul(q, self.target_weights[len(self.weights)]) + self.target_biases[len(self.weights)]
return q
def create_training_network(self):
self.x = tf.placeholder(tf.float32, [None, self.input_size])
self.y = tf.placeholder(tf.float32, [None])
self.a = tf.placeholder(tf.float32, [None, self.output_size])
self.q_value = self.feed_forward(self.x)
self.q_value_target = self.feed_forward_target(self.x)
self.output = tf.reduce_sum(tf.multiply(self.q_value, self.a), reduction_indices=1)
self.action = tf.argmax(self.q_value, 1)
self.loss = tf.reduce_mean(tf.reduce_mean(tf.square(self.output - self.y)) + 0.01 * (
tf.reduce_mean(tf.nn.l2_loss(self.weights[1])) + tf.reduce_mean(
tf.nn.l2_loss(self.weights[2])) + tf.reduce_mean(tf.nn.l2_loss(self.weights[3])) + tf.reduce_mean(
tf.nn.l2_loss(self.weights[4]))))
self.optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate).minimize(self.loss)
def append_to_memory(self, state, action, reward, next_state, done):
one_hot_action = np.zeros(self.output_size)
one_hot_action[action] = 1.0
prob = (abs(reward) + .01) ** 0.6
if(self.cur_size==self.max_size):
self.memory.popleft()
else:
self.cur_size+=1
self.memory.append((state, one_hot_action, reward, next_state, done))
if self.cur_size >= self.max_size:
self.step += 1
# self.epsilon = self.final_epsilon + (self.initial_epsilon - self.final_epsilon) * np.exp(
# -self.lambda1 * (self.step / 200))
self.epsilon = max(self.initial_epsilon - (self.step / 400) * self.lambda1, self.final_epsilon)
if (self.flag == 0):
print("started training")
self.flag = 1
self.train()
def get_reward(self, q1, q2, reward, done):
if done:
return reward
else:
return reward + self.gamma * q2[np.argmax(q1)]
def train(self):
sample = random.sample(self.memory,self.batch_size)
train_x = [i[0] for i in sample]
action = [i[1] for i in sample]
reward = [i[2] for i in sample]
next_state = [i[3] for i in sample]
train_y = []
q = self.sess.run(self.q_value, feed_dict={self.x: np.array(train_x)})
q_1 = self.sess.run(self.q_value, feed_dict={self.x: np.array(next_state)})
q_next = self.sess.run(self.q_value_target, feed_dict={self.x: np.array(next_state)})
for i in range(len(reward)):
train_y.append(self.get_reward(q_1[i], q_next[i], reward[i], sample[i][4]))
train_y = np.array(train_y)
train_x = np.array(train_x)
action = np.array(action)
self.sess.run(self.optimizer, feed_dict={self.x: train_x, self.y: train_y, self.a: action})
def copy_variables(self):
for i in range(1, len(self.weights) + 1, 1):
self.sess.run(self.target_weights[i].assign(self.weights[i]))
self.sess.run(self.target_biases[i].assign(self.biases[i]))
def save(self):
self.saver.save(self.sess, model_save_path)
print("model saved")
def main():
obj = dqn()
for e in range(obj.episodes):
p = env.reset()
for i in range(500):
# obj.step += 1
ac = obj.sess.run(obj.action, feed_dict={obj.x: np.array([p])})[0]
if np.random.rand() < obj.epsilon:
ac = random.randint(0, obj.output_size - 1)
obs, rew, done, _ = env.step(ac)
obj.append_to_memory(p, ac, rew, obs, done)
p = obs
if done:
break
if obj.step % 1000 == 0 and obj.flag == 1:
obj.copy_variables()
# print("episode {0} completed with loss: {1}".format(e, total_loss))
if e % 100 == 0:
print("episodes {0} completed".format(e), )
av = []
for f in range(10):
p = env.reset()
r = 0
for i in range(200):
ac = obj.sess.run(obj.action, feed_dict={obj.x: np.array([p])})[0]
p, rew, done, _ = env.step(ac)
r += rew
if done:
break
av.append(r)
print("average score is {0}".format(np.average(np.array(av))))
print("epsilon value is {0}".format(obj.epsilon))
obj.save()
if __name__ == '__main__':
main()