通过调试信息,我指的是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
...

当前回答

可以通过os关闭所有调试日志。环境:

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 
import tensorflow as tf

在tf 0.12和1.0上测试

在细节,

0 = all messages are logged (default behavior)
1 = INFO messages are not printed
2 = INFO and WARNING messages are not printed
3 = INFO, WARNING, and ERROR messages are not printed

其他回答

我用这篇文章解决了无法删除所有警告#27045,解决方案是:

import logging
logging.getLogger('tensorflow').disabled = True

因为TF_CPP_MIN_LOG_LEVEL对我没用,你可以试试:

tf.logging.set_verbosity(tf.logging.WARN)

在tensorflow v1.6.0中为我工作过

在测试了各种建议之后,他们也可以沉默使用PyInstaller构建的可执行文件,我想出了这样的设置:

import logging
import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
logging.getLogger('tensorflow').setLevel(logging.ERROR)

import tensorflow as tf

这条线

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

将关闭重建TensorFlow的警告:

我tensorflow / /平台/ cpu_feature_guard核心。这个TensorFlow二进制文件是用onapi深度神经网络库(oneDNN)优化的,可以在性能关键操作中使用以下CPU指令:AVX2 FMA。 要在其他操作中启用它们,请使用适当的编译器标志重新构建TensorFlow。

这条线

logging.getLogger('tensorflow').setLevel(logging.ERROR)

将取消对AutoGraph的警告:

警告:tensorflow:AutoGraph在此环境中不可用:函数缺少代码信息。这是一些典型的环境,如交互式Python shell。更多信息请参见https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/autograph/g3doc/reference/limitations.md#access-to-source-code。

关键是在导入tensorflow之前放置这两个文件——尽管Pylint警告过!

tensorflow 2.11.0

为了增加一些灵活性,你可以通过编写一个过滤消息的函数来实现对日志级别的更细粒度的控制:

logging.getLogger('tensorflow').addFilter(my_filter_func)

where my_filter_func接受LogRecord对象作为输入[LogRecord docs]和 如果您希望抛出消息,则返回0;非零。

下面是一个示例过滤器,它只保留每n条信息消息(Python 3到期) 在这里使用nonlocal):

def keep_every_nth_info(n):
    i = -1
    def filter_record(record):
        nonlocal i
        i += 1
        return int(record.levelname != 'INFO' or i % n == 0)
    return filter_record

# Example usage for TensorFlow:
logging.getLogger('tensorflow').addFilter(keep_every_nth_info(5))

以上所有都假设TensorFlow已经设置了它的日志记录状态。在添加过滤器之前调用tf.logging.get_verbosity()可以确保没有副作用。

通常的python3日志管理器为我工作tensorflow==1.11.0:

import logging
logging.getLogger('tensorflow').setLevel(logging.INFO)