-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImitationLearningAgents.py
296 lines (223 loc) · 7.99 KB
/
ImitationLearningAgents.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/usr/bin/env python
import tensorflow as tf
import keras
from keras.models import Sequential
from keras.layers import *
from keras.optimizers import RMSprop, Adam
import numpy as np
import random
from collections import deque
import gym
import pickle
import os, sys, copy, argparse
import cv2
from keras import backend as K
from keras.layers import Lambda
from keras.models import Model
import pdb
class RLAgents():
"""
Different reinforcement learning for different mini-tasks
dynamically adding new agents
"""
def __init__(self, state_dim, action_cnt, env_name='MZ'):
"""
state_dim: dimension of states, tuple: (84, 84, 4)
action_cnt: dimension of actions, scala
env_name: environment name, for keeping track
"""
self.state_dim = state_dim
self.action_cnt = action_cnt
self.env_name = env_name
self.Agent_Dict = {}
def _get_agent(self, mini_task):
"""
mini_task: string for mini_task, each agent correspond to a mini_task
"""
if mini_task not in self.Agent_Dict:
self.Agent_Dict[mini_task] = Imitation_Agent(self.state_dim, self.action_cnt, self.env_name, mini_task,
lr=1e-3, train=True, save_every_step=1000, debug=False)
return self.Agent_Dict[mini_task]
def feedback(self, mini_task, feedbacks):
agent = self._get_agent(mini_task)
agent.step(feedbacks)
def execute(self, mini_task, states):
agent = self._get_agent(mini_task)
action = agent.predict(states)
return action
class Imitation_Agent():
"""
Policy network imitation learning agent.
"""
def __init__(self, state_dim, action_cnt, env_name, mini_task, lr=1e-3,
train=True, save_every_step=1000, debug=False):
"""
Initialize a imitation learning agent.
@param state_dim The dimension of states. (84, 84, 4)
@param action_cnt The number of actions. scala
@param env_name The name of the environment.
@param mini_task The mini task for the network.
@param train The switch to train the model. (default: True)
"""
self.state_dim = state_dim
self.action_cnt = action_cnt
# initialize the full actions array
self.all_actions = [i for i in range(self.action_cnt)]
self.env_name = env_name
self.mini_task = mini_task
self.debug = debug
self.should_train = train
# initialize the policy network and load weights if exist
self.batch_size = 32
self.learning_rate = lr
self.network = PolicyNetwork(self.state_dim, self.action_cnt,
self.env_name, self.mini_task,
self.batch_size, self.learning_rate)
self.step_cnt = 0
self.save_every_step = save_every_step
# load the saved model weights
if os.path.isfile(self.network.default_weights_path):
self.network.load_weights()
def predict(self, s):
"""
Predict the policy for the input state.
@param s The current state.
@return The action to take learned from expert.
"""
action_logits = self.network.predict(s)
action = np.argmax(action_logits[0]) # TODO
return action
def step(self, feedbacks):
"""
Step forward to the next state.
feedbacks: a tuple.
example:
(state_images, actions, next_states, rewards, done)
state_images and next_states should be of shape (1, 84, 84, 4)
action should be a scala
rewards should be a scala, the sum of rewards for the 4 frames
done should be a boolean
"""
state_images, actions, _, _, _ = feedbacks
action_idx = actions[0] # all actions the same
action = np.zeros((1, self.action_cnt))
action[0, action_idx] = 1
self.network.train(state_images, action)
# save network
self.step_cnt += 1
if self.should_train and self.step_cnt % self.save_every_step == 0 and self.step_cnt != 0:
# save the latest model weights
self.network.save_weights()
class PolicyNetwork():
def __init__(self, state_dim, action_cnt, env_name, suffix, batch_size=32, learning_rate=1e-4, debug=False):
"""
Initialize a policy network instance.
@param state_dim The dimension of state space. (84, 84, 4)
@param action_cnt The dimension of action space. scala
@param env_name The name of the environment.
@param suffix The test case suffix for the network.
@param batch_size The size of the mini-batch in mini-batch gradient descent
optimization for training the network. (default: 32)
@param learning_rate The learning rate for training the network.
(default: 1e-4)
"""
self.state_dim = state_dim
self.action_cnt = action_cnt
self.env_name = env_name
self.suffix = suffix
self.batch_size = batch_size
self.learning_rate = learning_rate
self.debug = debug
# construct default file paths
self.default_model_path = 'save_PN_' + self.env_name + '_' + self.suffix + '_model.h5'
self.default_weights_path = 'save_PN_' + self.env_name + '_' + self.suffix + '_weights.h5'
# build the keras model
self.model = self._build_model()
def _build_model(self):
"""
(Internal)
Build the keras model of the policy network.
@return A built keras model.
"""
model_input = Input(shape=self.state_dim)
x = Conv2D(filters=32, kernel_size=8, strides=4, padding="valid", activation="relu")(model_input)
x = Conv2D(filters=64, kernel_size=4, strides=2, padding="valid", activation="relu")(x)
x = Conv2D(filters=64, kernel_size=3, strides=1, padding="valid", activation="relu")(x)
x = Flatten()(x)
x = Dense(activation='relu', units=512)(x)
action_logits = Dense(activation='softmax', units=self.action_cnt)(x)
model = Model(input=model_input, output=action_logits)
optimizerRMSprop = RMSprop(lr=self.learning_rate)
optimizerAdam = Adam(lr=self.learning_rate, beta_1=0.9, beta_2=0.999)
optimizer = optimizerAdam # TODO
model.compile(loss='categorical_crossentropy', optimizer=optimizer)
return model
def train(self, X, Y, epochs=1, verbose=0):
self.model.fit(X, Y, epochs=epochs, verbose=verbose)
def predict(self, state):
prediction = self.model.predict(state)
return prediction
def save_model(self, file_path=None):
"""
Save the keras model as a file.
@param file_path The file path to save the keras model. (optional)
@return The path to the saved keras model file.
"""
if file_path is None:
file_path = self.default_model_path
if self.debug:
print("-----saving model to {}".format(file_path))
self.model.save(file_path)
return file_path
def save_weights(self, file_path=None):
"""
Save the keras model weights as a file.
@param file_path The file path to save the keras model weights. (optional)
@return The path to the saved keras weights file.
"""
if file_path is None:
file_path = self.default_weights_path
if self.debug:
print("-----saving weights to {}".format(file_path))
self.model.save_weights(file_path)
return file_path
def load_model(self, file_path=None):
"""
Load an existing keras model from a file.
@param file_path The path to an existing keras model file. (optional)
"""
if file_path is not None:
if self.debug:
print("-----loading model from {}".format(file_path))
self.model.load(file_path)
else:
if self.debug:
print("-----loading model from {}".format(self.default_model_path))
self.model.load(self.default_model_path)
def load_weights(self, file_path=None):
"""
Load existing keras model weights from a file.
@param file_path The path to an existing keras weights file. (optional)
"""
if file_path is not None:
if self.debug:
print("-----loading weights from {}".format(file_path))
self.model.load_weights(file_path)
else:
if self.debug:
print("-----loading weights from {}".format(self.default_weights_path))
self.model.load_weights(self.default_weights_path)
def main():
env_name = 'SpaceInvaders-v0'
env = gym.make(env_name)
state_dim = (84, 84, 4)
action_cnt = env.action_space.n
rlagents = RLAgents(state_dim, action_cnt, '_debug_')
mini_task = "say hi"
states = np.random.rand(1, 84, 84, 4)
feedbacks = (states, [5, 4, 3, 2], states, 100, True)
rlagents.feedback(mini_task, feedbacks)
action = rlagents.execute(mini_task, states)
print("------action: {}------".format(action))
if __name__ == '__main__':
main()