|
| 1 | +import math |
| 2 | +import pytest |
| 3 | +from pina import Trainer |
| 4 | +from pina.solver import PINN |
| 5 | +from pina.model import FeedForward |
| 6 | +from pina.loss import LinearWeighting |
| 7 | +from pina.problem.zoo import Poisson2DSquareProblem |
| 8 | + |
| 9 | + |
| 10 | +# Initialize problem and model |
| 11 | +problem = Poisson2DSquareProblem() |
| 12 | +problem.discretise_domain(10) |
| 13 | +model = FeedForward(len(problem.input_variables), len(problem.output_variables)) |
| 14 | + |
| 15 | +# Weights for testing |
| 16 | +init_weight_1 = {cond: 3 for cond in problem.conditions.keys()} |
| 17 | +init_weight_2 = {cond: 4 for cond in problem.conditions.keys()} |
| 18 | +final_weight_1 = {cond: 1 for cond in problem.conditions.keys()} |
| 19 | +final_weight_2 = {cond: 5 for cond in problem.conditions.keys()} |
| 20 | + |
| 21 | + |
| 22 | +@pytest.mark.parametrize("initial_weights", [init_weight_1, init_weight_2]) |
| 23 | +@pytest.mark.parametrize("final_weights", [final_weight_1, final_weight_2]) |
| 24 | +@pytest.mark.parametrize("target_epoch", [5, 10]) |
| 25 | +def test_constructor(initial_weights, final_weights, target_epoch): |
| 26 | + LinearWeighting( |
| 27 | + initial_weights=initial_weights, |
| 28 | + final_weights=final_weights, |
| 29 | + target_epoch=target_epoch, |
| 30 | + ) |
| 31 | + |
| 32 | + # Should fail if initial_weights is not a dictionary |
| 33 | + with pytest.raises(ValueError): |
| 34 | + LinearWeighting( |
| 35 | + initial_weights=[1, 1, 1], |
| 36 | + final_weights=final_weights, |
| 37 | + target_epoch=target_epoch, |
| 38 | + ) |
| 39 | + |
| 40 | + # Should fail if final_weights is not a dictionary |
| 41 | + with pytest.raises(ValueError): |
| 42 | + LinearWeighting( |
| 43 | + initial_weights=initial_weights, |
| 44 | + final_weights=[1, 1, 1], |
| 45 | + target_epoch=target_epoch, |
| 46 | + ) |
| 47 | + |
| 48 | + # Should fail if target_epoch is not an integer |
| 49 | + with pytest.raises(AssertionError): |
| 50 | + LinearWeighting( |
| 51 | + initial_weights=initial_weights, |
| 52 | + final_weights=final_weights, |
| 53 | + target_epoch=1.5, |
| 54 | + ) |
| 55 | + |
| 56 | + # Should fail if target_epoch is not positive |
| 57 | + with pytest.raises(AssertionError): |
| 58 | + LinearWeighting( |
| 59 | + initial_weights=initial_weights, |
| 60 | + final_weights=final_weights, |
| 61 | + target_epoch=0, |
| 62 | + ) |
| 63 | + |
| 64 | + # Should fail if dictionary keys do not match |
| 65 | + with pytest.raises(ValueError): |
| 66 | + LinearWeighting( |
| 67 | + initial_weights={list(initial_weights.keys())[0]: 1}, |
| 68 | + final_weights=final_weights, |
| 69 | + target_epoch=target_epoch, |
| 70 | + ) |
| 71 | + |
| 72 | + |
| 73 | +@pytest.mark.parametrize("initial_weights", [init_weight_1, init_weight_2]) |
| 74 | +@pytest.mark.parametrize("final_weights", [final_weight_1, final_weight_2]) |
| 75 | +@pytest.mark.parametrize("target_epoch", [5, 10]) |
| 76 | +def test_train_aggregation(initial_weights, final_weights, target_epoch): |
| 77 | + weighting = LinearWeighting( |
| 78 | + initial_weights=initial_weights, |
| 79 | + final_weights=final_weights, |
| 80 | + target_epoch=target_epoch, |
| 81 | + ) |
| 82 | + solver = PINN(problem=problem, model=model, weighting=weighting) |
| 83 | + trainer = Trainer(solver=solver, max_epochs=target_epoch, accelerator="cpu") |
| 84 | + trainer.train() |
| 85 | + |
| 86 | + # Check that weights are updated correctly |
| 87 | + assert all( |
| 88 | + math.isclose( |
| 89 | + weighting.last_saved_weights()[cond], |
| 90 | + final_weights[cond], |
| 91 | + rel_tol=1e-5, |
| 92 | + abs_tol=1e-8, |
| 93 | + ) |
| 94 | + for cond in final_weights.keys() |
| 95 | + ) |
0 commit comments