Buckets:
| import unittest | |
| import numpy as np | |
| from real2sim.mask_loss import MaskLoss | |
| from real2sim.optimization import search | |
| class OptimizationTests(unittest.TestCase): | |
| def test_cem_improves_bounded_problem_and_respects_budget(self): | |
| target = np.array([0.7, -0.4]) | |
| result = search( | |
| lambda values: float(np.sum((values - target) ** 2)), | |
| [0, 0], | |
| [0.5, 0.5], | |
| [[-1, 1], [-1, 1]], | |
| population=32, | |
| iterations=10, | |
| ) | |
| self.assertLess(result.loss, 0.001) | |
| self.assertEqual(result.evaluations, 321) | |
| self.assertTrue((np.diff(result.history) <= 0).all()) | |
| self.assertTrue((np.abs(result.parameters) <= 1).all()) | |
| def test_random_and_cem_have_equal_evaluation_budgets(self): | |
| calls = [] | |
| for method in ("cem", "random"): | |
| count = [] | |
| def objective(values, current_count=count): | |
| current_count.append(values.copy()) | |
| return float(np.dot(values, values)) | |
| result = search( | |
| objective, | |
| [0], | |
| [1], | |
| [[-2, 2]], | |
| population=4, | |
| iterations=3, | |
| method=method, | |
| ) | |
| self.assertEqual(result.evaluations, len(count)) | |
| calls.append(len(count)) | |
| self.assertEqual(calls, [13, 13]) | |
| def test_mask_loss_prefers_nearby_disjoint_masks(self): | |
| target = np.zeros((1, 1, 40, 40), bool) | |
| target[0, 0, 10:15, 10:15] = True | |
| near = np.roll(target, 7, axis=3) | |
| far = np.roll(target, 20, axis=3) | |
| loss = MaskLoss(target, [[1]]) | |
| self.assertEqual(loss(target), 0) | |
| self.assertLess(loss(near), loss(far)) | |
| self.assertGreater(loss(np.zeros_like(target)), 1) | |
| def test_missing_observations_do_not_penalize_predictions(self): | |
| target = np.zeros((1, 2, 10, 10), bool) | |
| target[0, 0, 2:5, 2:5] = True | |
| prediction = target.copy() | |
| prediction[0, 1] = True | |
| self.assertEqual(MaskLoss(target, [[1, 0]])(prediction), 0) | |
| if __name__ == "__main__": | |
| unittest.main() | |
Xet Storage Details
- Size:
- 2.17 kB
- Xet hash:
- bfbccddd30a037aa45985d881c34cacf5aefb15578ebb6581b88b053bd9f3a17
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.