def testLayerProperties(self): layer = base_layers._Layer(name='my_layer') self.assertEqual(layer.name, 'my_layer') self.assertListEqual(layer.variables, []) self.assertListEqual(layer.trainable_variables, []) self.assertListEqual(layer.non_trainable_variables, []) self.assertListEqual(layer.updates, []) self.assertListEqual(layer.losses, []) self.assertEqual(layer.built, False) layer = base_layers._Layer(name='my_layer', trainable=False) self.assertEqual(layer.trainable, False)
def testLayerProperties(self): layer = base_layers._Layer(name="my_layer") self.assertEqual(layer.name, "my_layer") self.assertListEqual(layer.variables, []) self.assertListEqual(layer.trainable_variables, []) self.assertListEqual(layer.non_trainable_variables, []) self.assertListEqual(layer.updates, []) self.assertListEqual(layer.losses, []) self.assertEqual(layer.built, False) layer = base_layers._Layer(name="my_layer", trainable=False) self.assertEqual(layer.trainable, False)
def testLayerProperties(self): layer = base_layers._Layer(name='my_layer') self.assertEqual(layer.name, 'my_layer') self.assertListEqual(layer.weights, []) self.assertListEqual(layer.trainable_weights, []) self.assertListEqual(layer.non_trainable_weights, []) self.assertListEqual(layer.updates, []) self.assertListEqual(layer.losses, []) self.assertEqual(layer.built, False) layer = base_layers._Layer(name='my_layer', trainable=False) self.assertEqual(layer.trainable, False)
def testLayerProperties(self): layer = base_layers._Layer(name='my_layer') self.assertListEqual(layer.variables, []) self.assertListEqual(layer.trainable_variables, []) self.assertListEqual(layer.non_trainable_variables, []) self.assertListEqual(layer.updates, []) self.assertListEqual(layer.losses, []) self.assertEqual(layer.built, False) with self.assertRaisesRegexp(ValueError, 'not been used yet'): _ = layer.name layer = base_layers._Layer(name='my_layer', trainable=False) self.assertEqual(layer.trainable, False)
def testAddWeight(self): with self.test_session(): layer = base_layers._Layer(name='my_layer') # Test basic variable creation. variable = layer._add_variable( 'my_var', [2, 2], initializer=init_ops.zeros_initializer()) self.assertEqual(variable.name, 'my_var:0') self.assertListEqual(layer.variables, [variable]) self.assertListEqual(layer.trainable_variables, [variable]) self.assertListEqual(layer.non_trainable_variables, []) self.assertListEqual( layer.variables, ops.get_collection(ops.GraphKeys.TRAINABLE_VARIABLES)) # Test non-trainable variable creation. # layer._add_variable should work even outside `build` and `call`. variable_2 = layer._add_variable( 'non_trainable_var', [2, 2], initializer=init_ops.zeros_initializer(), trainable=False) self.assertListEqual(layer.variables, [variable, variable_2]) self.assertListEqual(layer.trainable_variables, [variable]) self.assertListEqual(layer.non_trainable_variables, [variable_2]) self.assertEqual( len(ops.get_collection(ops.GraphKeys.TRAINABLE_VARIABLES)), 1) # Test with regularizer. regularizer = lambda x: math_ops.reduce_sum(x) * 1e-3 variable = layer._add_variable( 'reg_var', [2, 2], initializer=init_ops.zeros_initializer(), regularizer=regularizer) self.assertEqual(len(layer.losses), 1)
def testAddWeight(self): with self.test_session(): layer = base_layers._Layer(name='my_layer') # Test basic variable creation. variable = layer._add_weight('my_var', [2, 2], initializer=tf.zeros_initializer) self.assertEqual(variable.name, 'my_var:0') self.assertListEqual(layer.weights, [variable]) self.assertListEqual(layer.trainable_weights, [variable]) self.assertListEqual(layer.non_trainable_weights, []) self.assertListEqual(layer.weights, tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)) # Test non-trainable variable creation. # layer._add_weight should work even outside `build` and `call`. variable_2 = layer._add_weight('non_trainable_var', [2, 2], initializer=tf.zeros_initializer, trainable=False) self.assertListEqual(layer.weights, [variable, variable_2]) self.assertListEqual(layer.trainable_weights, [variable]) self.assertListEqual(layer.non_trainable_weights, [variable_2]) self.assertEqual( len(tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)), 1) # Test with regularizer. regularizer = lambda x: tf.reduce_sum(x) * 1e-3 variable = layer._add_weight('reg_var', [2, 2], initializer=tf.zeros_initializer, regularizer=regularizer) self.assertEqual(len(layer.losses), 1)
def testAddWeight(self): with self.test_session(): layer = base_layers._Layer(name="my_layer") # Test basic variable creation. variable = layer._add_variable("my_var", [2, 2], initializer=init_ops.zeros_initializer) self.assertEqual(variable.name, "my_var:0") self.assertListEqual(layer.variables, [variable]) self.assertListEqual(layer.trainable_variables, [variable]) self.assertListEqual(layer.non_trainable_variables, []) self.assertListEqual(layer.variables, ops.get_collection(ops.GraphKeys.TRAINABLE_VARIABLES)) # Test non-trainable variable creation. # layer._add_variable should work even outside `build` and `call`. variable_2 = layer._add_variable( "non_trainable_var", [2, 2], initializer=init_ops.zeros_initializer, trainable=False ) self.assertListEqual(layer.variables, [variable, variable_2]) self.assertListEqual(layer.trainable_variables, [variable]) self.assertListEqual(layer.non_trainable_variables, [variable_2]) self.assertEqual(len(ops.get_collection(ops.GraphKeys.TRAINABLE_VARIABLES)), 1) # Test with regularizer. regularizer = lambda x: math_ops.reduce_sum(x) * 1e-3 variable = layer._add_variable( "reg_var", [2, 2], initializer=init_ops.zeros_initializer, regularizer=regularizer ) self.assertEqual(len(layer.losses), 1)
def testNaming(self): default_layer = base_layers._Layer() self.assertEqual(default_layer.name, 'private__layer') default_layer1 = base_layers._Layer() self.assertEqual(default_layer1.name, 'private__layer_1') my_layer = base_layers._Layer(name='my_layer') self.assertEqual(my_layer.name, 'my_layer') my_layer1 = base_layers._Layer(name='my_layer') self.assertEqual(my_layer1.name, 'my_layer_1') # New graph has fully orthogonal names. with ops.Graph().as_default(): my_layer_other_graph = base_layers._Layer(name='my_layer') self.assertEqual(my_layer_other_graph.name, 'my_layer') my_layer2 = base_layers._Layer(name='my_layer') self.assertEqual(my_layer2.name, 'my_layer_2') # Name scope shouldn't affect names. with ops.name_scope('some_name_scope'): default_layer2 = base_layers._Layer() self.assertEqual(default_layer2.name, 'private__layer_2') my_layer3 = base_layers._Layer(name='my_layer') self.assertEqual(my_layer3.name, 'my_layer_3') other_layer = base_layers._Layer(name='other_layer') self.assertEqual(other_layer.name, 'other_layer') # Variable scope gets added to names. with variable_scope.variable_scope('var_scope'): default_layer_scoped = base_layers._Layer() self.assertEqual(default_layer_scoped.name, 'var_scope/private__layer') my_layer_scoped = base_layers._Layer(name='my_layer') self.assertEqual(my_layer_scoped.name, 'var_scope/my_layer') my_layer_scoped1 = base_layers._Layer(name='my_layer') self.assertEqual(my_layer_scoped1.name, 'var_scope/my_layer_1')
def testNaming(self): default_layer = base_layers._Layer() self.assertEqual(default_layer.name, "private__layer") default_layer1 = base_layers._Layer() self.assertEqual(default_layer1.name, "private__layer_1") my_layer = base_layers._Layer(name="my_layer") self.assertEqual(my_layer.name, "my_layer") my_layer1 = base_layers._Layer(name="my_layer") self.assertEqual(my_layer1.name, "my_layer_1") # New graph has fully orthogonal names. with ops.Graph().as_default(): my_layer_other_graph = base_layers._Layer(name="my_layer") self.assertEqual(my_layer_other_graph.name, "my_layer") my_layer2 = base_layers._Layer(name="my_layer") self.assertEqual(my_layer2.name, "my_layer_2") # Name scope shouldn't affect names. with ops.name_scope("some_name_scope"): default_layer2 = base_layers._Layer() self.assertEqual(default_layer2.name, "private__layer_2") my_layer3 = base_layers._Layer(name="my_layer") self.assertEqual(my_layer3.name, "my_layer_3") other_layer = base_layers._Layer(name="other_layer") self.assertEqual(other_layer.name, "other_layer") # Variable scope gets added to names. with variable_scope.variable_scope("var_scope"): default_layer_scoped = base_layers._Layer() self.assertEqual(default_layer_scoped.name, "var_scope/private__layer") my_layer_scoped = base_layers._Layer(name="my_layer") self.assertEqual(my_layer_scoped.name, "var_scope/my_layer") my_layer_scoped1 = base_layers._Layer(name="my_layer") self.assertEqual(my_layer_scoped1.name, "var_scope/my_layer_1")