7

"Using XXX backend" would print when I import keras.

import keras

Outputs:

Using TensorFlow backend.

But I clearly know what I am using.

How to disable it?

2
  • which version of tensorflow, keras,dask are you using? Jul 21, 2018 at 14:14
  • I am using tensorflow 1.8.0, keras 2.1.6, dask 0.18.1.
    – fmnijk
    Jul 21, 2018 at 17:49

7 Answers 7

9

Just using the code below.

import os
import sys
stderr = sys.stderr
sys.stderr = open(os.devnull, 'w')
import keras
sys.stderr = stderr
4

workaround for this problem:

stderr = sys.stderr
sys.stderr = open(os.devnull, 'w')
import keras
sys.stderr = stderr

You can find more info in keras issues here

1

If you are using Tensorflow 2.0, then you might get an error saying AttributeError: module 'tf' has no attribute 'logging'

Unfortunately tf.logging has been removed from tensorflow 2.0. Please execute the below commands:

import logging logger = tf.get_logger() logger.setLevel(logging.ERROR)

0

You have two options: First comment in here keras/backend/__init__.py the string print('Using TensorFlow backend.'), but of course this option is not suggested since you are going to edit code in the keras module. The second option is:

import sys
stdout = sys.stdout
sys.stdout = open('/dev/null', 'w')
import keras
sys.stdout = stdout

It's not elegant, but it works.

2
  • 1
    I found this needed to be using stderr, as in other answers, not stdout Feb 1, 2019 at 9:45
  • 1
    stderr should be used not stdout .. otherwise all outputs will be redirected to null Feb 27, 2019 at 6:08
0

First comment in here "keras/backend/init.py" the string print('Using TensorFlow backend.') it will work!!.. but it is not a permanent solution.

0
0

Another way to hide keras printing "using Tensorflow backend"

Suggested here https://github.com/keras-team/keras/issues/1406

import ...

import os
from contextlib import redirect_stderr
with redirect_stderr(open(os.devnull, "w")):
    import keras

import ...
import ... 
-1

Include this line in your code. It will ignore the error.

tf.logging.set_verbosity(tf.logging.ERROR)
3
  • Unfortunately this is what happens. AttributeError: module 'tf' has no attribute 'logging'
    – RKO
    Aug 21, 2019 at 18:05
  • @RKO Unfortunately tf.logging has been removed from tensorflow 2.0. Please execute the below commands: import logging logger = tf.get_logger() logger.setLevel(logging.ERROR)
    – Ankit Jain
    Aug 23, 2019 at 4:33
  • The 'Using XXX backend' is not logged from Tensorflow but from Keras, so updating TF log levels won't help.
    – Lefty
    Jun 5, 2020 at 10:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.