def test_penalization_function_shape(): model = MockModel() lp = LocalPenalization(model) lp.update_batches(np.zeros((5, 1)), 1, -0.1) value = lp.evaluate(np.random.rand(10, 1)) assert value.shape == (10, 1)
def test_local_penalization_gradients_with_multiple_points_in_batch(): np.random.seed(123) model = MockModel() lp = LocalPenalization(model) lp.update_batches(np.random.rand(5, 1), 1, -0.1) x0 = np.array([0.5]) _check_grad(lp, TOL, x0)
def test_penalization_function_gradients_shape(): model = MockModel() lp = LocalPenalization(model) lp.update_batches(np.zeros((5, 2)), 1, -0.1) val, grad = lp.evaluate_with_gradients(np.random.rand(10, 2)) assert grad.shape == (10, 2) assert val.shape == (10, 1)
def test_local_penaliztion_at_batch_point(): # Test edge case where evaluating local penalization at a point already in the batch. # This can lead to divide by zero errors if not done correctly. np.random.seed(123) model = MockModel() lp = LocalPenalization(model) x_batch = np.random.rand(5, 1) lp.update_batches(x_batch, 1, -0.1) val, grad = lp.evaluate_with_gradients(x_batch) assert not np.any(np.isnan(grad))
def test_local_penalization_at_batch_point(): # Test edge case where evaluating local penalization at a point already in the batch. # This can lead to divide by zero errors if not done correctly. np.random.seed(123) model = MockModel() lp = LocalPenalization(model) x_batch = np.random.rand(5, 1) lp.update_batches(x_batch, 1, -0.1) val, grad = lp.evaluate_with_gradients(x_batch) assert not np.any(np.isnan(grad))