forked from Stanford-STAGES/sleep-staging
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsc_network.py
More file actions
208 lines (155 loc) · 7.36 KB
/
Copy pathsc_network.py
File metadata and controls
208 lines (155 loc) · 7.36 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
197
198
199
200
201
202
203
204
205
206
207
208
import sc_conv
import sc_config
import tensorflow as tf
class SCModel(object):
def __init__(self, config):
self.is_training = config.is_training
# Placeholders
self._features = tf.placeholder(tf.float32, [None, None, config.num_features], name='ModelInput')
self._targets = tf.placeholder(tf.float32, [None, config.num_classes], name='ModelOutput')
self._mask = tf.placeholder(tf.float32, [None], name='ModelWeights')
#self._batch_size = tf.placeholder_with_default(config.batch_size, [1,], name='BatchSize')
self._batch_size = tf.placeholder(tf.int32, name='BatchSize')
self._learning_rate = tf.placeholder(tf.float32, name='LearningRate')
batch_size_int = tf.reshape(self._batch_size, [])
if config.lstm:
self._initial_state = tf.placeholder_with_default(tf.zeros([batch_size_int,config.num_hidden*2],dtype=tf.float32), [None, config.num_hidden*2],name='InitialState')
# Layer in
with tf.variable_scope('input_hidden') as scope:
inputs = self._features
inputs = tf.reshape(inputs, shape=[batch_size_int, -1, config.segsize, config.num_features]) # (time, batch, feat) -> (time*batch, feat)
if config.scope=='oct':
hidden_eeg = sc_conv.main(inputs[:,:,:,:10],config,'eeg',batch_size_int)
hidden_eog = sc_conv.main(inputs[:,:,:,10:20],config,'eog',batch_size_int)
hidden_emg = sc_conv.main(inputs[:,:,:,20:],config,'emg',batch_size_int)
elif config.scope=='ac':
hidden_eeg = sc_conv.main(inputs[:,:,:,:400],config,'eeg',batch_size_int)
hidden_eog = sc_conv.main(inputs[:,:,:,400:1600],config,'eog',batch_size_int)
hidden_emg = sc_conv.main(inputs[:,:,:,1600:],config,'emg',batch_size_int)
print('ac')
hidden_combined = tf.concat(2,[hidden_eeg,hidden_eog,hidden_emg])
nHid = hidden_combined.get_shape()
# Regularization
if config.is_training and config.keep_prob < 1.0:
iKeepProb = config.keep_prob
oKeepProb = config.keep_prob
else:
iKeepProb = 1
oKeepProb = 1
# Layer hidden
with tf.variable_scope('hidden_hidden') as scope:
if config.lstm:
cell = tf.nn.rnn_cell.BasicLSTMCell(config.num_hidden, forget_bias=1.0,state_is_tuple=True)
cell = tf.nn.rnn_cell.DropoutWrapper(cell, input_keep_prob=iKeepProb, output_keep_prob=oKeepProb)
initial_state = tf.nn.rnn_cell.LSTMStateTuple(self._initial_state[:,:config.num_hidden],self._initial_state[:,config.num_hidden:])
outputs,final_state = tf.nn.dynamic_rnn(cell, hidden_combined, dtype=tf.float32, initial_state = initial_state)
else:
hidden_combined = tf.reshape(hidden_combined, [-1,int(nHid[2])])
weights = sc_conv._variable_with_weight_decay('weights', shape=[nHid[2], config.num_hidden],
stddev=0.04, wd=0.00001)
biases = sc_conv._variable_on_cpu('biases', config.num_hidden, tf.constant_initializer(0.01))
outputs = tf.nn.relu(tf.add(tf.matmul(hidden_combined, weights),biases), name=scope.name)
#sc_conv._activation_summary(outputs)
# Layer out
with tf.variable_scope('hidden_output') as scope:
outputs = tf.reshape(outputs, [-1,config.num_hidden])
weights = sc_conv._variable_with_weight_decay('weights', shape=[config.num_hidden,config.num_classes],
stddev=0.04, wd=0.00001)
biases = sc_conv._variable_on_cpu('biases', config.num_classes, tf.constant_initializer(0.001))
logits = tf.add(tf.matmul(outputs, weights), biases, name=scope.name)
#sc_conv._activation_summary(logits)
# Evaluate
cross_ent = self.intelligent_cost(logits)
loss = self.gather_loss()
self._loss = loss
self._logits = logits
self._cross_ent = cross_ent
self._softmax = tf.nn.softmax(logits)
self._predict = tf.argmax(self._softmax, 1)
self._correct = tf.equal(tf.argmax(logits, 1), tf.argmax(self._targets, 1))
self._accuracy = tf.reduce_mean(tf.cast(self._correct, tf.float32))
self._confidence = tf.reduce_sum(tf.multiply(self._softmax,self._targets),1);
self._baseline = (tf.reduce_mean(self._targets,0))
if config.lstm:
self._final_state = tf.concat(1,[final_state.c,final_state.h])
if not config.is_training:
return
# Optimize
optimizer = tf.train.MomentumOptimizer(learning_rate=self._learning_rate,momentum=0.9)
variables_averages = tf.train.ExponentialMovingAverage(0.999)
optimize = optimizer.minimize(self._loss)
variables_averages_op = variables_averages.apply(tf.trainable_variables())
with tf.control_dependencies([optimize, variables_averages_op]):
self._train_op = tf.no_op(name='train')
def intelligent_cost(self, logits):
logits = tf.clip_by_value(logits,-1e10,1e+10)
cross_ent = tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=self._targets)
#cross_ent = tf.mul(cross_ent, self._mask)
cross_ent = tf.reduce_mean(cross_ent)# / tf.reduce_sum(self._mask)
tf.add_to_collection('losses', cross_ent)
return cross_ent
def gather_loss(self):
loss_averages = tf.train.ExponentialMovingAverage(0.9,name='avg_loss')
losses = tf.get_collection('losses')
total_loss = tf.add_n(losses, name='total_loss')
loss_averages_op = loss_averages.apply(losses + [total_loss])
#for l in losses + [total_loss]:
# Name each loss as '(raw)' and name the moving average version of the loss
# as the original loss name.
#tf.scalar_summary(total_loss.op.name +' (raw)', total_loss)
#tf.scalar_summary(total_loss.op.name, loss_averages.average(total_loss))
return total_loss
@property
def features(self):
return self._features
@property
def final_state(self):
return self._final_state
@property
def initial_state(self):
return self._initial_state
@property
def targets(self):
return self._targets
@property
def mask(self):
return self._mask
@property
def batch_size(self):
return self._batch_size
@property
def learning_rate(self):
return self._learning_rate
@property
def cost(self):
return self._cost
@property
def loss(self):
return self._loss
@property
def cross_ent(self):
return self._cross_ent
@property
def accuracy(self):
return self._accuracy
@property
def baseline(self):
return self._baseline
@property
def train_op(self):
return self._train_op
@property
def predict(self):
return self._predict
@property
def logits(self):
return self._logits
@property
def confidence(self):
return self._confidence
@property
def ar_prob(self):
return self._ar_prob
@property
def softmax(self):
return self._softmax