|
1 | | -using Tensorflow.NumPy; |
| 1 | +using Tensorflow.Keras.Utils; |
| 2 | +using Tensorflow.NumPy; |
2 | 3 |
|
3 | 4 | namespace Tensorflow.Keras.Metrics; |
4 | 5 |
|
@@ -36,4 +37,172 @@ public static Tensor sparse_top_k_categorical_matches(Tensor y_true, Tensor y_pr |
36 | 37 |
|
37 | 38 | return matches; |
38 | 39 | } |
| 40 | + |
| 41 | + public static Tensor update_confusion_matrix_variables(Dictionary<string, IVariableV1> variables_to_update, |
| 42 | + Tensor y_true, |
| 43 | + Tensor y_pred, |
| 44 | + Tensor thresholds, |
| 45 | + int top_k, |
| 46 | + int class_id, |
| 47 | + Tensor sample_weight = null, |
| 48 | + bool multi_label = false, |
| 49 | + Tensor label_weights = null, |
| 50 | + bool thresholds_distributed_evenly = false) |
| 51 | + { |
| 52 | + var variable_dtype = variables_to_update.Values.First().dtype; |
| 53 | + y_true = tf.cast(y_true, dtype: variable_dtype); |
| 54 | + y_pred = tf.cast(y_pred, dtype: variable_dtype); |
| 55 | + var num_thresholds = thresholds.shape.dims[0]; |
| 56 | + |
| 57 | + Tensor one_thresh = null; |
| 58 | + if (multi_label) |
| 59 | + { |
| 60 | + one_thresh = tf.equal(tf.cast(constant_op.constant(1), dtype:tf.int32), |
| 61 | + tf.rank(thresholds), |
| 62 | + name: "one_set_of_thresholds_cond"); |
| 63 | + } |
| 64 | + else |
| 65 | + { |
| 66 | + one_thresh = tf.cast(constant_op.constant(true), dtype: dtypes.@bool); |
| 67 | + } |
| 68 | + |
| 69 | + if (sample_weight == null) |
| 70 | + { |
| 71 | + (y_pred, y_true, _) = losses_utils.squeeze_or_expand_dimensions(y_pred, y_true); |
| 72 | + } |
| 73 | + else |
| 74 | + { |
| 75 | + sample_weight = tf.cast(sample_weight, dtype: variable_dtype); |
| 76 | + (y_pred, y_true, sample_weight) = losses_utils.squeeze_or_expand_dimensions(y_pred, |
| 77 | + y_true, |
| 78 | + sample_weight: sample_weight); |
| 79 | + } |
| 80 | + |
| 81 | + if (thresholds_distributed_evenly) |
| 82 | + { |
| 83 | + throw new NotImplementedException(); |
| 84 | + } |
| 85 | + |
| 86 | + var pred_shape = tf.shape(y_pred); |
| 87 | + var num_predictions = pred_shape[0]; |
| 88 | + |
| 89 | + Tensor num_labels; |
| 90 | + if (y_pred.shape.ndim == 1) |
| 91 | + { |
| 92 | + num_labels = constant_op.constant(1); |
| 93 | + } |
| 94 | + else |
| 95 | + { |
| 96 | + num_labels = tf.reduce_prod(pred_shape["1:"], axis: 0); |
| 97 | + } |
| 98 | + var thresh_label_tile = tf.where(one_thresh, num_labels, tf.ones(new int[0], dtype: tf.int32)); |
| 99 | + |
| 100 | + // Reshape predictions and labels, adding a dim for thresholding. |
| 101 | + Tensor predictions_extra_dim, labels_extra_dim; |
| 102 | + if (multi_label) |
| 103 | + { |
| 104 | + predictions_extra_dim = tf.expand_dims(y_pred, 0); |
| 105 | + labels_extra_dim = tf.expand_dims(tf.cast(y_true, dtype: tf.@bool), 0); |
| 106 | + } |
| 107 | + |
| 108 | + else |
| 109 | + { |
| 110 | + // Flatten predictions and labels when not multilabel. |
| 111 | + predictions_extra_dim = tf.reshape(y_pred, (1, -1)); |
| 112 | + labels_extra_dim = tf.reshape(tf.cast(y_true, dtype: tf.@bool), (1, -1)); |
| 113 | + } |
| 114 | + |
| 115 | + // Tile the thresholds for every prediction. |
| 116 | + object[] thresh_pretile_shape, thresh_tiles, data_tiles; |
| 117 | + |
| 118 | + if (multi_label) |
| 119 | + { |
| 120 | + thresh_pretile_shape = new object[] { num_thresholds, 1, -1 }; |
| 121 | + thresh_tiles = new object[] { 1, num_predictions, thresh_label_tile }; |
| 122 | + data_tiles = new object[] { num_thresholds, 1, 1 }; |
| 123 | + } |
| 124 | + else |
| 125 | + { |
| 126 | + thresh_pretile_shape = new object[] { num_thresholds, -1 }; |
| 127 | + thresh_tiles = new object[] { 1, num_predictions * num_labels }; |
| 128 | + data_tiles = new object[] { num_thresholds, 1 }; |
| 129 | + } |
| 130 | + var thresh_tiled = tf.tile(tf.reshape(thresholds, thresh_pretile_shape), tf.stack(thresh_tiles)); |
| 131 | + |
| 132 | + // Tile the predictions for every threshold. |
| 133 | + var preds_tiled = tf.tile(predictions_extra_dim, data_tiles); |
| 134 | + |
| 135 | + // Compare predictions and threshold. |
| 136 | + var pred_is_pos = tf.greater(preds_tiled, thresh_tiled); |
| 137 | + |
| 138 | + // Tile labels by number of thresholds |
| 139 | + var label_is_pos = tf.tile(labels_extra_dim, data_tiles); |
| 140 | + |
| 141 | + Tensor weights_tiled = null; |
| 142 | + |
| 143 | + if (sample_weight != null) |
| 144 | + { |
| 145 | + /*sample_weight = broadcast_weights( |
| 146 | + tf.cast(sample_weight, dtype: variable_dtype), y_pred);*/ |
| 147 | + weights_tiled = tf.tile( |
| 148 | + tf.reshape(sample_weight, thresh_tiles), data_tiles); |
| 149 | + } |
| 150 | + |
| 151 | + if (label_weights != null && !multi_label) |
| 152 | + { |
| 153 | + throw new NotImplementedException(); |
| 154 | + } |
| 155 | + |
| 156 | + Func<Tensor, Tensor, Tensor, IVariableV1, ITensorOrOperation> weighted_assign_add |
| 157 | + = (label, pred, weights, var) => |
| 158 | + { |
| 159 | + var label_and_pred = tf.cast(tf.logical_and(label, pred), dtype: var.dtype); |
| 160 | + if (weights != null) |
| 161 | + { |
| 162 | + label_and_pred *= tf.cast(weights, dtype: var.dtype); |
| 163 | + } |
| 164 | + |
| 165 | + return var.assign_add(tf.reduce_sum(label_and_pred, 1)); |
| 166 | + }; |
| 167 | + |
| 168 | + |
| 169 | + var loop_vars = new Dictionary<string, (Tensor, Tensor)> |
| 170 | + { |
| 171 | + { "tp", (label_is_pos, pred_is_pos) } |
| 172 | + }; |
| 173 | + var update_tn = variables_to_update.ContainsKey("tn"); |
| 174 | + var update_fp = variables_to_update.ContainsKey("fp"); |
| 175 | + var update_fn = variables_to_update.ContainsKey("fn"); |
| 176 | + |
| 177 | + Tensor pred_is_neg = null; |
| 178 | + if (update_fn || update_tn) |
| 179 | + { |
| 180 | + pred_is_neg = tf.logical_not(pred_is_pos); |
| 181 | + loop_vars["fn"] = (label_is_pos, pred_is_neg); |
| 182 | + } |
| 183 | + |
| 184 | + if(update_fp || update_tn) |
| 185 | + { |
| 186 | + var label_is_neg = tf.logical_not(label_is_pos); |
| 187 | + loop_vars["fp"] = (label_is_neg, pred_is_pos); |
| 188 | + if (update_tn) |
| 189 | + { |
| 190 | + loop_vars["tn"] = (label_is_neg, pred_is_neg); |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + var update_ops = new List<ITensorOrOperation>(); |
| 195 | + foreach (var matrix_cond in loop_vars.Keys) |
| 196 | + { |
| 197 | + var (label, pred) = loop_vars[matrix_cond]; |
| 198 | + if (variables_to_update.ContainsKey(matrix_cond)) |
| 199 | + { |
| 200 | + var op = weighted_assign_add(label, pred, weights_tiled, variables_to_update[matrix_cond]); |
| 201 | + update_ops.append(op); |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + tf.group(update_ops.ToArray()); |
| 206 | + return null; |
| 207 | + } |
39 | 208 | } |
0 commit comments