(self)
| 131 | self.assertAlmostEqual(self.evaluate(m.total), 52., 2) |
| 132 | |
| 133 | def test_save_restore(self): |
| 134 | checkpoint_directory = self.get_temp_dir() |
| 135 | checkpoint_prefix = os.path.join(checkpoint_directory, 'ckpt') |
| 136 | m = metrics.Sum() |
| 137 | checkpoint = trackable_utils.Checkpoint(sum=m) |
| 138 | self.evaluate(variables.variables_initializer(m.variables)) |
| 139 | |
| 140 | # update state |
| 141 | self.evaluate(m(100.)) |
| 142 | self.evaluate(m(200.)) |
| 143 | |
| 144 | # save checkpoint and then add an update |
| 145 | save_path = checkpoint.save(checkpoint_prefix) |
| 146 | self.evaluate(m(1000.)) |
| 147 | |
| 148 | # restore to the same checkpoint sum object (= 300) |
| 149 | checkpoint.restore(save_path).assert_consumed().run_restore_ops() |
| 150 | self.evaluate(m(300.)) |
| 151 | self.assertEqual(600., self.evaluate(m.result())) |
| 152 | |
| 153 | # restore to a different checkpoint sum object |
| 154 | restore_sum = metrics.Sum() |
| 155 | restore_checkpoint = trackable_utils.Checkpoint(sum=restore_sum) |
| 156 | status = restore_checkpoint.restore(save_path) |
| 157 | restore_update = restore_sum(300.) |
| 158 | status.assert_consumed().run_restore_ops() |
| 159 | self.evaluate(restore_update) |
| 160 | self.assertEqual(600., self.evaluate(restore_sum.result())) |
| 161 | |
| 162 | |
| 163 | @keras_parameterized.run_all_keras_modes |
nothing calls this directly
no test coverage detected