Keras
Warnings
Disable
Python
Machine Learning

How to disable keras warnings?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Keras warnings come from more than one place, so there is no single universal off switch. Some messages are normal Python warnings, some come from TensorFlow's logger, and some are low-level backend logs printed before your model code even runs.

Decide What You Actually Want to Silence

Before disabling anything, identify which kind of output you are seeing:

  • Python warnings such as deprecation or user warnings
  • TensorFlow logger messages
  • low-level C++ backend logs from TensorFlow

That distinction matters because each source uses a different control mechanism. If you suppress the wrong layer, the noisy message will keep appearing and it will look like the filter is broken.

Suppress Python Warnings From Keras

For standard Python warnings, use the warnings module. This is the safest place to start because you can target a category or message pattern instead of hiding everything.

python
1import warnings
2
3warnings.filterwarnings("ignore", category=UserWarning, module="keras")
4warnings.filterwarnings("ignore", category=DeprecationWarning, module="keras")

If you really need a broad filter during an experiment, you can ignore all warnings:

python
import warnings

warnings.filterwarnings("ignore")

That is convenient for quick notebooks, but it is too broad for normal development because it can hide messages that would have warned you about real breakage.

Reduce TensorFlow Log Noise

TensorFlow also writes log messages through its own logger. Those are not controlled by the Python warnings module.

python
import tensorflow as tf

tf.get_logger().setLevel("ERROR")

Using "ERROR" keeps only serious log messages. If you still want warnings but not informational output, use "WARNING" instead.

This is useful when Keras model creation triggers extra informational messages that clutter notebook output or CI logs.

Set Backend Log Level Before Importing TensorFlow

Some of the noisiest messages come from the TensorFlow backend and are emitted during import. Those must be controlled with the TF_CPP_MIN_LOG_LEVEL environment variable before TensorFlow is imported.

python
1import os
2
3os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
4
5import tensorflow as tf

The common levels are:

  • '0 for all logs'
  • '1 to hide informational logs'
  • '2 to hide informational and warning logs'
  • '3 to hide informational, warning, and many error logs'

Use 2 carefully, and use 3 only when you are very sure you do not need backend error details.

A Practical Combined Setup

In many projects, a combined setup gives the cleanest result:

python
1import os
2import warnings
3
4os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
5
6warnings.filterwarnings("ignore", category=UserWarning, module="keras")
7
8import tensorflow as tf
9
10tf.get_logger().setLevel("ERROR")

This silences most routine noise while still letting hard failures surface as exceptions.

If you are debugging a model or upgrading TensorFlow, remove these filters temporarily. Keras warnings are often pointing at deprecated APIs, shape mismatches, or behavior changes that you should fix rather than suppress permanently.

Common Pitfalls

  • Setting TF_CPP_MIN_LOG_LEVEL after importing TensorFlow. At that point the import-time backend messages have already been printed.
  • Ignoring all warnings during active development. That can hide useful deprecation or compatibility signals.
  • Assuming Python's warnings module controls TensorFlow logger output. It does not.
  • Suppressing warning text instead of fixing the underlying deprecated or unsupported API call.
  • Using the same notebook kernel after changing environment variables and expecting import-time behavior to change without a restart.

Summary

  • Keras output can come from Python warnings, TensorFlow logging, or low-level backend logs.
  • Use warnings.filterwarnings for Python-side warnings.
  • Use tf.get_logger().setLevel(...) for TensorFlow logger messages.
  • Set TF_CPP_MIN_LOG_LEVEL before importing TensorFlow to reduce backend log noise.
  • Prefer targeted suppression over global silence so important warnings are not lost.

Course illustration
Course illustration

All Rights Reserved.