Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
235 views
in Technique[技术] by (71.8m points)

python - Tensorflow causes logging messages to double

So I was playing around with Google's Tensorflow library they published yesterday and encountered an annoying bug that keeps biting me.

What I did was setup the python logging functions as I usually do, and the result was that, if I import the tensorflow library, all messages in the console started doubling. Interestingly, this does not happen if you just use the logging.warn/info/..() function.

An example of a code that does not double the messages:

import tensorflow as tf
import logging

logging.warn('test')

An example of a code that does double all messages:

import tensorflow as tf
import logging

logger = logging.getLogger('TEST')
ch = logging.StreamHandler()
logger.addHandler(ch)

logger.warn('test')

Now, I'm a simple man. I like the functionality of logging, so I use it. The setup with the logger object and the adding of a StreamHandler is something I picked up looking at how other people did this, but it looks like it fits with how the thing was meant to be used. However, I do not have in-depth knowledge of the logging library, as it always just kind of worked.

So, any help explaining why the doubling of the messages occurs will be most helpful.

I am using Ubuntu 14.04.3 LTS with Python 2.7.6, but the error happens in all Python 2.7 versions I tried.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I get this output:

test
WARNING:TEST:test

Tensorflow is also using the logging framework and has set up its own handlers, so when you log, by default, it propagates up to the parent logging handlers inside tensorflow. You can change this behavior by setting:

logger.propagate = False

See also duplicate output in simple python logging configuration

Followup: This was an unintended side-effect of the way tensorflow was using the logging package. I've changed it at HEAD to scope its internal loggers under the name "tensorflow" to avoid this pollution. Should be in the github head within a day or so. In the meantime, the logger.propagate solution will work and won't break once that fix is in, so you should be safe to go. Thanks again for spotting this!

Followup-Followup: Starting with TensorFlow 1.14 exposes the logger directly:

import tensorflow as tf

logger = tf.get_logger()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...