|
5 | 5 |
|
6 | 6 |
|
7 | 7 | class Network(): |
8 | | - def __init__(self, train_batch_size, test_batch_size, pooling_scale): |
| 8 | + def __init__(self, train_batch_size, test_batch_size, pooling_scale, |
| 9 | + optimizeMethod='adam'): |
9 | 10 | ''' |
10 | 11 | @num_hidden: 隐藏层的节点数量 |
11 | 12 | @batch_size:因为我们要节省内存,所以分批处理数据。每一批的数据量。 |
12 | 13 | ''' |
| 14 | + self.optimizeMethod = optimizeMethod; |
| 15 | + |
13 | 16 | self.train_batch_size = train_batch_size |
14 | 17 | self.test_batch_size = test_batch_size |
15 | 18 |
|
@@ -148,9 +151,32 @@ def model(data_flow, train=True): |
148 | 151 | self.loss += self.apply_regularization(_lambda=5e-4) |
149 | 152 | self.train_summaries.append(tf.scalar_summary('Loss', self.loss)) |
150 | 153 |
|
| 154 | + # learning rate decay |
| 155 | + global_step = tf.Variable(0) |
| 156 | + lr = 0.001 |
| 157 | + dr = 0.99 |
| 158 | + learning_rate = tf.train.exponential_decay( |
| 159 | + learning_rate=lr, |
| 160 | + global_step=global_step*self.train_batch_size, |
| 161 | + decay_steps=100, |
| 162 | + decay_rate=dr, |
| 163 | + staircase=True |
| 164 | + ) |
| 165 | + |
151 | 166 | # Optimizer. |
152 | 167 | with tf.name_scope('optimizer'): |
153 | | - self.optimizer = tf.train.GradientDescentOptimizer(0.0001).minimize(self.loss) |
| 168 | + if(self.optimizeMethod=='gradient'): |
| 169 | + self.optimizer = tf.train \ |
| 170 | + .GradientDescentOptimizer(learning_rate) \ |
| 171 | + .minimize(self.loss) |
| 172 | + elif(self.optimizeMethod=='momentum'): |
| 173 | + self.optimizer = tf.train \ |
| 174 | + .MomentumOptimizer(learning_rate, 0.5) \ |
| 175 | + .minimize(self.loss) |
| 176 | + elif(self.optimizeMethod=='adam'): |
| 177 | + self.optimizer = tf.train \ |
| 178 | + .AdamOptimizer(learning_rate) \ |
| 179 | + .minimize(self.loss) |
154 | 180 |
|
155 | 181 | # Predictions for the training, validation, and test data. |
156 | 182 | with tf.name_scope('train'): |
|
0 commit comments