def test_unweighted(self): bce_obj = metrics.BinaryCrossentropy() y_true = np.asarray([1, 0, 1, 0]).reshape([2, 2]) y_pred = np.asarray([1, 1, 1, 0], dtype=np.float32).reshape([2, 2]) result = bce_obj(y_true, y_pred) assert np.allclose(K.eval(result), 3.833, atol=1e-3)
def test_weighted_from_logits(self): bce_obj = metrics.BinaryCrossentropy(from_logits=True) y_true = [[1, 0, 1], [0, 1, 1]] y_pred = [[100.0, -100.0, 100.0], [100.0, 100.0, -100.0]] sample_weight = [2., 2.5] result = bce_obj(y_true, y_pred, sample_weight=sample_weight) assert np.allclose(K.eval(result), 37.037, atol=1e-3)
def test_weighted(self): bce_obj = metrics.BinaryCrossentropy() y_true = np.asarray([1, 0, 1, 0]).reshape([2, 2]) y_pred = np.asarray([1, 1, 1, 0], dtype=np.float32).reshape([2, 2]) sample_weight = [1.5, 2.] result = bce_obj(y_true, y_pred, sample_weight=sample_weight) assert np.allclose(K.eval(result), 3.285, atol=1e-3)
def test_unweighted_with_logits(self): bce_obj = metrics.BinaryCrossentropy(from_logits=True) y_true = [[1, 0, 1], [0, 1, 1]] y_pred = [[100.0, -100.0, 100.0], [100.0, 100.0, -100.0]] result = bce_obj(y_true, y_pred) assert np.allclose(K.eval(result), 33.333, atol=1e-3)
def test_config(self): bce_obj = metrics.BinaryCrossentropy( name='bce', dtype='int32', label_smoothing=0.2) assert bce_obj.name == 'bce' assert bce_obj.dtype == 'int32' old_config = bce_obj.get_config() assert np.allclose(old_config['label_smoothing'], 0.2, atol=1e-3)
def test_label_smoothing(self): logits = ((100., -100., -100.)) y_true = ((1, 0, 1)) label_smoothing = 0.1 bce_obj = metrics.BinaryCrossentropy( from_logits=True, label_smoothing=label_smoothing) result = bce_obj(y_true, logits) expected_value = (100.0 + 50.0 * label_smoothing) / 3.0 assert np.allclose(expected_value, K.eval(result), atol=1e-3)