MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / add_metric_step

Function add_metric_step

tensorflow/python/keras/custom_training_loop_test.py:115–141  ·  view source on GitHub ↗
(defun)

Source from the content-addressed store, hash-verified

113
114
115def add_metric_step(defun):
116 optimizer = keras.optimizer_v2.rmsprop.RMSprop()
117 model = testing_utils.get_model_from_layers([
118 LayerWithMetrics(),
119 keras.layers.Dense(1, kernel_initializer='zeros', activation='softmax')
120 ],
121 input_shape=(10,))
122
123 def train_step(x, y):
124 with backprop.GradientTape() as tape:
125 y_pred_1 = model(x)
126 y_pred_2 = model(2 * x)
127 y_pred = y_pred_1 + y_pred_2
128 loss = keras.losses.mean_squared_error(y, y_pred)
129 gradients = tape.gradient(loss, model.trainable_weights)
130 optimizer.apply_gradients(zip(gradients, model.trainable_weights))
131 assert len(model.metrics) == 2
132 return [m.result() for m in model.metrics]
133
134 if defun:
135 train_step = def_function.function(train_step)
136
137 x, y = array_ops.ones((10, 10)), array_ops.zeros((10, 1))
138 metrics = train_step(x, y)
139 assert np.allclose(metrics[0], 1.5)
140 assert np.allclose(metrics[1], 1.5)
141 return metrics
142
143
144@keras_parameterized.run_with_all_model_types

Callers

nothing calls this directly

Calls 4

LayerWithMetricsClass · 0.85
onesMethod · 0.80
train_stepFunction · 0.70
functionMethod · 0.45

Tested by

no test coverage detected