* Draw a ROC curve. * * @param {tf.Tensor} targets The actual target labels, as a 1D Tensor * object consisting of only 0 and 1 values. * @param {tf.Tensor} probs The probabilities output by a model, as a 1D * Tensor of the same shape as `targets`. It is assumed that the values of * the
(targets, probs, epoch)
| 62 | * @returns {number} Area under the curve (AUC). |
| 63 | */ |
| 64 | function drawROC(targets, probs, epoch) { |
| 65 | return tf.tidy(() => { |
| 66 | const thresholds = [ |
| 67 | 0.0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.55, |
| 68 | 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.92, 0.94, 0.96, 0.98, 1.0 |
| 69 | ]; |
| 70 | const tprs = []; // True positive rates. |
| 71 | const fprs = []; // False positive rates. |
| 72 | let area = 0; |
| 73 | for (let i = 0; i < thresholds.length; ++i) { |
| 74 | const threshold = thresholds[i]; |
| 75 | |
| 76 | const threshPredictions = utils.binarize(probs, threshold).as1D(); |
| 77 | const fpr = falsePositiveRate(targets, threshPredictions).dataSync()[0]; |
| 78 | const tpr = tf.metrics.recall(targets, threshPredictions).dataSync()[0]; |
| 79 | fprs.push(fpr); |
| 80 | tprs.push(tpr); |
| 81 | |
| 82 | // Accumulate to area for AUC calculation. |
| 83 | if (i > 0) { |
| 84 | area += (tprs[i] + tprs[i - 1]) * (fprs[i - 1] - fprs[i]) / 2; |
| 85 | } |
| 86 | } |
| 87 | ui.plotROC(fprs, tprs, epoch); |
| 88 | return area; |
| 89 | }); |
| 90 | } |
| 91 | |
| 92 | // Some hyperparameters for model training. |
| 93 | const epochs = 400; |
no test coverage detected