Evaluation Metrics in TensorFlow
Last Updated :
12 Feb, 2025
Evaluation metrics accesses the performance of machine learning models.
In TensorFlow, these metrics help quantify how well the model is performing during training and after it has been trained. TensorFlow provides a wide variety of built-in metrics for both classification and regression tasks, allowing you to choose the most appropriate one for your specific problem.
1. Accuracy
Accuracy is one of the most widely used evaluation metrics, particularly in classification problems. It measures the percentage of correct predictions out of all predictions made. It’s suitable for balanced datasets but may not be the best choice for imbalanced datasets, as it can give misleading results.
Function: tf.keras.metrics.Accuracy()
TensorFlow Code:
Python
import tensorflow as tf
# Example model
model = tf.keras.Sequential([
tf.keras.layers.Dense(64, activation='relu', input_shape=(32,)),
tf.keras.layers.Dense(10, activation='softmax')
])
# Compile model with Accuracy metric
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=[tf.keras.metrics.Accuracy()])
# Train the model
model.fit(x_train, y_train)
2. Precision
Precision is a metric used in classification tasks that measures how many of the predicted positive labels were actually positive. It’s particularly useful when the cost of false positives is high.
Function: tf.keras.metrics.Precision()
Example Code in TensorFlow:
Python
import tensorflow as tf
model = tf.keras.Sequential([
tf.keras.layers.Dense(64, activation='relu', input_shape=(32,)),
tf.keras.layers.Dense(1, activation='sigmoid') # For binary classification
])
# Compile model with Precision metric
model.compile(optimizer='adam',
loss='binary_crossentropy',
metrics=[tf.keras.metrics.Precision()])
model.fit(x_train, y_train)
3. Recall
Recall is another important metric in classification, especially in situations where false negatives are more costly than false positives. It measures how many of the actual positive labels were correctly identified by the model.
Function: tf.keras.metrics.Recall()
Example Code in TensorFlow:
Python
import tensorflow as tf
# Example model
model = tf.keras.Sequential([
tf.keras.layers.Dense(64, activation='relu', input_shape=(32,)),
tf.keras.layers.Dense(1, activation='sigmoid') # For binary classification
])
# Compile model with Recall metric
model.compile(optimizer='adam',
loss='binary_crossentropy',
metrics=[tf.keras.metrics.Recall()])
# Train the model
model.fit(x_train, y_train)
4. F1 Score
F1 Score is the harmonic mean of precision and recall, providing a balanced evaluation metric for classification tasks. It is particularly useful when you need to balance both false positives and false negatives.
Function: TensorFlow doesn’t have a built-in F1 score metric, but you can compute it using precision and recall.
5. Mean Squared Error (MSE)
MSE is commonly used for regression tasks. It measures the average squared difference between the predicted and actual values. A lower MSE indicates better model performance.
Function: tf.keras.metrics.MeanSquaredError()
Example Code in TensorFlow:
Python
import tensorflow as tf
model = tf.keras.Sequential([
tf.keras.layers.Dense(64, activation='relu', input_shape=(32,)),
tf.keras.layers.Dense(1)
])
# Compile model with Mean Squared Error metric
model.compile(optimizer='adam',
loss='mean_squared_error',
metrics=[tf.keras.metrics.MeanSquaredError()])
model.fit(x_train, y_train)
6. Mean Absolute Error (MAE)
Mean Absolute Error is another metric commonly used in regression. It measures the average absolute differences between the predicted and actual values. Unlike MSE, MAE doesn’t penalize large errors as much, making it more robust to outliers.
Function: tf.keras.metrics.MeanAbsoluteError()
Example Code in TensorFlow:
Python
import tensorflow as tf
model = tf.keras.Sequential([
tf.keras.layers.Dense(64, activation='relu', input_shape=(32,)),
tf.keras.layers.Dense(1) # For regression
])
# Compile model with Mean Absolute Error metric
model.compile(optimizer='adam',
loss='mean_absolute_error',
metrics=[tf.keras.metrics.MeanAbsoluteError()])
model.fit(x_train, y_train)
7. AUC (Area Under the Curve)
AUC measures the area under the receiver operating characteristic (ROC) curve. It is a valuable metric for binary classification tasks, indicating the model’s ability to distinguish between classes. A higher AUC value indicates a better performing model.
Function: tf.keras.metrics.AUC()
8. Cosine Similarity
Cosine similarity measures the cosine of the angle between the predicted and actual vectors. It is commonly used in text-related tasks, such as document similarity, or in cases where the direction of the vector is more important than its magnitude.
Function: tf.keras.metrics.CosineSimilarity()
Example Code in TensorFlow:
Python
import tensorflow as tf
model = tf.keras.Sequential([
tf.keras.layers.Dense(64, activation='relu', input_shape=(32,)),
tf.keras.layers.Dense(1, activation='sigmoid') # For binary classification
])
# Compile model with Cosine Similarity metric
model.compile(optimizer='adam',
loss='binary_crossentropy',
metrics=[tf.keras.metrics.CosineSimilarity()])
model.fit(x_train, y_train)
Evaluation metrics are vital tools for assessing the performance of your machine learning models. TensorFlow provides a comprehensive suite of built-in metrics that cater to both classification and regression tasks.
By understanding and using the appropriate metrics in TensorFlow, you can better tune your models and achieve optimal performance.
Similar Reads
model.evaluate() in TensorFlow
The model.evaluate() function in TensorFlow is used to evaluate a trained model on a given dataset. It returns the loss value and any additional metrics specified during model compilation. model.evaluate() function allows us to assess how well the trained model generalizes to unseen data.Syntax of m
2 min read
tf.function in TensorFlow
TensorFlow is a machine learning framework that has offered flexibility, scalability and performance for deep learning tasks. tf.function helps to optimize and accelerate computation by leveraging graph-based execution. In the article, we will cover the concept of tf.function in TensorFlow. Table of
5 min read
Debugging in TensorFlow
This article discusses the basics of TensorFlow and also dives deep into debugging in TensorFlow in Python. We will see debugging techniques, and debugging tools, and also get to know about common TensorFlow errors. TensorFlow TensorFlow is an open-source library that helps develop, deploy, and trai
8 min read
Numerical Operations in TensorFlow
TensorFlow is an open-source machine-learning library developed by Google. TensorFlow is used to build and train deep learning models as it facilitates the creation of computational graphs and efficient execution on various hardware platforms. Here, we will learn some of the basic Numerical operatio
5 min read
tensorflow.math.atan2() function in Python
TensorFlow is open-source python library designed by Google to develop Machine Learning models and deep learning  neural networks. atan2() is used to find element wise arctangent of y/x. Syntax: tf.math.atan2(y, x, name) Parameters: y: It's the input tensor. Allowed dtype for this tensor are bfloat
2 min read
tf.Module in Tensorflow Example
TensorFlow is an open-source library for data science. It provides various tools and APIs. One of the core components of TensorFlow is tf.Module, a class that represents a reusable piece of computation. A tf.Module is an object that encapsulates a set of variables and functions that operate on them.
6 min read
Tensor Indexing in Tensorflow
In the realm of machine learning and deep learning, tensors are fundamental data structures used to represent numerical data with multiple dimensions. TensorFlow, a powerful numerical computation library, equips you with an intuitive and versatile set of operations for manipulating and accessing dat
10 min read
Tensorflow.js tf.eye() Function
Tensorflow.js is an open-source library for creating machine learning models in Javascript that allows users to run the models directly in the browser. The tf.eye() is a function defined in the class tf.Tensor. Itâs used to create an identity matrix of specified rows and columns. An identity matrix
3 min read
Python - tensorflow.math.not_equal()
TensorFlow is open-source Python library designed by Google to develop Machine Learning models and deep learning  neural networks. not_equal() is used to find element wise truth value of x!=y. It supports broadcasting Syntax: tensorflow.math.not_equal( x, y, name) Parameters: x: It is a tensor. All
2 min read
Jacobians in TensorFlow
In the field of machine learning and numerical computation, it is extremely important to understand mathematical concepts. One of such fundamental concepts is Jacobian matrix. It is important part of calculus and has extensive applications in diverse fields. In this article, we will discuss Jacobian
6 min read