def testConvBatchNormArgument(self):
   st = self.input_layer.sequential()
   st.reshape([DIM_SAME, DIM_SAME, DIM_SAME, 1])
   st.conv2d(3, 2,
             batch_normalize=prettytensor.BatchNormalizationArguments(
                 scale_after_normalization=False))
   self.assertEqual(2,
                    len(tf.get_collection(prettytensor.GraphKeys.UPDATE_OPS)))
   self.assertTrue(tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, '.*/beta'))
   self.assertFalse(tf.get_collection(
       tf.GraphKeys.GLOBAL_VARIABLES, '.*/gamma'))
   self.assertTrue(tf.get_collection(
       tf.GraphKeys.GLOBAL_VARIABLES, '.*/moving_variance'))
   self.assertTrue(tf.get_collection(
       tf.GraphKeys.GLOBAL_VARIABLES, '.*/moving_mean'))
Пример #2
0
 def testConvBatchNorm(self):
   st = self.input_layer.sequential()
   st.reshape([DIM_SAME, DIM_SAME, DIM_SAME, 1])
   with prettytensor.defaults_scope(
       batch_normalize=prettytensor.BatchNormalizationArguments(
           learned_moments_update_rate=0.0003,
           variance_epsilon=0.001,
           scale_after_normalization=True)):
     st.conv2d(3, 2)
   self.assertEqual(2,
                    len(tf.get_collection(prettytensor.GraphKeys.UPDATE_OPS)))
   self.assertTrue(tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, '.*/beta'))
   self.assertTrue(tf.get_collection(
       tf.GraphKeys.GLOBAL_VARIABLES, '.*/gamma'))
   self.assertTrue(tf.get_collection(
       tf.GraphKeys.GLOBAL_VARIABLES, '.*/moving_variance'))
   self.assertTrue(tf.get_collection(
       tf.GraphKeys.GLOBAL_VARIABLES, '.*/moving_mean'))
    return x_train, y_train


img_size = 28
img_size_flat = img_size * img_size
img_shape = (img_size, img_size)
num_channels = 1
num_classes = 10

x = tf.placeholder(tf.float32, shape=[None, img_size_flat], name='x')
x_image = tf.reshape(x, [-1, img_size, img_size, num_channels])
y_true = tf.placeholder(tf.float32, shape=[None, 10], name='y_true')
y_true_cls = tf.argmax(y_true, dimension=1)

x_wrap = pt.wrap(x_image)
norm = pt.BatchNormalizationArguments(scale_after_normalization=True)

with pt.defaults_scope(activation_fn=tf.nn.relu):
    y_pred, loss = x_wrap.\
        conv2d(kernel=5, depth=16, name='conv1', batch_normalize=norm).\
        max_pool(kernel=2, stride=2).\
        conv2d(kernel=5, depth=36, name='conv2', batch_normalize=norm).\
        max_pool(kernel=2, stride=2).\
        flatten().\
        fully_connected(size=128, name='fc1').\
        softmax_classifier(num_classes=num_classes, labels=y_true)

optimizer = tf.train.AdamOptimizer(learning_rate=1e-4).minimize(loss)
y_pred_cls = tf.argmax(y_pred, dimension=1)
correct_prediction = tf.equal(y_pred_cls, y_true_cls)
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))