-
Notifications
You must be signed in to change notification settings - Fork 15
Feature: Option to set / change area weighting outside of graph-creation #136
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the contributions!, I really like making the spatial scaling explicit. I think it may be a good opportunity to refactor this node_loss_weights
into a new config field under training
. I leave a few things open for discussion below:
- Merge the model.node_loss_weights and the training.loss_scaling.spatial and taking it outside the
training.loss_scaling
totraining.node_loss_weights
??. - I am thinking of something like the following (names to be discussed):
node_loss_weights:
_target_: anemoi.training.losses.scaling.ReweightedGraphNodeAttribute
target_nodes: ${graph.data}
node_attribute: area_weight
scale_by_attribute: "cutout"
weight_frac_of_total: 0.3
- Make it explicit for the global model too, so we have something like:
node_loss_weights:
_target_: anemoi.training.losses.scaling.GraphNodeAttribute
target_nodes: ${graph.data}
node_attribute: area_weight
- Maybe the code fits better in the
losses/
folder, what do you think?
I updated the implementation based on your feedback. The base class behavior is now to load weights from the graph with a fall-back of calculating area-weights on the fly. Similiarly ReweightedGraphNodeAttribute will also attempt to load the weights from the graph and then re-weights. I also changed the re-weighting implementation slightly to be more in line with https://github.com/ecmwf/anemoi-training/blob/feature/limited_area/src/anemoi/training/train/forecaster.py. In aifs-mono we scaled the limited area nodes to be a fraction of the global (unmasked) sum, but since the function is now more generic it makes more sense to use a fraction of the total (masked + unmasked). I didn't actually test if the code works as expected yet, will do that when we are happy with the intended behaviour. |
It's looking very good, I like the overall implementation, and think it will become very useful in the future. |
Fixed the issues addressed in the comments and made sure the weights are loaded correctly in training for the four cases: load from graph (+fallback) and reweight (+fallback). |
Made docstrings and changed the try-except. Should I remove |
If it is not used anywhere else, yes |
Thanks for adding those tests, once we have ATS Approval this can likely be merged. |
Really nice contribution, thanks. |
I added documentation in user-guide.training under loss function scaling. |
This should be finished now. I can see that it is failing some of the pytests, but I think that is because these are being run with an older version of anemoi-graphs. |
PR #159 will bump the minimum required version of |
@havardhhaugen can you rebase |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great contribution! Approved
Desctiption of functionality
Adds functionality to change the area weighting of nodes in the limited area of a stretched grid graph.
Adds two new classes to scaling.py:
Uses scipy.SphericalVoronoi to calculate area weights based on latlon input grid similarly to how this is currently done in anemoi-graphs. Currently this probably won't have many use-cases since the area weights are already calculated like this in graphs, but it serves as a good base class.
Re-calculates area-weights using BaseAreaWeights and then scales the area weights in the limited area (cutout) such that their sum is equal to a configurable fraction of the sum of global area weights.
Adds staticmethod get_node_weights to GraphForecaster which uses the new scaling methods if they are set in the config.
Configuration options
Specified in config.training.loss_scaling.spatial, for instance:
Further comments
Thanks to Magnus Ingstad, Jasper Wijnands and Sophie Buurman and Mario Santa Cruz for their contributions to this PR.
📚 Documentation preview 📚: https://anemoi-training--136.org.readthedocs.build/en/136/