|
| 1 | +namespace Tensorflow.Keras.Metrics; |
| 2 | + |
| 3 | +public class FBetaScore : Metric |
| 4 | +{ |
| 5 | + int _num_classes; |
| 6 | + string? _average; |
| 7 | + Tensor _beta; |
| 8 | + Tensor _threshold; |
| 9 | + Axis _axis; |
| 10 | + int[] _init_shape; |
| 11 | + |
| 12 | + IVariableV1 true_positives; |
| 13 | + IVariableV1 false_positives; |
| 14 | + IVariableV1 false_negatives; |
| 15 | + IVariableV1 weights_intermediate; |
| 16 | + |
| 17 | + public FBetaScore(int num_classes, |
| 18 | + string? average = null, |
| 19 | + float beta = 0.1f, |
| 20 | + float? threshold = -1f, |
| 21 | + string name = "fbeta_score", |
| 22 | + TF_DataType dtype = TF_DataType.TF_FLOAT) |
| 23 | + : base(name: name, dtype: dtype) |
| 24 | + { |
| 25 | + _num_classes = num_classes; |
| 26 | + _average = average; |
| 27 | + _beta = constant_op.constant(beta); |
| 28 | + _dtype = dtype; |
| 29 | + |
| 30 | + if (threshold.HasValue) |
| 31 | + { |
| 32 | + _threshold = constant_op.constant(threshold); |
| 33 | + } |
| 34 | + |
| 35 | + _init_shape = new int[0]; |
| 36 | + |
| 37 | + if (average != "micro") |
| 38 | + { |
| 39 | + _axis = 0; |
| 40 | + _init_shape = new int[] { num_classes }; |
| 41 | + } |
| 42 | + |
| 43 | + true_positives = add_weight("true_positives", shape: _init_shape, initializer: tf.initializers.zeros_initializer()); |
| 44 | + false_positives = add_weight("false_positives", shape: _init_shape, initializer: tf.initializers.zeros_initializer()); |
| 45 | + false_negatives = add_weight("false_negatives", shape: _init_shape, initializer: tf.initializers.zeros_initializer()); |
| 46 | + weights_intermediate = add_weight("weights_intermediate", shape: _init_shape, initializer: tf.initializers.zeros_initializer()); |
| 47 | + } |
| 48 | + |
| 49 | + public override Tensor update_state(Tensor y_true, Tensor y_pred, Tensor sample_weight = null) |
| 50 | + { |
| 51 | + if (_threshold == null) |
| 52 | + { |
| 53 | + _threshold = tf.reduce_max(y_pred, axis: -1, keepdims: true); |
| 54 | + // make sure [0, 0, 0] doesn't become [1, 1, 1] |
| 55 | + // Use abs(x) > eps, instead of x != 0 to check for zero |
| 56 | + y_pred = tf.logical_and(y_pred >= _threshold, tf.abs(y_pred) > 1e-12); |
| 57 | + } |
| 58 | + else |
| 59 | + { |
| 60 | + y_pred = y_pred > _threshold; |
| 61 | + } |
| 62 | + |
| 63 | + y_true = tf.cast(y_true, _dtype); |
| 64 | + y_pred = tf.cast(y_pred, _dtype); |
| 65 | + |
| 66 | + true_positives.assign_add(_weighted_sum(y_pred * y_true, sample_weight)); |
| 67 | + false_positives.assign_add( |
| 68 | + _weighted_sum(y_pred * (1 - y_true), sample_weight) |
| 69 | + ); |
| 70 | + false_negatives.assign_add( |
| 71 | + _weighted_sum((1 - y_pred) * y_true, sample_weight) |
| 72 | + ); |
| 73 | + weights_intermediate.assign_add(_weighted_sum(y_true, sample_weight)); |
| 74 | + |
| 75 | + return weights_intermediate.AsTensor(); |
| 76 | + } |
| 77 | + |
| 78 | + Tensor _weighted_sum(Tensor val, Tensor? sample_weight = null) |
| 79 | + { |
| 80 | + if (sample_weight != null) |
| 81 | + { |
| 82 | + val = tf.math.multiply(val, tf.expand_dims(sample_weight, 1)); |
| 83 | + } |
| 84 | + |
| 85 | + return tf.reduce_sum(val, axis: _axis); |
| 86 | + } |
| 87 | + |
| 88 | + public override Tensor result() |
| 89 | + { |
| 90 | + var precision = tf.math.divide_no_nan( |
| 91 | + true_positives.AsTensor(), true_positives.AsTensor() + false_positives.AsTensor() |
| 92 | + ); |
| 93 | + var recall = tf.math.divide_no_nan( |
| 94 | + true_positives.AsTensor(), true_positives.AsTensor() + false_negatives.AsTensor() |
| 95 | + ); |
| 96 | + |
| 97 | + var mul_value = precision * recall; |
| 98 | + var add_value = (tf.math.square(_beta) * precision) + recall; |
| 99 | + var mean = tf.math.divide_no_nan(mul_value, add_value); |
| 100 | + var f1_score = mean * (1 + tf.math.square(_beta)); |
| 101 | + |
| 102 | + Tensor weights; |
| 103 | + if (_average == "weighted") |
| 104 | + { |
| 105 | + weights = tf.math.divide_no_nan( |
| 106 | + weights_intermediate.AsTensor(), tf.reduce_sum(weights_intermediate.AsTensor()) |
| 107 | + ); |
| 108 | + f1_score = tf.reduce_sum(f1_score * weights); |
| 109 | + } |
| 110 | + // micro, macro |
| 111 | + else if (_average != null) |
| 112 | + { |
| 113 | + f1_score = tf.reduce_mean(f1_score); |
| 114 | + } |
| 115 | + |
| 116 | + return f1_score; |
| 117 | + } |
| 118 | + |
| 119 | + public override void reset_states() |
| 120 | + { |
| 121 | + var reset_value = np.zeros(_init_shape, dtype: _dtype); |
| 122 | + keras.backend.batch_set_value( |
| 123 | + new List<(IVariableV1, NDArray)> |
| 124 | + { |
| 125 | + (true_positives, reset_value), |
| 126 | + (false_positives, reset_value), |
| 127 | + (false_negatives, reset_value), |
| 128 | + (weights_intermediate, reset_value) |
| 129 | + }); |
| 130 | + } |
| 131 | +} |
0 commit comments