Example #1
0
def test_ml_5_classes():
    actuals = tf.constant(
        [
            [1, 0, 0, 0, 0],
            [0, 0, 1, 1, 0],
            [0, 1, 0, 1, 0],
            [0, 1, 1, 0, 0],
            [0, 0, 1, 1, 0],
            [0, 0, 1, 1, 0],
            [1, 0, 0, 0, 1],
            [0, 1, 1, 0, 0],
        ],
        dtype=tf.float32,
    )
    predictions = tf.constant(
        [
            [1, 0.75, 0.2, 0.55, 0],
            [0.65, 0.22, 0.97, 0.88, 0],
            [0, 1, 0, 1, 0],
            [0, 0.85, 0.9, 0.34, 0.5],
            [0.4, 0.65, 0.87, 0, 0.12],
            [0.66, 0.55, 1, 0.98, 0],
            [0.95, 0.34, 0.67, 0.65, 0.10],
            [0.45, 0.97, 0.89, 0.67, 0.46],
        ],
        dtype=tf.float32,
    )
    # Initialize
    hl_obj = HammingLoss("multilabel", threshold=0.7)
    hl_obj.update_state(actuals, predictions)
    # Check results
    check_results(hl_obj, 0.075)
Example #2
0
def test_mc_5_classes():
    actuals = tf.constant(
        [
            [1, 0, 0, 0, 0],
            [0, 0, 0, 1, 0],
            [0, 0, 0, 0, 1],
            [0, 1, 0, 0, 0],
            [0, 0, 1, 0, 0],
            [0, 0, 1, 0, 0],
            [1, 0, 0, 0, 0],
            [0, 1, 0, 0, 0],
        ],
        dtype=tf.float32,
    )

    predictions = tf.constant(
        [
            [0.85, 0, 0.15, 0, 0],
            [0, 0, 0, 1, 0],
            [0, 1, 0, 0, 0],
            [0.05, 0.90, 0.04, 0, 0.01],
            [0.10, 0, 0.81, 0.09, 0],
            [0.10, 0.045, 0, 0.81, 0.045],
            [1, 0, 0, 0, 0],
            [0, 0.85, 0, 0, 0.15],
        ],
        dtype=tf.float32,
    )
    # Initialize
    hl_obj = HammingLoss("multiclass", threshold=0.8)
    hl_obj.update_state(actuals, predictions)
    # Check results
    check_results(hl_obj, 0.25)
Example #3
0
def test_ml_4_classes():
    actuals = tf.constant([[1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 0, 1]], dtype=tf.float32)
    predictions = tf.constant(
        [[0.97, 0.56, 0.83, 0.77], [0.34, 0.95, 0.7, 0.89], [0.95, 0.45, 0.23, 0.56]],
        dtype=tf.float32,
    )
    # Initialize
    hl_obj = HammingLoss("multilabel", threshold=0.8)
    hl_obj.update_state(actuals, predictions)
    # Check results
    check_results(hl_obj, 0.16666667)