UNPKG

@tensorflow/tfjs-layers

Version:

TensorFlow layers API in JavaScript

284 lines (283 loc) 8.59 kB
/// <amd-module name="@tensorflow/tfjs-layers/dist/exports_metrics" /> /** * @license * Copyright 2018 Google LLC * * Use of this source code is governed by an MIT-style * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT. * ============================================================================= */ import { Tensor } from '@tensorflow/tfjs-core'; /** * Binary accuracy metric function. * * `yTrue` and `yPred` can have 0-1 values. Example: * ```js * const x = tf.tensor2d([[1, 1, 1, 1], [0, 0, 0, 0]], [2, 4]); * const y = tf.tensor2d([[1, 0, 1, 0], [0, 0, 0, 1]], [2, 4]); * const accuracy = tf.metrics.binaryAccuracy(x, y); * accuracy.print(); * ``` * * `yTrue` and `yPred` can also have floating-number values between 0 and 1, in * which case the values will be thresholded at 0.5 to yield 0-1 values (i.e., * a value >= 0.5 and <= 1.0 is interpreted as 1). * * Example: * ```js * const x = tf.tensor1d([1, 1, 1, 1, 0, 0, 0, 0]); * const y = tf.tensor1d([0.2, 0.4, 0.6, 0.8, 0.2, 0.3, 0.4, 0.7]); * const accuracy = tf.metrics.binaryAccuracy(x, y); * accuracy.print(); * ``` * * @param yTrue Binary Tensor of truth. * @param yPred Binary Tensor of prediction. * @return Accuracy Tensor. * * @doc {heading: 'Metrics', namespace: 'metrics'} */ export declare function binaryAccuracy(yTrue: Tensor, yPred: Tensor): Tensor; /** * Binary crossentropy metric function. * * Example: * ```js * const x = tf.tensor2d([[0], [1], [1], [1]]); * const y = tf.tensor2d([[0], [0], [0.5], [1]]); * const crossentropy = tf.metrics.binaryCrossentropy(x, y); * crossentropy.print(); * ``` * * @param yTrue Binary Tensor of truth. * @param yPred Binary Tensor of prediction, probabilities for the `1` case. * @return Accuracy Tensor. * * @doc {heading: 'Metrics', namespace: 'metrics'} */ export declare function binaryCrossentropy(yTrue: Tensor, yPred: Tensor): Tensor; /** * Sparse categorical accuracy metric function. * * Example: * ```js * * const yTrue = tf.tensor1d([1, 1, 2, 2, 0]); * const yPred = tf.tensor2d( * [[0, 1, 0], [1, 0, 0], [0, 0.4, 0.6], [0, 0.6, 0.4], [0.7, 0.3, 0]]); * const crossentropy = tf.metrics.sparseCategoricalAccuracy(yTrue, yPred); * crossentropy.print(); * ``` * * @param yTrue True labels: indices. * @param yPred Predicted probabilities or logits. * @returns Accuracy tensor. * * @doc {heading: 'Metrics', namespace: 'metrics'} */ export declare function sparseCategoricalAccuracy(yTrue: Tensor, yPred: Tensor): Tensor; /** * Categorical accuracy metric function. * * Example: * ```js * const x = tf.tensor2d([[0, 0, 0, 1], [0, 0, 0, 1]]); * const y = tf.tensor2d([[0.1, 0.8, 0.05, 0.05], [0.1, 0.05, 0.05, 0.8]]); * const accuracy = tf.metrics.categoricalAccuracy(x, y); * accuracy.print(); * ``` * * @param yTrue Binary Tensor of truth: one-hot encoding of categories. * @param yPred Binary Tensor of prediction: probabilities or logits for the * same categories as in `yTrue`. * @return Accuracy Tensor. * * @doc {heading: 'Metrics', namespace: 'metrics'} */ export declare function categoricalAccuracy(yTrue: Tensor, yPred: Tensor): Tensor; /** * Categorical crossentropy between an output tensor and a target tensor. * * @param target A tensor of the same shape as `output`. * @param output A tensor resulting from a softmax (unless `fromLogits` is * `true`, in which case `output` is expected to be the logits). * @param fromLogits Boolean, whether `output` is the result of a softmax, or is * a tensor of logits. * * @doc {heading: 'Metrics', namespace: 'metrics'} */ export declare function categoricalCrossentropy(yTrue: Tensor, yPred: Tensor): Tensor; /** * Computes the precision of the predictions with respect to the labels. * * Example: * ```js * const x = tf.tensor2d( * [ * [0, 0, 0, 1], * [0, 1, 0, 0], * [0, 0, 0, 1], * [1, 0, 0, 0], * [0, 0, 1, 0] * ] * ); * * const y = tf.tensor2d( * [ * [0, 0, 1, 0], * [0, 1, 0, 0], * [0, 0, 0, 1], * [0, 1, 0, 0], * [0, 1, 0, 0] * ] * ); * * const precision = tf.metrics.precision(x, y); * precision.print(); * ``` * * @param yTrue The ground truth values. Expected to contain only 0-1 values. * @param yPred The predicted values. Expected to contain only 0-1 values. * @return Precision Tensor. * * @doc {heading: 'Metrics', namespace: 'metrics'} */ export declare function precision(yTrue: Tensor, yPred: Tensor): Tensor; /** * Computes the recall of the predictions with respect to the labels. * * Example: * ```js * const x = tf.tensor2d( * [ * [0, 0, 0, 1], * [0, 1, 0, 0], * [0, 0, 0, 1], * [1, 0, 0, 0], * [0, 0, 1, 0] * ] * ); * * const y = tf.tensor2d( * [ * [0, 0, 1, 0], * [0, 1, 0, 0], * [0, 0, 0, 1], * [0, 1, 0, 0], * [0, 1, 0, 0] * ] * ); * * const recall = tf.metrics.recall(x, y); * recall.print(); * ``` * * @param yTrue The ground truth values. Expected to contain only 0-1 values. * @param yPred The predicted values. Expected to contain only 0-1 values. * @return Recall Tensor. * * @doc {heading: 'Metrics', namespace: 'metrics'} */ export declare function recall(yTrue: Tensor, yPred: Tensor): Tensor; /** * Loss or metric function: Cosine proximity. * * Mathematically, cosine proximity is defined as: * `-sum(l2Normalize(yTrue) * l2Normalize(yPred))`, * wherein `l2Normalize()` normalizes the L2 norm of the input to 1 and `*` * represents element-wise multiplication. * * ```js * const yTrue = tf.tensor2d([[1, 0], [1, 0]]); * const yPred = tf.tensor2d([[1 / Math.sqrt(2), 1 / Math.sqrt(2)], [0, 1]]); * const proximity = tf.metrics.cosineProximity(yTrue, yPred); * proximity.print(); * ``` * * @param yTrue Truth Tensor. * @param yPred Prediction Tensor. * @return Cosine proximity Tensor. * * @doc {heading: 'Metrics', namespace: 'metrics'} */ export declare function cosineProximity(yTrue: Tensor, yPred: Tensor): Tensor; /** * Loss or metric function: Mean absolute error. * * Mathematically, mean absolute error is defined as: * `mean(abs(yPred - yTrue))`, * wherein the `mean` is applied over feature dimensions. * * ```js * const yTrue = tf.tensor2d([[0, 1], [0, 0], [2, 3]]); * const yPred = tf.tensor2d([[0, 1], [0, 1], [-2, -3]]); * const mse = tf.metrics.meanAbsoluteError(yTrue, yPred); * mse.print(); * ``` * * @param yTrue Truth Tensor. * @param yPred Prediction Tensor. * @return Mean absolute error Tensor. * * @doc {heading: 'Metrics', namespace: 'metrics'} */ export declare function meanAbsoluteError(yTrue: Tensor, yPred: Tensor): Tensor; /** * Loss or metric function: Mean absolute percentage error. * * ```js * const yTrue = tf.tensor2d([[0, 1], [10, 20]]); * const yPred = tf.tensor2d([[0, 1], [11, 24]]); * const mse = tf.metrics.meanAbsolutePercentageError(yTrue, yPred); * mse.print(); * ``` * * Aliases: `tf.metrics.MAPE`, `tf.metrics.mape`. * * @param yTrue Truth Tensor. * @param yPred Prediction Tensor. * @return Mean absolute percentage error Tensor. * * @doc {heading: 'Metrics', namespace: 'metrics'} */ export declare function meanAbsolutePercentageError(yTrue: Tensor, yPred: Tensor): Tensor; export declare function MAPE(yTrue: Tensor, yPred: Tensor): Tensor; export declare function mape(yTrue: Tensor, yPred: Tensor): Tensor; /** * Loss or metric function: Mean squared error. * * ```js * const yTrue = tf.tensor2d([[0, 1], [3, 4]]); * const yPred = tf.tensor2d([[0, 1], [-3, -4]]); * const mse = tf.metrics.meanSquaredError(yTrue, yPred); * mse.print(); * ``` * * Aliases: `tf.metrics.MSE`, `tf.metrics.mse`. * * @param yTrue Truth Tensor. * @param yPred Prediction Tensor. * @return Mean squared error Tensor. * * @doc {heading: 'Metrics', namespace: 'metrics'} */ export declare function meanSquaredError(yTrue: Tensor, yPred: Tensor): Tensor; export declare function MSE(yTrue: Tensor, yPred: Tensor): Tensor; export declare function mse(yTrue: Tensor, yPred: Tensor): Tensor; /** * Computes R2 score. * * ```js * const yTrue = tf.tensor2d([[0, 1], [3, 4]]); * const yPred = tf.tensor2d([[0, 1], [-3, -4]]); * const r2Score = tf.metrics.r2Score(yTrue, yPred); * r2Score.print(); * ``` * @param yTrue Truth Tensor. * @param yPred Prediction Tensor. * @return R2 score Tensor. * * @doc {heading: 'Metrics', namespace: 'metrics'} */ export declare function r2Score(yTrue: Tensor, yPred: Tensor): Tensor;