-
Notifications
You must be signed in to change notification settings - Fork 0
/
actor.py
50 lines (39 loc) · 1.77 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
from keras import layers, models
from keras import backend as K
from keras import optimizers
class Actor:
"""Actor (Policy) Model."""
def __init__(self, state_size, action_size, action_high, action_low):
"""Initialize Actor instance."""
self.state_size = state_size
self.action_size = action_size
self.action_high = action_high
self.action_low = action_low
self.action_range = self.action_high - self.action_low
self.build_model()
def build_model(self):
# input layer (states)
states = layers.Input(shape=(self.state_size,), name='states')
# hidden layers
net = layers.Dense(units=32, activation='relu')(states)
net = layers.Dense(units=64, activation='relu')(net)
net = layers.Dense(units=32, activation='relu')(net)
# final layer with sigmoid
raw_actions = layers.Dense(units=self.action_size, activation='sigmoid',
name='raw_actions')(net)
# Scale output for each action dimension to proper range
actions = layers.Lambda(lambda x: (x * self.action_range) + self.action_low,
name='actions')(raw_actions)
# Keras model
self.model = models.Model(inputs=states, outputs=actions)
# loss function
action_gradients = layers.Input(shape=(self.action_size,))
loss = K.mean(-action_gradients * actions)
# optimizer and training function
optimizer = optimizers.Adam()
updates_op = optimizer.get_updates(
params=self.model.trainable_weights, loss=loss)
self.train_fn = K.function(
inputs=[self.model.input, action_gradients, K.learning_phase()],
outputs=[],
updates=updates_op)