def _get_compiled_multi_io_model(self): model = get_multi_io_model() model.compile( optimizer='rmsprop', loss='mse', metrics=[metrics.MeanSquaredError(name='mean_squared_error')], weighted_metrics=[ metrics.MeanSquaredError(name='mean_squared_error_2') ], run_eagerly=testing_utils.should_run_eagerly()) return model
def _get_model(self): x = layers.Dense(3, kernel_initializer='ones', trainable=False) out = layers.Dense( 1, kernel_initializer='ones', name='output', trainable=False) model = testing_utils.get_model_from_layers([x, out], input_shape=(1,)) model.compile( optimizer='rmsprop', loss='mse', metrics=[metrics.MeanSquaredError()], weighted_metrics=[metrics.MeanSquaredError()], run_eagerly=testing_utils.should_run_eagerly()) return model
def test_config(self): mse_obj = metrics.MeanSquaredError(name='my_mse', dtype=dtypes.int32) self.assertEqual(mse_obj.name, 'my_mse') self.assertEqual(mse_obj._dtype, dtypes.int32) # Check save and restore config mse_obj2 = metrics.MeanSquaredError.from_config(mse_obj.get_config()) self.assertEqual(mse_obj2.name, 'my_mse') self.assertEqual(mse_obj2._dtype, dtypes.int32)
def test_weighted(self): mse_obj = metrics.MeanSquaredError() self.evaluate(variables.variables_initializer(mse_obj.variables)) y_true = constant_op.constant(((0, 1, 0, 1, 0), (0, 0, 1, 1, 1), (1, 1, 1, 1, 0), (0, 0, 0, 0, 1))) y_pred = constant_op.constant(((0, 0, 1, 1, 0), (1, 1, 1, 1, 1), (0, 1, 0, 1, 0), (1, 1, 1, 1, 1))) sample_weight = constant_op.constant((1., 1.5, 2., 2.5)) result = mse_obj(y_true, y_pred, sample_weight=sample_weight) self.assertAllClose(0.54285, self.evaluate(result), atol=1e-5)
def _get_multi_io_model(self): inp_1 = layers.Input(shape=(1,), name='input_1') inp_2 = layers.Input(shape=(1,), name='input_2') x = layers.Dense(3, kernel_initializer='ones', trainable=False) out_1 = layers.Dense( 1, kernel_initializer='ones', name='output_1', trainable=False) out_2 = layers.Dense( 1, kernel_initializer='ones', name='output_2', trainable=False) branch_a = [inp_1, x, out_1] branch_b = [inp_2, x, out_2] model = testing_utils.get_multi_io_model(branch_a, branch_b) model.compile( optimizer='rmsprop', loss='mse', metrics=[metrics.MeanSquaredError()], weighted_metrics=[metrics.MeanSquaredError()], run_eagerly=testing_utils.should_run_eagerly()) return model
def test_unweighted(self): mse_obj = metrics.MeanSquaredError() self.evaluate(variables.variables_initializer(mse_obj.variables)) y_true = constant_op.constant(((0, 1, 0, 1, 0), (0, 0, 1, 1, 1), (1, 1, 1, 1, 0), (0, 0, 0, 0, 1))) y_pred = constant_op.constant(((0, 0, 1, 1, 0), (1, 1, 1, 1, 1), (0, 1, 0, 1, 0), (1, 1, 1, 1, 1))) update_op = mse_obj.update_state(y_true, y_pred) self.evaluate(update_op) result = mse_obj.result() self.assertAllClose(0.5, result, atol=1e-5)