def testActivation(self): fc = core_layers.FullyConnected(2, activation=tf.nn.relu, name='fc1') inputs = tf.random_uniform((5, 3), seed=1) outputs = fc(inputs) self.assertEqual(outputs.op.name, 'fc1/Relu') fc = core_layers.FullyConnected(2, name='fc2') inputs = tf.random_uniform((5, 3), seed=1) outputs = fc(inputs) self.assertEqual(outputs.op.name, 'fc2/BiasAdd')
def testFCProperties(self): fc = core_layers.FullyConnected(2, activation=tf.nn.relu, name='fc') self.assertEqual(fc.output_dim, 2) self.assertEqual(fc.activation, tf.nn.relu) self.assertEqual(fc.w_regularizer, None) self.assertEqual(fc.bias_regularizer, None) self.assertEqual(fc.activity_regularizer, None) self.assertEqual(fc.use_bias, True) self.assertEqual(fc.name, 'fc') # Test auto-naming fc = core_layers.FullyConnected(2, activation=tf.nn.relu) self.assertEqual(fc.name, 'fully_connected') fc = core_layers.FullyConnected(2, activation=tf.nn.relu) self.assertEqual(fc.name, 'fully_connected_1')
def testBiasRegularizer(self): regularizer = lambda x: tf.reduce_sum(x) * 1e-3 fc = core_layers.FullyConnected(2, name='fc', bias_regularizer=regularizer) inputs = tf.random_uniform((5, 3), seed=1) _ = fc(inputs) loss_keys = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES) self.assertEqual(len(loss_keys), 1) self.assertListEqual(fc.losses, loss_keys)
def testNonTrainable(self): fc = core_layers.FullyConnected(2, trainable=False, name='fc') inputs = tf.random_uniform((5, 2), seed=1) _ = fc(inputs) self.assertListEqual(fc.weights, [fc.w, fc.bias]) self.assertListEqual(fc.non_trainable_weights, [fc.w, fc.bias]) self.assertListEqual(fc.trainable_weights, []) self.assertListEqual(fc._trainable_weights, [fc.w, fc.bias]) self.assertListEqual(fc._non_trainable_weights, []) self.assertEqual( len(tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)), 0)
def testNoBias(self): fc = core_layers.FullyConnected(2, use_bias=False, name='fc') inputs = tf.random_uniform((5, 2), seed=1) _ = fc(inputs) self.assertListEqual(fc.weights, [fc.w]) self.assertListEqual(fc.trainable_weights, [fc.w]) self.assertListEqual(fc.non_trainable_weights, []) self.assertEqual( len(tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)), 1) self.assertEqual(fc.w.name, 'fc/weights:0') self.assertEqual(fc.bias, None)
def testOutputShape(self): fc = core_layers.FullyConnected(7, activation=tf.nn.relu, name='fc') inputs = tf.random_uniform((5, 3), seed=1) outputs = fc.apply(inputs) self.assertEqual(outputs.get_shape().as_list(), [5, 7]) inputs = tf.random_uniform((5, 2, 3), seed=1) outputs = fc(inputs) self.assertEqual(outputs.get_shape().as_list(), [5, 2, 7]) inputs = tf.random_uniform((1, 2, 4, 3), seed=1) outputs = fc.apply(inputs) self.assertEqual(outputs.get_shape().as_list(), [1, 2, 4, 7])
def testCall(self): fc = core_layers.FullyConnected(2, activation=tf.nn.relu, name='fc') inputs = tf.random_uniform((5, 2), seed=1) _ = fc(inputs) self.assertListEqual(fc.weights, [fc.w, fc.bias]) self.assertListEqual(fc.trainable_weights, [fc.w, fc.bias]) self.assertListEqual(fc.non_trainable_weights, []) self.assertListEqual(fc._trainable_weights, [fc.w, fc.bias]) self.assertListEqual(fc._non_trainable_weights, []) self.assertEqual( len(tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)), 2) self.assertEqual(fc.w.name, 'fc/weights:0') self.assertEqual(fc.bias.name, 'fc/biases:0')
def testCallOnPlaceHolder(self): inputs = tf.placeholder(dtype=tf.float32) fc = core_layers.FullyConnected(4, name='fc') with self.assertRaises(ValueError): fc(inputs) inputs = tf.placeholder(dtype=tf.float32, shape=[None, None]) fc = core_layers.FullyConnected(4, name='fc') with self.assertRaises(ValueError): fc(inputs) inputs = tf.placeholder(dtype=tf.float32, shape=[None, None, None]) fc = core_layers.FullyConnected(4, name='fc') with self.assertRaises(ValueError): fc(inputs) inputs = tf.placeholder(dtype=tf.float32, shape=[None, 3]) fc = core_layers.FullyConnected(4, name='fc') fc(inputs) inputs = tf.placeholder(dtype=tf.float32, shape=[None, None, 3]) fc = core_layers.FullyConnected(4, name='fc') fc(inputs)