-
Notifications
You must be signed in to change notification settings - Fork 10
/
cartpole_networks.py
260 lines (220 loc) · 7.2 KB
/
cartpole_networks.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import numpy as np
import tensorflow as tf
import neural_network as nn
class Generator(nn.Generator):
"""
Example OpenAI-Gym Generator architecture.
"""
def __init__(self, sess):
"""
Args
----
sess : the tensorflow session to be used
"""
self.sess_ = sess
with tf.variable_scope('gen'):
self.input_state_ = tf.placeholder(tf.float32, shape=[None, 4], name='input_state')
self.input_seed_ = tf.placeholder(tf.float32, shape=[None, 1], name='input_seed')
self.concat = tf.concat([self.input_state_, self.input_seed_], 1, name='concat')
self.hidden = tf.layers.dense(self.concat, 8, activation=tf.nn.relu, name='hidden')
self.output_ = tf.layers.dense(self.hidden, 2, name='output')
self.sess.run(tf.global_variables_initializer())
@property
def input_state(self):
"""
The input state of shape [None, 4]
Returns
-------
A placeholder tensor: the input state's placeholder tensor
"""
return self.input_state_
@property
def output(self):
"""
The outputted action distribution of shape [None, 2]
Returns
-------
A tensor: the output tensor
"""
return self.output_
@property
def sess(self):
"""
The session used to create the graph
Returns
-------
A session: the graph's session
"""
return self.sess_
@property
def input_seed(self):
"""
The input random seed
Returns
-------
A placeholder: the input seed's placeholder tensor
"""
return self.input_seed_
@property
def trainable_variables(self):
"""
A list of the trainable variables in our generator
Returns
-------
A list of tensors: the trainable variables in this graph
"""
return tf.trainable_variables('gen')
class Discriminator(nn.Discriminator):
"""
Example OpenAI-Gym Discriminator Architecture
"""
def __init__(self, sess):
"""
Args
----
sess : the tensorflow session to be used
"""
self.sess_ = sess
with tf.variable_scope('dis'):
self.input_state_ = tf.placeholder(tf.float32, shape=[None, 4], name='input_state')
self.input_reward_ = tf.placeholder(tf.float32, shape=[None], name='input_reward')
self.input_action_ = tf.placeholder(tf.float32, shape=[None, 1], name='input_action')
self.input_reward_exp = tf.expand_dims(self.input_reward_, axis=-1, name='input_reward_expanded')
self.concat = tf.concat([self.input_state_, self.input_reward_exp, self.input_action_], axis=1, name='concat')
self.hidden = tf.layers.dense(self.concat, 8, activation=tf.nn.relu, name='hidden')
self.output_ = tf.layers.dense(self.hidden, 1, activation=tf.sigmoid, name='output')
self.sess.run(tf.global_variables_initializer())
@property
def input_state(self):
"""
The input state of shape [None, 4]
Returns
-------
A placeholder tensor: the input state's placeholder tensor
"""
return self.input_state_
@property
def input_action(self):
"""
The input action of shape [None, 1]
Returns
-------
A placeholder tensor: the input action's placeholder tensor
"""
return self.input_action_
@property
def output(self):
"""
The probability output of shape [None, 1]
Returns
-------
A tensor: the output's tensor
"""
return self.output_
@property
def sess(self):
"""
The session used to create a graph
Returns
-------
A session: the graph's session
"""
return self.sess_
@property
def input_reward(self):
"""
The input reward
Returns
-------
A placeholder tensor: the input reward's tensor
"""
return self.input_reward_
@property
def trainable_variables(self):
"""
A list of the trainable variables in our generator
Returns
-------
A list of tensors: the trainable variables in this graph
"""
return tf.trainable_variables('dis')
class Discriminator_copy(nn.Discriminator_copy):
"""
Example OpenAI-Gym Discriminator Copying method
"""
def __init__(self, dis, new_rew_input):
"""
Initializes a discriminator_copy object
Args
----
dis (Discriminator) : The discriminator to copy
new_rew_input (tf.placeholder) : a new reward input.
"""
self.sess_ = dis.sess
#reuse the variables
with tf.variable_scope('dis', reuse=tf.AUTO_REUSE):
self.input_state_ = tf.placeholder(tf.float32, shape=[None, 4], name='input_state')
self.input_reward_ = new_rew_input
self.input_action_ = tf.placeholder(tf.float32, shape=[None, 1], name='input_action')
self.input_reward_exp = tf.expand_dims(self.input_reward_, axis=-1, name='input_reward_expanded')
self.concat = tf.concat([self.input_state_, self.input_reward_exp, self.input_action_], axis=1, name='concat_copy')
self.hidden_ker = tf.get_variable('hidden/kernel')
self.hidden_bias = tf.get_variable('hidden/bias')
self.output_ker = tf.get_variable('output/kernel')
self.output_bias = tf.get_variable('output/bias')
self.hidden = tf.matmul(self.concat, self.hidden_ker) + self.hidden_bias
self.output_ = tf.sigmoid(tf.matmul(self.hidden, self.output_ker) + self.output_bias)
@property
def input_state(self):
"""
The input state of shape [None, 4]
Returns
-------
A placeholder tensor: the input state's placeholder tensor
"""
return self.input_state_
@property
def input_action(self):
"""
The input action of shape [None, 1]
Returns
-------
A placeholder tensor: the input action's placeholder tensor
"""
return self.input_action_
@property
def output(self):
"""
The probability output of shape [None, 1]
Returns
-------
A tensor: the output's tensor
"""
return self.output_
@property
def sess(self):
"""
The session used to create a graph
Returns
-------
A session: the graph's session
"""
return self.sess_
@property
def input_reward(self):
"""
The input reward
Returns
-------
A placeholder tensor: the input reward's tensor
"""
return self.input_reward_
@property
def trainable_variables(self):
"""
A list of the trainable variables in our generator
Returns
-------
A list of tensors: the trainable variables in this graph
"""
return tf.trainable_variables('dis')