def test_unweighted_from_logits(self): cce_obj = metrics.CategoricalCrossentropy(from_logits=True) y_true = np.asarray([[0, 1, 0], [0, 0, 1]]) logits = np.asarray([[1, 9, 0], [1, 8, 1]], dtype=np.float32) result = cce_obj(y_true, logits) assert np.allclose(K.eval(result), 3.5011, atol=1e-3)
def test_unweighted(self): cce_obj = metrics.CategoricalCrossentropy() y_true = np.asarray([[0, 1, 0], [0, 0, 1]]) y_pred = np.asarray([[0.05, 0.95, 0], [0.1, 0.8, 0.1]]) result = cce_obj(y_true, y_pred) assert np.allclose(K.eval(result), 1.176, atol=1e-3)
def test_config(self): cce_obj = metrics.CategoricalCrossentropy( name='cce', dtype='int32', label_smoothing=0.2) assert cce_obj.name == 'cce' assert cce_obj.dtype == 'int32' old_config = cce_obj.get_config() assert np.allclose(old_config['label_smoothing'], 0.2, 1e-3)
def test_label_smoothing(self): y_true = np.asarray([[0, 1, 0], [0, 0, 1]]) logits = np.asarray([[1, 9, 0], [1, 8, 1]], dtype=np.float32) label_smoothing = 0.1 cce_obj = metrics.CategoricalCrossentropy( from_logits=True, label_smoothing=label_smoothing) loss = cce_obj(y_true, logits) assert np.allclose(K.eval(loss), 3.667, atol=1e-3)
def test_weighted_from_logits(self): cce_obj = metrics.CategoricalCrossentropy(from_logits=True) y_true = np.asarray([[0, 1, 0], [0, 0, 1]]) logits = np.asarray([[1, 9, 0], [1, 8, 1]], dtype=np.float32) sample_weight = [1.5, 2.] result = cce_obj(y_true, logits, sample_weight=sample_weight) assert np.allclose(K.eval(result), 4.0012, atol=1e-3)
def test_weighted(self): cce_obj = metrics.CategoricalCrossentropy() y_true = np.asarray([[0, 1, 0], [0, 0, 1]]) y_pred = np.asarray([[0.05, 0.95, 0], [0.1, 0.8, 0.1]]) sample_weight = [1.5, 2.] result = cce_obj(y_true, y_pred, sample_weight=sample_weight) assert np.allclose(K.eval(result), 1.338, atol=1e-3)