-
Notifications
You must be signed in to change notification settings - Fork 0
/
actor.py
167 lines (137 loc) · 6.73 KB
/
actor.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
import tensorflow as tf
import numpy as np
from parameter import *
def maxPoolLayer(x, kHeight, kWidth, strideX, strideY, name, padding = "SAME"):
return tf.nn.max_pool(x, ksize = [1, kHeight, kWidth, 1], strides = [1, strideX, strideY, 1], padding = padding, name = name)
def dropout(x, keepPro, name = None):
return tf.nn.dropout(x, keepPro, name)
def fcLayer(x, inputD, outputD, reluFlag, name):
with tf.variable_scope(name) as scope:
w = tf.get_variable("w", shape = [inputD, outputD], dtype = "float")
b = tf.get_variable("b", [outputD], dtype = "float")
out = tf.nn.xw_plus_b(x, w, b, name = scope.name)
if reluFlag:
return tf.nn.relu(out)
else:
return out
def convLayer(x, kHeight, kWidth, strideX, strideY,
featureNum, name, padding = "SAME"):
channel = int(x.get_shape()[-1])
with tf.variable_scope(name) as scope:
w = tf.get_variable("w", shape = [kHeight, kWidth, channel, featureNum])
b = tf.get_variable("b", shape = [featureNum])
featureMap = tf.nn.conv2d(x, w, strides = [1, strideY, strideX, 1], padding = padding)
out = tf.nn.bias_add(featureMap, b)
#return tf.nn.relu(tf.reshape(out, featureMap.get_shape().as_list()), name = scope.name)
return tf.nn.relu(out, name = scope.name)
class Actor(object):
def __init__(self, n_actions, lr):
self.lr = lr
self.graph = tf.Graph()
with self.graph.as_default():
#self.s = tf.placeholder(tf.float32, [None, STATE_SIZE_X, STATE_SIZE_Y, 6], "state")
#self.s = tf.placeholder(tf.float32, [None, 7, 7, 1024], "state")
self.s = tf.placeholder(tf.float32, [None, 7, 7, 1536], "state")
self.a = tf.placeholder(tf.int32, [None, 1], "act")
self.td_error = tf.placeholder(tf.float32, [None, 1], "td_error") # TD_error
#self.conv1_1 = convLayer(self.s, 3, 3, 1, 1, 64, "conv1_1")
#self.conv1_2 = convLayer(self.conv1_1, 3, 3, 1, 1, 64, "conv1_2")
#self.pool1 = maxPoolLayer(self.conv1_2, 2, 2, 2, 2, "pool1")
#self.fcIn = tf.reshape(self.pool1, [-1, STATE_SIZE_X * STATE_SIZE_Y * 16])
#self.fcIn = tf.reshape(self.s, [-1, 7 * 7 * 1024])
self.fcIn = tf.reshape(self.s, [-1, 7 * 7 * 1536])
self.fc1 = tf.layers.dense(
inputs=self.fcIn,
units=512,
activation=tf.nn.relu, # None
kernel_initializer=tf.random_normal_initializer(0., 0.01), # weights
bias_initializer=tf.constant_initializer(0.1), # biases
name='fc1'
)
self.fc2 = tf.layers.dense(
inputs=self.fc1,
units=512, # number of hidden units
activation=tf.nn.relu, # None
kernel_initializer=tf.random_normal_initializer(0., 0.01), # weights
bias_initializer=tf.constant_initializer(0.1), # biases
name='fc2'
)
self.acts_prob = tf.layers.dense(
inputs=self.fc2,
units=n_actions, # output units
activation=tf.nn.softmax, # get action probabilities
kernel_initializer=tf.random_normal_initializer(0., .1), # weights
bias_initializer=tf.constant_initializer(0.1), # biases
name='acts_prob'
)
#log_prob = tf.log(self.acts_prob[0, self.a])
prob_ = self.acts_prob[0]
a_ = self.a[0][0]
prob_a = prob_[a_]
prob_final = tf.reshape(prob_a, [1, 1])
for i in range(1, RECORD_NUM):
prob_ = self.acts_prob[i]
a_ = self.a[i][0]
prob_a = prob_[a_]
prob_a_ = tf.reshape(prob_a, [1, 1])
prob_final = tf.concat([prob_final, prob_a_], 0)
log_prob = tf.log(prob_final)
self.exp_v = tf.reduce_mean(log_prob * self.td_error) # advantage (TD_error) guided loss
# exp_v: (1) -> log probability * reward(using TD error here)
self.train_op = tf.train.AdamOptimizer(self.lr).minimize(-self.exp_v) # minimize(-exp_v) = maximize(exp_v)
self.sess = tf.Session(graph=self.graph)
self.saver = tf.train.Saver()
def initialze(self):
with self.sess.as_default():
with self.graph.as_default():
self.sess.run(tf.global_variables_initializer())
def learn(self, s, a, td):
with self.sess.as_default():
with self.graph.as_default():
#s = s[np.newaxis, :] # (4,) => (1,4)
feed_dict = {self.s: s, self.a: a, self.td_error: td}
_, exp_v = self.sess.run([self.train_op, self.exp_v], feed_dict)
return exp_v
def get_value(self, node, s):
with self.sess.as_default():
with self.graph.as_default():
value = self.sess.run(node, {self.s:s})
return value
def get_probs(self, s):
with self.sess.as_default():
with self.graph.as_default():
#s = s[np.newaxis, :] # (4,) => (1,4)
# acts_prob: (?, 2)
probs = self.sess.run(self.acts_prob, {self.s: s})
return probs
def choose_action(self, s):
with self.sess.as_default():
with self.graph.as_default():
#s = s[np.newaxis, :] # (4,) => (1,4)
# acts_prob: (?, 2)
probs = self.sess.run(self.acts_prob, {self.s: s}) # get probabilities for all actions
# probs: (?, 2)
# probs.shape => tuple, 0:1, 1:2 (0 represents row and 1 represents column)
probs_shape = probs.shape[1]
# probs_shape => 2 (number of columns)
probs_arrange = np.arange(probs_shape)
# probs_arrange => [0,1]
probs_ = probs.ravel()
# probs: (?, 2) -> (1, 2)
# probs_: (2,)
p = np.random.choice(probs_arrange, p=probs_) # get probabilities for all actions
return p # return a int
def save(self):
with self.sess.as_default():
with self.graph.as_default():
self.saver.save(self.sess, "./actor_models/actor_model.ckpt")
def restore(self):
with self.sess.as_default():
with self.graph.as_default():
self.saver.restore(self.sess, "./actor_models/actor_model.ckpt")
def save_learning_rate(self):
f = open("./actor_models/actor_lr", "w")
f.write(str(self.lr))
def restore_learning_rate(self):
f = open("./actor_models/actor_lr", "r")
self.lr = float(f.read())