通过调试信息,我指的是TensorFlow在我的终端中显示的关于加载的库和找到的设备等的信息,而不是Python错误。

I tensorflow/stream_executor/dso_loader.cc:105] successfully opened CUDA library libcublas.so locally
I tensorflow/stream_executor/dso_loader.cc:105] successfully opened CUDA library libcudnn.so locally
I tensorflow/stream_executor/dso_loader.cc:105] successfully opened CUDA library libcufft.so locally
I tensorflow/stream_executor/dso_loader.cc:105] successfully opened CUDA library libcuda.so.1 locally
I tensorflow/stream_executor/dso_loader.cc:105] successfully opened CUDA library libcurand.so locally
I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:900] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
I tensorflow/core/common_runtime/gpu/gpu_init.cc:102] Found device 0 with properties: 
name: Graphics Device
major: 5 minor: 2 memoryClockRate (GHz) 1.0885
pciBusID 0000:04:00.0
Total memory: 12.00GiB
Free memory: 11.83GiB
I tensorflow/core/common_runtime/gpu/gpu_init.cc:126] DMA: 0 
I tensorflow/core/common_runtime/gpu/gpu_init.cc:136] 0:   Y 
I tensorflow/core/common_runtime/gpu/gpu_device.cc:717] Creating TensorFlow device (/gpu:0) -> (device: 0, name: Graphics Device, pci bus id: 0000:04:00.0)
I tensorflow/core/common_runtime/gpu/gpu_bfc_allocator.cc:51] Creating bin of max chunk size 1.0KiB
...

当前回答

对于tensorflow 2.1.0,以下代码运行正常。

import tensorflow as tf
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)

其他回答

我使用的是Tensorflow 2.3.1版本,上面的解决方案都没有完全有效。 直到我找到这个包裹。

像这样安装:

水蟒,

python -m pip install silence-tensorflow

ide,

pip install silence-tensorflow

并在第一行代码中添加:

from silence_tensorflow import silence_tensorflow
silence_tensorflow()

就是这样!

2.0更新(10/8/19) 设置TF_CPP_MIN_LOG_LEVEL应该仍然有效(参见下文v0.12+更新),但在2.0版到2.3版有报告的问题。Z固定在2.4及以后。如果设置TF_CPP_MIN_LOG_LEVEL对您不起作用(同样,参见下文),请尝试执行以下操作来设置日志级别:

import tensorflow as tf
tf.get_logger().setLevel('INFO')

另外,请参阅tf.autograph的文档。Set_verbosity设置签名日志消息的详细信息,例如:

# Can also be set using the AUTOGRAPH_VERBOSITY environment variable
tf.autograph.set_verbosity(1)

v0.12+更新(5/20/17),通过TF 2.0+工作:

在TensorFlow 0.12+中,你现在可以通过名为TF_CPP_MIN_LOG_LEVEL的环境变量来控制日志记录;它默认为0(显示所有日志),但可以在Level列下设置为以下值之一。

  Level | Level for Humans | Level Description                  
 -------|------------------|------------------------------------ 
  0     | DEBUG            | [Default] Print all messages       
  1     | INFO             | Filter out INFO messages           
  2     | WARNING          | Filter out INFO & WARNING messages 
  3     | ERROR            | Filter out all messages      

请参阅下面使用Python的通用操作系统示例:

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'  # or any {'0', '1', '2'}
import tensorflow as tf

您可以在运行脚本的环境中设置此环境变量。例如,对于bash,它可以在文件~/中。Bashrc, /etc/environment, /etc/profile,或者在实际的shell中:

TF_CPP_MIN_LOG_LEVEL=2 python my_tf_script.py

为了更彻底,你还可以调用Python tf_logging模块的级别设置,该模块用于例如摘要操作,tensorboard,各种估计器等。

# append to lines above
tf.logging.set_verbosity(tf.logging.ERROR)  # or any {DEBUG, INFO, WARN, ERROR, FATAL}

对于1.14,如果你没有更改为使用v1 API,你会收到如下警告:

# append to lines above
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)  # or any {DEBUG, INFO, WARN, ERROR, FATAL}

**For Prior Versions of TensorFlow or TF-Learn Logging (v0.11.x or lower):**

查看下面的页面,了解TensorFlow日志记录的信息;使用新的更新,您可以将日志详细信息设置为DEBUG, INFO, WARN, ERROR或FATAL。例如:

tf.logging.set_verbosity(tf.logging.ERROR)

该页还介绍了可以与TF-Learn模型一起使用的监视器。这是那一页。

不过,这并不会阻止所有日志记录(只会阻止TF-Learn)。我有两个解;一个是“技术上正确的”解决方案(Linux),另一个涉及重建TensorFlow。

script -c 'python [FILENAME].py' | grep -v 'I tensorflow/'

至于另一个,请参阅这个答案,其中涉及修改源代码和重建TensorFlow。

我也遇到过这个问题(在tensorflow-0.10.0rc0上),但无法通过建议的答案修复过多的鼻子测试日志问题。

我设法通过直接探测张量流记录器来解决这个问题。不是最正确的修复,但工作很好,只污染直接或间接导入tensorflow的测试文件:

# Place this before directly or indirectly importing tensorflow
import logging
logging.getLogger("tensorflow").setLevel(logging.WARNING)

以上的解决方案都不能解决我在Jupyter Notebook中的问题,所以我使用以下来自Cicoria的代码片段,问题解决了。

import warnings  
with warnings.catch_warnings():  
    warnings.filterwarnings("ignore",category=FutureWarning)
    import tensorflow as tf
    from tensorflow import keras
    from tensorflow.keras.preprocessing.text import Tokenizer

print('Done') 

如果你只需要清除屏幕上的警告输出,你可能想在导入tensorflow后立即使用这个简单的命令清除控制台屏幕(根据我的经验,它比禁用所有调试日志更有效):

在windows中:

import os
os.system('cls')

在Linux或Mac中:

import os
os.system('clear')