def test_save_restore(self): with self.test_session(): checkpoint_directory = self.get_temp_dir() checkpoint_prefix = os.path.join(checkpoint_directory, "ckpt") m = metrics.Sum() checkpoint = tf.train.Checkpoint(sum=m) self.evaluate(tf.compat.v1.variables_initializer(m.variables)) # update state self.evaluate(m(100.0)) self.evaluate(m(200.0)) # save checkpoint and then add an update save_path = checkpoint.save(checkpoint_prefix) self.evaluate(m(1000.0)) # restore to the same checkpoint sum object (= 300) checkpoint.restore(save_path).assert_consumed().run_restore_ops() self.evaluate(m(300.0)) self.assertEqual(600.0, self.evaluate(m.result())) # restore to a different checkpoint sum object restore_sum = metrics.Sum() restore_checkpoint = tf.train.Checkpoint(sum=restore_sum) status = restore_checkpoint.restore(save_path) restore_update = restore_sum(300.0) status.assert_consumed().run_restore_ops() self.evaluate(restore_update) self.assertEqual(600.0, self.evaluate(restore_sum.result()))
def test_sum(self): with self.test_session(): m = metrics.Sum(name="my_sum") # check config self.assertEqual(m.name, "my_sum") self.assertTrue(m.stateful) self.assertEqual(m.dtype, tf.float32) self.assertLen(m.variables, 1) self.evaluate(tf.compat.v1.variables_initializer(m.variables)) # check initial state self.assertEqual(self.evaluate(m.total), 0) # check __call__() self.assertEqual(self.evaluate(m(100)), 100) self.assertEqual(self.evaluate(m.total), 100) # check update_state() and result() + state accumulation + tensor # input update_op = m.update_state(tf.convert_to_tensor([1, 5])) self.evaluate(update_op) self.assertAlmostEqual(self.evaluate(m.result()), 106) self.assertEqual(self.evaluate(m.total), 106) # 100 + 1 + 5 # check reset_state() m.reset_state() self.assertEqual(self.evaluate(m.total), 0)
def __init__(self): super(MetricLayer, self).__init__(name="metric_layer") self.sum = metrics.Sum(name="sum") # Using aggregation for jit_compile results in failure. Thus only set # aggregation for PS Strategy for multi-gpu tests. if isinstance(distribution, tf.distribute.experimental.ParameterServerStrategy): self.sum_var = tf.Variable( 1.0, aggregation=tf.VariableAggregation.ONLY_FIRST_REPLICA) else: self.sum_var = tf.Variable(1.0)
def test_sum_graph_with_placeholder(self): with tf.compat.v1.get_default_graph().as_default( ), self.cached_session() as sess: # noqa: E501 m = metrics.Sum() v = tf.compat.v1.placeholder(tf.float32) w = tf.compat.v1.placeholder(tf.float32) self.evaluate(tf.compat.v1.variables_initializer(m.variables)) # check __call__() result_t = m(v, sample_weight=w) result = sess.run(result_t, feed_dict=({v: 100, w: 0.5})) self.assertEqual(result, 50) self.assertEqual(self.evaluate(m.total), 50) # check update_state() and result() result = sess.run(result_t, feed_dict=({v: [1, 5], w: [1, 0.2]})) self.assertAlmostEqual(result, 52.0, 2) # 50 + 1 + 5 * 0.2 self.assertAlmostEqual(self.evaluate(m.total), 52.0, 2)
def test_sum_with_sample_weight(self): m = metrics.Sum(dtype=tf.float64) self.assertEqual(m.dtype, tf.float64) self.evaluate(tf.compat.v1.variables_initializer(m.variables)) # check scalar weight result_t = m(100, sample_weight=0.5) self.assertEqual(self.evaluate(result_t), 50) self.assertEqual(self.evaluate(m.total), 50) # check weights not scalar and weights rank matches values rank result_t = m([1, 5], sample_weight=[1, 0.2]) result = self.evaluate(result_t) self.assertAlmostEqual(result, 52.0, 4) # 50 + 1 + 5 * 0.2 self.assertAlmostEqual(self.evaluate(m.total), 52.0, 4) # check weights broadcast result_t = m([1, 2], sample_weight=0.5) self.assertAlmostEqual(self.evaluate(result_t), 53.5, 1) # 52 + 0.5 + 1 self.assertAlmostEqual(self.evaluate(m.total), 53.5, 1) # check weights squeeze result_t = m([1, 5], sample_weight=[[1], [0.2]]) self.assertAlmostEqual(self.evaluate(result_t), 55.5, 1) # 53.5 + 1 + 1 self.assertAlmostEqual(self.evaluate(m.total), 55.5, 1) # check weights expand result_t = m([[1], [5]], sample_weight=[1, 0.2]) self.assertAlmostEqual(self.evaluate(result_t), 57.5, 2) # 55.5 + 1 + 1 self.assertAlmostEqual(self.evaluate(m.total), 57.5, 1) # check values reduced to the dimensions of weight result_t = m([[[1.0, 2.0], [3.0, 2.0], [0.5, 4.0]]], sample_weight=[0.5]) result = np.round(self.evaluate(result_t), decimals=2) # result = (prev: 57.5) + 0.5 + 1 + 1.5 + 1 + 0.25 + 2 self.assertAlmostEqual(result, 63.75, 2) self.assertAlmostEqual(self.evaluate(m.total), 63.75, 2)
def test_sum_with_sample_weight(self): m = metrics.Sum(dtype='float64') assert m.dtype == 'float64' # check scalar weight result_t = m(100, sample_weight=0.5) assert K.eval(result_t) == 50 assert K.eval(m.total) == 50 # check weights not scalar and weights rank matches values rank result_t = m([1, 5], sample_weight=[1, 0.2]) result = K.eval(result_t) assert np.isclose(result, 52.) # 50 + 1 + 5 * 0.2 assert np.isclose(K.eval(m.total), 52.) # check weights broadcast result_t = m([1, 2], sample_weight=0.5) assert np.isclose(K.eval(result_t), 53.5) # 52 + 0.5 + 1 assert np.isclose(K.eval(m.total), 53.5) # check weights squeeze result_t = m([1, 5], sample_weight=[[1], [0.2]]) assert np.isclose(K.eval(result_t), 55.5) # 53.5 + 1 + 1 assert np.isclose(K.eval(m.total), 55.5) # check weights expand result_t = m([[1], [5]], sample_weight=[1, 0.2]) assert np.isclose(K.eval(result_t), 57.5, 2) # 55.5 + 1 + 1 assert np.isclose(K.eval(m.total), 57.5, 1) # check values reduced to the dimensions of weight result_t = m([[[1., 2.], [3., 2.], [0.5, 4.]]], sample_weight=[0.5]) result = np.round(K.eval(result_t), decimals=2) # result = (prev: 57.5) + 0.5 + 1 + 1.5 + 1 + 0.25 + 2 assert np.isclose(result, 63.75, 2) assert np.isclose(K.eval(m.total), 63.75, 2)
def test_sum(self): m = metrics.Sum(name='my_sum', dtype='float32') # check config assert m.name == 'my_sum' assert m.stateful assert m.dtype == 'float32' assert len(m.weights) == 1 # check initial state assert K.eval(m.total) == 0 # check __call__ assert K.eval(m(100.0)) == 100 assert K.eval(m.total) == 100 # check update_state() and result() + state accumulation + tensor input result = m([1, 5]) assert np.isclose(K.eval(result), 106) assert K.eval(m.total) == 106 # 100 + 1 + 5 # check reset_states() m.reset_states() assert K.eval(m.total) == 0
def create_variables(): # When this method is called in a graph context, any usage of # `tf.init_scope` will bypass this variable creator scope, resulting # in different behavior. with tf.variable_creator_scope(capture_variable_creation): return metrics.Sum().variables
def __init__(self): super(MetricLayer, self).__init__(name="metric_layer") self.sum = metrics.Sum(name="sum") self.sum_var = tf.Variable(1.0)