-
Notifications
You must be signed in to change notification settings - Fork 8
/
dqn_keras.py
203 lines (166 loc) · 9.06 KB
/
dqn_keras.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
from collections import deque
import os
import random
from agents.actions.base.abwrapper import ActionWrapper
from agents.states.abstate import StateBuilder
import numpy as np
from tensorflow.keras.layers import Activation, Conv2D, Dense, Dropout, Flatten, MaxPooling2D
from tensorflow.keras.models import Sequential
from tensorflow.keras.optimizers import Adam
from urnai.utils.error import IncoherentBuildModelError, UnsupportedBuildModelLayerTypeError
from .base.abmodel import LearningModel
from .model_builder import ModelBuilder
class DQNKeras(LearningModel):
def __init__(self, action_wrapper: ActionWrapper, state_builder: StateBuilder, gamma=0.99,
learning_rate=0.001, learning_rate_min=0.0001, learning_rate_decay=0.99995,
learning_rate_decay_ep_cutoff=0,
name='DQN', epsilon_start=1.0, epsilon_min=0.01, epsilon_decay=0.995,
batch_size=32, batch_training=False,
memory_maxlen=50000, use_memory=True, per_episode_epsilon_decay=False,
build_model=ModelBuilder.DEFAULT_BUILD_MODEL,
seed_value=None, cpu_only=False, epsilon_linear_decay=False,
lr_linear_decay=False):
super(DQNKeras, self).__init__(action_wrapper, state_builder, gamma, learning_rate,
learning_rate_min, learning_rate_decay,
epsilon_start, epsilon_min, epsilon_decay,
per_episode_epsilon_decay, learning_rate_decay_ep_cutoff,
name, seed_value, cpu_only, epsilon_linear_decay,
lr_linear_decay)
self.batch_size = batch_size
self.batch_training = batch_training
self.build_model = build_model
self.model = self.make_model()
self.use_memory = use_memory
if self.use_memory:
self.memory = deque(maxlen=memory_maxlen)
self.memory_maxlen = memory_maxlen
def make_model(self):
model = Sequential()
if self.build_model[0]['type'] == ModelBuilder.LAYER_INPUT \
and self.build_model[-1]['type'] == ModelBuilder.LAYER_OUTPUT:
self.build_model[0]['shape'] = [None, self.state_size]
self.build_model[-1]['length'] = self.action_size
for idx, (layer_model) in enumerate(self.build_model):
if layer_model['type'] == ModelBuilder.LAYER_INPUT:
if self.build_model.index(layer_model) == 0:
model.add(Dense(layer_model['nodes'], input_dim=layer_model['shape'][1],
activation='relu'))
else:
raise IncoherentBuildModelError('Input Layer must be the first one.')
elif layer_model['type'] == ModelBuilder.LAYER_FULLY_CONNECTED:
# if previous layer is convolutional, add a Flatten layer before the fully connected
if self.build_model[idx]['type'] == ModelBuilder.LAYER_CONVOLUTIONAL:
model.add(Flatten())
model.add(Dense(layer_model['nodes'], activation='relu'))
elif layer_model['type'] == ModelBuilder.LAYER_OUTPUT:
# if previous layer is convolutional, add a Flatten layer before the fully connected
if self.build_model[idx]['type'] == ModelBuilder.LAYER_CONVOLUTIONAL:
model.add(Flatten())
model.add(Dense(layer_model['length'], activation='linear'))
elif layer_model['type'] == ModelBuilder.LAYER_CONVOLUTIONAL:
# if convolutional layer is the first, it's going to have the input shape
# and be treated as the input layer
if self.build_model.index(layer_model) == 0:
model.add(Conv2D(layer_model['filters'], layer_model['filter_shape'],
padding=layer_model['padding'], activation='relu',
input_shape=layer_model['input_shape']))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=layer_model['max_pooling_pool_size_shape']))
try:
# this code makes dropout layer optional
model.add(Dropout(layer_model['dropout']))
except KeyError:
pass
else:
model.add(Conv2D(layer_model['filters'], layer_model['filter_shape'],
padding=layer_model['padding'], activation='relu'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=layer_model['max_pooling_pool_size_shape']))
try:
# this code makes dropout layer optional
model.add(Dropout(layer_model['dropout']))
except KeyError:
pass
else:
raise UnsupportedBuildModelLayerTypeError(
'Unsuported Layer Type ' + layer_model['type'])
model.compile(optimizer=Adam(lr=self.learning_rate), loss='mse', metrics=['accuracy'])
return model
def memorize(self, state, action, reward, next_state, done):
self.memory.append((state, action, reward, next_state, done))
def replay(self):
minibatch = random.sample(self.memory, self.batch_size)
if not hasattr(self, 'batch_training') or not self.batch_training:
for state, action, reward, next_state, done in minibatch:
target = reward
if not done:
target = (reward + self.gamma * np.amax(self.model(next_state).numpy()[0]))
target_f = self.model(state).numpy()
target_f[0][action] = target
self.model.fit(state, target_f, epochs=1, verbose=0,
callbacks=self.tensorboard_callback)
else:
inputs = np.zeros((len(minibatch), self.state_size))
targets = np.zeros((len(minibatch), self.action_size))
i = 0
for state, action, reward, next_state, done in minibatch:
q_current_state = self.model(state).numpy()[0]
q_next_state = self.model(next_state).numpy()[0]
inputs[i] = state
targets[i] = q_current_state
if done:
targets[i, np.argmax(action)] = reward
else:
targets[i, np.argmax(action)] = reward + self.gamma * np.max(q_next_state)
i += 1
# loss = self.model.train_on_batch(inputs, targets)
# Epsilon decay operation was here, moved it to 'decay_epsilon()' and to 'learn()'
def no_memory_learning(self, s, a, r, s_, done):
target = r
if not done:
target = (r + self.gamma * np.amax(self.model(s_).numpy()[0]))
target_f = self.model(s).numpy()
target_f[0][a] = target
self.model.fit(s, target_f, epochs=1, verbose=0, callbacks=self.tensorboard_callback)
def learn(self, s, a, r, s_, done):
if self.use_memory:
self.memorize(s, a, r, s_, done)
if (len(self.memory) > self.batch_size):
self.replay()
else:
self.no_memory_learning(s, a, r, s_, done)
if not self.per_episode_epsilon_decay:
self.decay_epsilon()
def choose_action(self, state, excluded_actions=[], is_testing=False):
# Verifies if we are running a test (Evaluating our agent)
if is_testing:
return self.predict(state, excluded_actions)
# If we are not testing (therefore we are training), evaluate epsilon greedy strategy
else:
if np.random.rand() <= self.epsilon_greedy:
random_action = random.choice(self.actions)
# Removing excluded actions
while random_action in excluded_actions:
random_action = random.choice(self.actions)
return random_action
else:
return self.predict(state, excluded_actions)
def predict(self, state, excluded_actions=[]):
"""
model.predict returns an array of arrays, containing the Q-Values for the actions.
This function should return the corresponding action with the highest Q-Value.
"""
q_values = self.model(state).numpy()[0]
action_idx = int(np.argmax(q_values))
while action_idx in excluded_actions:
q_values = np.delete(q_values, action_idx)
action_idx = int(np.argmax(q_values))
return action_idx
def save_extra(self, persist_path):
self.model.save_weights(self.get_full_persistance_path(persist_path) + '.h5')
def load_extra(self, persist_path):
exists = os.path.isfile(self.get_full_persistance_path(persist_path) + '.h5')
if exists:
self.model = self.make_model()
self.model.load_weights(self.get_full_persistance_path(persist_path) + '.h5')
self.set_seeds()