-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
196 lines (166 loc) · 7.96 KB
/
model.py
File metadata and controls
196 lines (166 loc) · 7.96 KB
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
import tensorflow as tf
import numpy as np
class DNNModel(object):
"""
Creates training and validation computational graphs.
Note that tf.variable_scope enables parameter sharing so that both graphs are identical.
"""
def __init__(self, config, placeholders, mode):
"""
Basic setup.
:param config: configuration dictionary
:param placeholders: dictionary of input placeholders
:param mode: training, validation or inference
"""
assert mode in ['training', 'validation', 'inference']
self.delay = config['delay']
self.config = config
self.input_ = placeholders['input_pl']
self.target = placeholders['target_pl']
self.out_last_last = self.input_
self.out_last = self.input_
self.out = self.input_
self.mode = mode
self.is_training = self.mode == 'training'
self.reuse = ((self.mode == 'validation') or (self.mode == 'inference'))
self.batch_size = tf.shape(self.input_)[0] # dynamic size
self.input_dim = config['input_dim']
self.output_dim = config['output_dim']
self.length_split = config['length_split']
self.summary_collection = 'training_summaries' if mode == 'training' else 'validation_summaries'
def build_graph(self):
self.build_model()
self.build_loss()
self.count_parameters()
def build_model(self):
"""
Builds the actual model.
"""
with tf.variable_scope('rnn_model', reuse=self.reuse):
x = tf.layers.dense(self.input_, units=int(self.input_dim), reuse=self.reuse, name='layer1')
x = tf.nn.relu(x)
x = tf.layers.dense(x, units=int(self.input_dim/4), reuse=self.reuse, name='layer2')
x = tf.nn.relu(x)
x = tf.layers.dense(x, units=int(self.input_dim/16), reuse=self.reuse, name='layer3')
x = tf.nn.relu(x)
x = tf.layers.dropout(x, rate=0.1, training=self.is_training)
x = tf.layers.dense(x, units=self.output_dim, reuse=self.reuse, name='layer4')
x = tf.nn.relu(x)
outputs = tf.nn.softmax(x, name='softmaxLayer')
self.prediction = outputs
def build_loss(self):
"""
Builds the loss function.
"""
# only need loss if we are not in inference mode
if self.mode != 'inference':
with tf.name_scope('loss'):
self.loss = tf.losses.softmax_cross_entropy(onehot_labels=self.target, logits=self.prediction)
tf.summary.scalar('loss', self.loss, collections=[self.summary_collection])
if self.mode == 'validation':
with tf.name_scope('loss'):
self.loss = tf.losses.softmax_cross_entropy(onehot_labels=self.target, logits=self.prediction)
def count_parameters(self):
"""
Counts the number of trainable parameters in this model
"""
self.n_parameters = 0
for v in tf.trainable_variables():
params = 1
for s in v.get_shape():
params *= s.value
self.n_parameters += params
class CNNModel(object):
"""
Creates training and validation computational graphs.
Note that tf.variable_scope enables parameter sharing so that both graphs are identical.
"""
def __init__(self, config, placeholders, mode):
"""
Basic setup.
:param config: configuration dictionary
:param placeholders: dictionary of input placeholders
:param mode: training, validation or inference
"""
assert mode in ['training', 'validation', 'inference']
self.delay = config['delay']
self.config = config
self.input_ = placeholders['input_pl']
self.target = placeholders['target_pl']
self.out_last_last = self.input_
self.out_last = self.input_
self.out = self.input_
self.mode = mode
self.is_training = self.mode == 'training'
self.reuse = ((self.mode == 'validation') or (self.mode == 'inference'))
self.batch_size = tf.shape(self.input_)[0] # dynamic size
self.input_dim = config['input_dim']
self.output_dim = config['output_dim']
self.length_split = config['length_split']
self.summary_collection = 'training_summaries' if mode == 'training' else 'validation_summaries'
def build_graph(self):
self.build_model()
self.build_loss()
self.count_parameters()
def build_model(self):
"""
Builds the actual model.
"""
with tf.variable_scope('cnn_model', reuse=self.reuse):
x = tf.expand_dims(self.input_, -1)
x = tf.layers.batch_normalization(x, training=self.is_training, momentum=0.99)
x = tf.layers.conv1d(x, filters=3, kernel_size=5, strides=1, \
data_format='channels_last', reuse=self.reuse, name='layer1')
x = tf.nn.relu(x)
x = tf.layers.max_pooling1d(x, pool_size=3, strides=2, data_format='channels_last')
x = tf.layers.dropout(x, rate=0.1, training=self.is_training)
x = tf.layers.batch_normalization(x, training=self.is_training, momentum=0.99)
x = tf.layers.conv1d(x, filters=5, kernel_size=5, strides=1, \
data_format='channels_last', reuse=self.reuse, name='layer2')
x = tf.nn.relu(x)
x = tf.layers.max_pooling1d(x, pool_size=5, strides=2, data_format='channels_last')
x = tf.layers.batch_normalization(x, training=self.is_training, momentum=0.99)
x = tf.layers.conv1d(x, filters=10, kernel_size=5, strides=1, \
data_format='channels_last', reuse=self.reuse, name='layer3')
x = tf.nn.relu(x)
x = tf.layers.max_pooling1d(x, pool_size=10, strides=2, data_format='channels_last')
x = tf.layers.dropout(x, rate=0.1, training=self.is_training)
x = tf.layers.batch_normalization(x, training=self.is_training, momentum=0.99)
x = tf.layers.conv1d(x, filters=10, kernel_size=4, strides=1, \
data_format='channels_last', reuse=self.reuse, name='layer4')
x = tf.nn.relu(x)
x = tf.layers.max_pooling1d(x, pool_size=10, strides=2, data_format='channels_last')
x = tf.contrib.layers.flatten(x)
# dense layers
x = tf.layers.dense(x, units=20, reuse=self.reuse, name='layer7')
x = tf.nn.relu(x)
x = tf.layers.dense(x, units=10, reuse=self.reuse, name='layer8')
x = tf.nn.relu(x)
x = tf.layers.dense(x, units=self.output_dim, reuse=self.reuse, name='layer9')
x = tf.nn.relu(x)
outputs = tf.nn.softmax(x, name='softmaxLayer')
self.prediction = outputs
def build_loss(self):
"""
Builds the loss function.
"""
# only need loss if we are not in inference mode
if self.mode != 'inference':
with tf.name_scope('loss'):
# squared difference
self.loss = tf.losses.softmax_cross_entropy(onehot_labels=self.target, logits=self.prediction)
# L1 loss
tf.summary.scalar('loss', self.loss, collections=[self.summary_collection])
if self.mode == 'validation':
with tf.name_scope('loss'):
self.loss = tf.losses.softmax_cross_entropy(onehot_labels=self.target, logits=self.prediction)
def count_parameters(self):
"""
Counts the number of trainable parameters in this model
"""
self.n_parameters = 0
for v in tf.trainable_variables():
params = 1
for s in v.get_shape():
params *= s.value
self.n_parameters += params