-
Notifications
You must be signed in to change notification settings - Fork 0
/
actor.py
52 lines (40 loc) · 1.92 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
import tensorflow.keras.layers as kl
from tensorflow.keras.layers import Layer
import tensorflow as tf
from gru import GRUCell
# Policy/actor network
# Estimates the parameters mu and sigma of the normal distribution
# used to sample the actions for the agent
class Actor(Layer):
def __init__(self, env, network):
super(Actor, self).__init__()
self.action_space_size = env.action_space.shape[0]
self.observation_space_size = env.observation_space.shape[0]
self.type = network
if self.type == "gru":
self.cell_1 = GRUCell(input_dim=self.observation_space_size, units=64)
self.cell_2 = GRUCell(input_dim=64, units=32)
self.fc_gru = kl.Dense(units=32)
self.cells = [self.cell_1, self.cell_2]
self.rnn = tf.keras.layers.RNN(self.cells, return_sequences=True, return_state=True)
if self.type == "mlp":
self.fc1 = kl.Dense(units=128, activation='relu', kernel_regularizer="l2")
self.fc2 = kl.Dense(units=64, activation='relu')
self.fc3 = kl.Dense(units=32, activation='relu')
self.batch_norm = kl.BatchNormalization()
self.mu_out = kl.Dense(units=self.action_space_size, activation='tanh')
self.sigma_out = kl.Dense(units=self.action_space_size, activation='softplus')
def call(self, x, initial_state=None):
state = None
if self.type == "gru":
x, state_1, state_2 = self.rnn(x, initial_state=initial_state)
x, _ = tf.split(x, 2, axis=2)
state = [state_1, state_2]
x = self.fc_gru(x)
if self.type == "mlp":
x = self.fc1(x)
x = self.fc2(x)
x = self.fc3(x)
mu = self.mu_out(x)
sigma = self.sigma_out(x)
return tf.reshape(mu, [-1, self.action_space_size]), tf.reshape(sigma, [-1, self.action_space_size]), state