Add_metric: Alternative in tensorflow 2.16?

Previously in tensorflow 2.15 I used add_metric to track a latent mean square error in a submodel of a main model. I upgraded to 2.16 and add_metric has been removed.
Is there a nice replacement so that I can easily track the mean square error of latent signals in my model?

That is, I used to something along the lines of:

class LargeModel(tf.keras.Model):
   def __init__(self):
     self.submodel = SubModel()

class SubModel(tf.keras.Model):
 def __init__(self):
    ...

def call(self,x):
      latent_signal = SomeProcessing(subpart_of_x)
     self.add_metric(MSE(latent_signal, subpart_of_x)))
     ...

However, add_metric has been removed so that this does not work anymore.

1 Like

Hi @Cola_Lightyear,
Add_metric is removed from tensorflow 2.16 as by default it contains Keras3.0. And you can try toadd your metric in `Model.compile(metrics=[…]).

Thank you!

1 Like

Hi, thank you for your suggestion. That does not work in my case, because the metric requires certain latent data, which is not accessible if I use it during compilation.

1 Like