-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: implement validation losses as a metric
- Loading branch information
Showing
8 changed files
with
382 additions
and
392 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,19 @@ | ||
from n3fit.backends.keras_backend.internal_state import ( | ||
set_initial_state, | ||
clear_backend_state, | ||
set_eager | ||
) | ||
from n3fit.backends.keras_backend import callbacks, constraints, operations | ||
from n3fit.backends.keras_backend.MetaLayer import MetaLayer | ||
from n3fit.backends.keras_backend.MetaModel import MetaModel | ||
from n3fit.backends.keras_backend.base_layers import ( | ||
Concatenate, | ||
Input, | ||
concatenate, | ||
Lambda, | ||
base_layer_selector, | ||
concatenate, | ||
regularizer_selector, | ||
Concatenate, | ||
) | ||
from n3fit.backends.keras_backend import operations | ||
from n3fit.backends.keras_backend import constraints | ||
from n3fit.backends.keras_backend import callbacks | ||
from n3fit.backends.keras_backend.internal_state import ( | ||
clear_backend_state, | ||
set_eager, | ||
set_initial_state, | ||
) | ||
from n3fit.backends.keras_backend.metrics import LossMetric | ||
|
||
print("Using Keras backend") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import tensorflow as tf | ||
from tensorflow.keras.metrics import Metric | ||
|
||
import n3fit.backends.keras_backend.operations as op | ||
|
||
|
||
class LossMetric(Metric): | ||
""" | ||
Implementation of the (validation) loss as a metric. | ||
Keeps track of per replica loss internally, aggregates just for logging. | ||
Parameters | ||
---------- | ||
loss_layer : tf.keras.layers.Layer | ||
The loss layer to use for the metric. | ||
agg : str | ||
Aggregation method to use for the replicas. Can be 'sum' or 'mean'. | ||
""" | ||
|
||
def __init__(self, loss_layer, agg='sum', name='val_loss', **kwargs): | ||
super().__init__(name=name, **kwargs) | ||
self.loss_layer = loss_layer | ||
if agg == 'sum': | ||
self.agg = op.sum | ||
elif agg == 'mean': | ||
self.agg = op.mean | ||
else: | ||
raise ValueError(f'agg must be sum or mean, got {agg}') | ||
num_replicas = loss_layer.output.shape[0] | ||
self.per_replica_losses = self.add_weight( | ||
name="per_replica_losses", shape=(num_replicas,), initializer="zeros" | ||
) | ||
|
||
def update_state(self, y_true, y_pred, sample_weight=None): | ||
self.per_replica_losses.assign(self.loss_layer(y_pred)) | ||
|
||
def result(self): | ||
return self.agg(self.per_replica_losses) | ||
|
||
def reset_state(self): | ||
self.per_replica_losses.assign(tf.zeros_like(self.per_replica_losses)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.