ecmwf / anemoi-training

Apache License 2.0
17 stars 17 forks source link

Feature: Option to set / change area weighting outside of graph-creation #136

Open havardhhaugen opened 1 week ago

havardhhaugen commented 1 week ago

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:

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:

training:
  loss_scaling:
    spatial:
      _target_: anemoi.training.data.scaling.StretchedGridCutoutAreaWeights
      target_nodes: ${graph.data}
      cutout_weight_frac_of_global: 0.3     

Further comments

Thanks to Magnus Ingstad, Jasper Wijnands and Sophie Buurman and Mario Santa Cruz for their contributions to this PR.

havardhhaugen commented 4 days ago

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.

HCookie commented 4 days ago

It's looking very good, I like the overall implementation, and think it will become very useful in the future. I've left a few initial comments to start with.

havardhhaugen commented 4 days ago

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).

havardhhaugen commented 3 days ago

Made docstrings and changed the try-except. Should I remove node_loss_weight: area_weight from the model config files as part of this pr?

HCookie commented 3 days ago

If it is not used anywhere else, yes