Skip to content

Commit

Permalink
Change loss reduction default to mean
Browse files Browse the repository at this point in the history
  • Loading branch information
frostedoyster committed Dec 16, 2024
1 parent b99b527 commit eb22d61
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ architecture:
loss:
type: mse
weights: {}
reduction: sum
reduction: mean
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ def test_regression_train():
)

# if you need to change the hardcoded values:
# torch.set_printoptions(precision=12)
# print(output["mtt::U0"].block().values)
torch.set_printoptions(precision=12)
print(output["mtt::U0"].block().values)

torch.testing.assert_close(
output["mtt::U0"].block().values,
Expand Down
2 changes: 1 addition & 1 deletion src/metatrain/experimental/nanopet/default-hypers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ architecture:
loss:
type: mse
weights: {}
reduction: sum
reduction: mean
4 changes: 2 additions & 2 deletions src/metatrain/experimental/nanopet/tests/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ def test_regression_train():
)

# if you need to change the hardcoded values:
# torch.set_printoptions(precision=12)
# print(output["mtt::U0"].block().values)
torch.set_printoptions(precision=12)
print(output["mtt::U0"].block().values)

torch.testing.assert_close(
output["mtt::U0"].block().values, expected_output, rtol=1e-5, atol=1e-5
Expand Down
2 changes: 1 addition & 1 deletion src/metatrain/experimental/soap_bpnn/default-hypers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ architecture:
loss:
type: mse
weights: {}
reduction: sum
reduction: mean
4 changes: 2 additions & 2 deletions src/metatrain/experimental/soap_bpnn/tests/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ def test_regression_train():
)

# if you need to change the hardcoded values:
# torch.set_printoptions(precision=12)
# print(output["mtt::U0"].block().values)
torch.set_printoptions(precision=12)
print(output["mtt::U0"].block().values)

torch.testing.assert_close(
output["mtt::U0"].block().values, expected_output, rtol=1e-5, atol=1e-5
Expand Down
4 changes: 2 additions & 2 deletions src/metatrain/utils/loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class TensorMapLoss:

def __init__(
self,
reduction: str = "sum",
reduction: str = "mean",
weight: float = 1.0,
gradient_weights: Optional[Dict[str, float]] = None,
type: Union[str, dict] = "mse",
Expand Down Expand Up @@ -155,7 +155,7 @@ class TensorMapDictLoss:
def __init__(
self,
weights: Dict[str, float],
reduction: str = "sum",
reduction: str = "mean",
type: Union[str, dict] = "mse",
):
outputs = [key for key in weights.keys() if "gradients" not in key]
Expand Down
11 changes: 7 additions & 4 deletions tests/utils/test_loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def tensor_map_with_grad_4():
@pytest.mark.parametrize("type", ["mse", {"huber": {"deltas": {"values": 3.0}}}])
def test_tmap_loss_no_gradients(type):
"""Test that the loss is computed correctly when there are no gradients."""
loss = TensorMapLoss(type=type)
loss = TensorMapLoss(type=type, reduction="sum")

tensor_map_1 = TensorMap(
keys=Labels.single(),
Expand Down Expand Up @@ -138,7 +138,7 @@ def test_tmap_loss_no_gradients(type):
)
def test_tmap_loss_with_gradients(tensor_map_with_grad_1, tensor_map_with_grad_2, type):
"""Test that the loss is computed correctly when there are gradients."""
loss = TensorMapLoss(type=type, gradient_weights={"gradient": 0.5})
loss = TensorMapLoss(type=type, gradient_weights={"gradient": 0.5}, reduction="sum")

loss_value = loss(tensor_map_with_grad_1, tensor_map_with_grad_1)
torch.testing.assert_close(loss_value, torch.tensor(0.0))
Expand Down Expand Up @@ -166,7 +166,8 @@ def test_tmap_dict_loss(
"output_2": 1.0,
"output_1_gradient_gradients": 0.5,
"output_2_gradient_gradients": 0.5,
}
},
reduction="sum",
)
loss_huber = TensorMapDictLoss(
weights={
Expand All @@ -185,6 +186,7 @@ def test_tmap_dict_loss(
}
}
},
reduction="sum",
)

output_dict = {
Expand Down Expand Up @@ -248,7 +250,8 @@ def test_tmap_dict_loss_subset(tensor_map_with_grad_1, tensor_map_with_grad_3):
"output_2": 1.0,
"output_1_gradient_gradients": 0.5,
"output_2_gradient_gradients": 0.5,
}
},
reduction="sum",
)

output_dict = {
Expand Down

0 comments on commit eb22d61

Please sign in to comment.