forked from DongjunLee/dqn-tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
101 lines (67 loc) · 4 KB
/
model.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
import tensorflow as tf
class MLPv1:
def __init__(self, X: tf.placeholder, num_classes: int, frame_size: None, learning_rate=0.001) -> None:
state_length = X.get_shape().as_list()[1]
self.X = tf.reshape(X, [-1, state_length])
self.num_classes = num_classes
self.learning_rate = learning_rate
def build_network(self) -> None:
net = self.X
net = tf.layers.dense(net, 16, activation=tf.nn.relu)
net = tf.layers.dense(net, 64, activation=tf.nn.relu)
net = tf.layers.dense(net, 32, activation=tf.nn.relu)
net = tf.layers.dense(net, self.num_classes)
self.inference = net
self.predict = tf.argmax(self.inference, 1)
self.Y = tf.placeholder(tf.float32, shape=[None, self.num_classes])
self.loss = tf.losses.mean_squared_error(self.Y, self.inference)
self.optimizer = tf.train.AdamOptimizer(
learning_rate=self.learning_rate).minimize(self.loss)
class ConvNetv1:
def __init__(self, X: tf.placeholder, num_classes: int, frame_size: int=1, learning_rate=0.001) -> None:
self.X = tf.reshape(X, [-1, 128, frame_size])
self.num_classes = num_classes
self.learning_rate = learning_rate
def build_network(self) -> None:
conv1 = tf.layers.conv1d(self.X, 32, kernel_size=3, padding="same", activation=tf.nn.relu)
pool1 = tf.layers.max_pooling1d(inputs=conv1, pool_size=2, strides=2)
conv2 = tf.layers.conv1d(pool1, 64, kernel_size=3, padding="same", activation=tf.nn.relu)
pool2 = tf.layers.max_pooling1d(inputs=conv2, pool_size=2, strides=2)
conv3 = tf.layers.conv1d(pool2, 128, kernel_size=3, padding="same", activation=tf.nn.relu)
pool3 = tf.layers.max_pooling1d(inputs=conv3, pool_size=2, strides=2)
pool3_flat = tf.reshape(pool3, [-1, 16 * 128])
net = tf.layers.dense(pool3_flat, 512)
net = tf.layers.dense(net, 128)
net = tf.layers.dense(net, self.num_classes)
self.inference = net
self.predict = tf.argmax(self.inference, 1)
self.Y = tf.placeholder(tf.float32, shape=[None, self.num_classes])
self.loss = tf.losses.mean_squared_error(self.Y, self.inference)
self.optimizer = tf.train.AdamOptimizer(
learning_rate=self.learning_rate).minimize(self.loss)
class ConvNetv2:
def __init__(self, X: tf.placeholder, num_classes: int, frame_size: int=1, learning_rate=0.001) -> None:
self.X = tf.reshape(X, [-1, 128, frame_size])
self.num_classes = num_classes
self.learning_rate = learning_rate
def build_network(self) -> None:
conv1 = tf.layers.conv1d(self.X, 128, kernel_size=7, padding="same", activation=tf.nn.relu)
pool1 = tf.layers.max_pooling1d(inputs=conv1, pool_size=4, strides=2)
conv2 = tf.layers.conv1d(pool1, 256, kernel_size=5, padding="same", activation=tf.nn.relu)
pool2 = tf.layers.max_pooling1d(inputs=conv2, pool_size=3, strides=2)
conv3 = tf.layers.conv1d(pool2, 512, kernel_size=3, padding="same", activation=tf.nn.relu)
pool3 = tf.layers.max_pooling1d(inputs=conv3, pool_size=2, strides=2)
conv4 = tf.layers.conv1d(pool3, 512, kernel_size=3, padding="same", activation=tf.nn.relu)
pool4 = tf.layers.max_pooling1d(inputs=conv4, pool_size=2, strides=2)
conv5 = tf.layers.conv1d(pool4, 512, kernel_size=3, padding="same", activation=tf.nn.relu)
pool5 = tf.layers.max_pooling1d(inputs=conv5, pool_size=2, strides=2)
pool5_flat = tf.reshape(pool5, [-1, 3 * 512])
net = tf.layers.dense(pool5_flat, 1024)
net = tf.layers.dense(net, 256)
net = tf.layers.dense(net, self.num_classes)
self.inference = net
self.predict = tf.argmax(self.inference, 1)
self.Y = tf.placeholder(tf.float32, shape=[None, self.num_classes])
self.loss = tf.losses.mean_squared_error(self.Y, self.inference)
self.optimizer = tf.train.AdamOptimizer(
learning_rate=self.learning_rate).minimize(self.loss)