-
Notifications
You must be signed in to change notification settings - Fork 5
/
NeuralNets.py
104 lines (94 loc) · 5.56 KB
/
NeuralNets.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
import tensorflow as tf
import numpy as np
import Common_constants as CC
obs_shape = CC.obs_shape
num_actions = CC.num_actions
class value_nn(tf.keras.Model):
def __init__(self):
super(value_nn,self).__init__(name='Value_Net')
self.conv1 = tf.keras.layers.Conv2D(filters = 32,
kernel_size = (8, 8),
strides = (4, 4),
padding = 'same',
input_shape = obs_shape,
kernel_initializer=tf.keras.initializers.Orthogonal(np.sqrt(2)),
bias_initializer=tf.keras.initializers.Zeros(),
activation = 'linear')
self.conv2 = tf.keras.layers.Conv2D(filters = 64,
kernel_size = (4, 4),
strides = (2, 2),
padding = 'same',
kernel_initializer=tf.keras.initializers.Orthogonal(np.sqrt(2)),
bias_initializer=tf.keras.initializers.Zeros(),
activation = 'linear')
self.conv3 = tf.keras.layers.Conv2D(filters = 64,
kernel_size = (3, 3),
strides = (1, 1),
padding = 'same',
kernel_initializer=tf.keras.initializers.Orthogonal(np.sqrt(2)),
bias_initializer=tf.keras.initializers.Zeros(),
activation = 'linear')
self.flatten = tf.keras.layers.Flatten()
self.dense1 = tf.keras.layers.Dense(512,
kernel_initializer=tf.keras.initializers.Orthogonal(np.sqrt(2)),
bias_initializer=tf.keras.initializers.Zeros(),
activation = 'relu')
self.out = tf.keras.layers.Dense(1,
activation = 'linear',
bias_initializer=tf.keras.initializers.Ones(),
kernel_initializer=tf.keras.initializers.Orthogonal(.01))
@tf.function
def call(self,inputs):
x = self.conv1(inputs)
x = tf.keras.layers.LeakyReLU()(x)
x = self.conv2(x)
x = tf.keras.layers.LeakyReLU()(x)
x = self.conv3(x)
x = tf.keras.layers.LeakyReLU()(x)
x = self.flatten(x)
x = self.dense1(x)
return self.out(x)
class policy_nn(tf.keras.Model):
def __init__(self):
super(policy_nn,self).__init__(name='Policy_Net')
self.conv1 = tf.keras.layers.Conv2D(filters = 32,
kernel_size = (8, 8),
strides = (4, 4),
padding = 'same',
input_shape = obs_shape,
kernel_initializer=tf.keras.initializers.Orthogonal(np.sqrt(2)),
bias_initializer=tf.keras.initializers.Zeros(),
activation = 'linear')
self.conv2 = tf.keras.layers.Conv2D(filters = 64,
kernel_size = (4, 4),
strides = (2, 2),
padding = 'same',
kernel_initializer=tf.keras.initializers.Orthogonal(np.sqrt(2)),
bias_initializer=tf.keras.initializers.Zeros(),
activation = 'linear')
self.conv3 = tf.keras.layers.Conv2D(filters = 64,
kernel_size = (3, 3),
strides = (1, 1),
padding = 'same',
kernel_initializer=tf.keras.initializers.Orthogonal(np.sqrt(2)),
bias_initializer=tf.keras.initializers.Zeros(),
activation = 'linear')
self.flatten = tf.keras.layers.Flatten()
self.dense1 = tf.keras.layers.Dense(512,
kernel_initializer=tf.keras.initializers.Orthogonal(np.sqrt(2)),
bias_initializer=tf.keras.initializers.Zeros(),
activation = 'relu')
self.out = tf.keras.layers.Dense(num_actions,
bias_initializer=tf.keras.initializers.Zeros(),
activation = 'softmax')
@tf.function
def call(self,inputs):
x = self.conv1(inputs)
x = tf.keras.layers.LeakyReLU()(x)
x = self.conv2(x)
x = tf.keras.layers.LeakyReLU()(x)
x = self.conv3(x)
x = tf.keras.layers.LeakyReLU()(x)
x = self.flatten(x)
x = self.dense1(x)
return self.out(x)