namespace Tensorflow.Keras.Metrics
{
public class MetricsApi
{
public Tensor categorical_accuracy(Tensor y_true, Tensor y_pred)
{
var eql = math_ops.equal(math_ops.argmax(y_true, -1), math_ops.argmax(y_pred, -1));
return math_ops.cast(eql, TF_DataType.TF_FLOAT);
}
///
/// Calculates how often predictions matches integer labels.
///
/// Integer ground truth values.
/// The prediction values.
/// Sparse categorical accuracy values.
public Tensor sparse_categorical_accuracy(Tensor y_true, Tensor y_pred)
{
var y_pred_rank = y_pred.TensorShape.ndim;
var y_true_rank = y_true.TensorShape.ndim;
// If the shape of y_true is (num_samples, 1), squeeze to (num_samples,)
if (y_true_rank != -1 && y_pred_rank != -1
&& y_true.shape.Length == y_pred.shape.Length)
y_true = array_ops.squeeze(y_true, axis: new[] { -1 });
y_pred = math_ops.argmax(y_pred, -1);
// If the predicted output and actual output types don't match, force cast them
// to match.
if (y_pred.dtype != y_true.dtype)
y_pred = math_ops.cast(y_pred, y_true.dtype);
return math_ops.cast(math_ops.equal(y_true, y_pred), TF_DataType.TF_FLOAT);
}
}
}