import numpy as npimport tensorflow as tffrom tensorflow import kerasx = np.random.random((1000, 32))y = np.random.random((1000, 1))class CustomModel(keras.Model): tf.random.set_seed(100) def train_step(self, data): # Unpack the data. Its structure depends on your model and # on what you pass to `fit()`. x, y = data with tf.GradientTape() as tape: y_pred = self(x, training=True) # Forward pass # Compute the loss value # (the loss function is configured in `compile()`) loss = self.compiled_loss(y, y_pred, regularization_losses=self.losses) # Compute gradients trainable_vars = self.trainable_variables gradients = tape.gradient(loss, trainable_vars) # Update weights self.optimizer.apply_gradients(zip(gradients, trainable_vars)) # Update metrics (includes the metric that tracks the loss) self.compiled_metrics.update_state(y, y_pred) # Return a dict mapping metric names to current value return {m.name: m.result() for m in self.metrics} # Construct and compile an instance of CustomModelinputs = keras.Input(shape=(32,))outputs = keras.layers.Dense(1)(inputs)model = CustomModel(inputs, outputs)model.compile(optimizer="adam", loss=tf.losses.MSE, metrics=["mae"])# Just use `fit` as usualmodel.fit(x, y, epochs=1, shuffle=False)32/32 [==============================] - 0s 1ms/step - loss: 0.2783 - mae: 0.4257
loss_tracker = keras.metrics.Mean(name="loss")mae_metric = keras.metrics.MeanAbsoluteError(name="mae")class MyCustomModel(keras.Model): tf.random.set_seed(100) def train_step(self, data): # Unpack the data. Its structure depends on your model and # on what you pass to `fit()`. x, y = data with tf.GradientTape() as tape: y_pred = self(x, training=True) # Forward pass # Compute the loss value # (the loss function is configured in `compile()`) loss = custom_mse(y, y_pred) # loss += self.losses # Compute gradients trainable_vars = self.trainable_variables gradients = tape.gradient(loss, trainable_vars) # Update weights self.optimizer.apply_gradients(zip(gradients, trainable_vars)) # Compute our own metrics loss_tracker.update_state(loss) mae_metric.update_state(y, y_pred) return {"loss": loss_tracker.result(), "mae": mae_metric.result()} @property def metrics(self): # We list our `Metric` objects here so that `reset_states()` can be # called automatically at the start of each epoch # or at the start of `evaluate()`. # If you don't implement this property, you have to call # `reset_states()` yourself at the time of your choosing. return [loss_tracker, mae_metric] # Construct and compile an instance of CustomModelinputs = keras.Input(shape=(32,))outputs = keras.layers.Dense(1)(inputs)my_model_beta = MyCustomModel(inputs, outputs)my_model_beta.compile(optimizer="adam")# Just use `fit` as usualmy_model_beta.fit(x, y, epochs=1, shuffle=False)32/32 [==============================] - 0s 960us/step - loss: 0.2783 - mae: 0.4257