如何在tensorflow中屏蔽Debug信息

在python中执行

import tensorflow as tf
tf.__version__

获取到我的tf版本为1.4.0

我在Tensorflow中文社区学习的时候,在执行下面的基础代码时(我已经修改为在python3.6下可以运行):

import tensorflow as tf
import numpy as np

# 使用 NumPy 生成假数据(phony data), 总共 100 个点.
x_data = np.float32(np.random.rand(2, 100)) # 随机输入
y_data = np.dot([0.100, 0.200], x_data) + 0.300

# 构造一个线性模型
# 
b = tf.Variable(tf.zeros([1]))
W = tf.Variable(tf.random_uniform([1, 2], -1.0, 1.0))
y = tf.matmul(W, x_data) + b

# 最小化方差
loss = tf.reduce_mean(tf.square(y - y_data))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)

# 初始化变量
init = tf.global_variables_initializer()

# 启动图 (graph)
sess = tf.Session();
sess.run(init);

# 拟合平面
for step in range(0, 201):
    sess.run(train)
    if step % 20 == 0:
        print(step, sess.run(W), sess.run(b))

上述代码会产生如下所示的很多Debug信息以及设备查找信息:

2017-11-13 15:16:43.546962: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1061] Found device 0 with properties: 
name: GeForce GTX 1080 Ti major: 6 minor: 1 memoryClockRate(GHz): 1.62
pciBusID: 0000:04:00.0
totalMemory: 10.91GiB freeMemory: 4.21GiB
2017-11-13 15:16:43.714859: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1061] Found device 1 with properties: 
name: GeForce GTX 1080 Ti major: 6 minor: 1 memoryClockRate(GHz): 1.62
pciBusID: 0000:03:00.0
totalMemory: 10.91GiB freeMemory: 3.64GiB
2017-11-13 15:16:43.908690: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1061] Found device 2 with properties: 
name: GeForce GTX 1080 Ti major: 6 minor: 1 memoryClockRate(GHz): 1.62
pciBusID: 0000:02:00.0
totalMemory: 10.91GiB freeMemory: 1.34GiB
2017-11-13 15:16:44.164129: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1061] Found device 3 with properties: 
name: GeForce GTX 1080 Ti major: 6 minor: 1 memoryClockRate(GHz): 1.62
pciBusID: 0000:01:00.0
totalMemory: 10.91GiB freeMemory: 5.61GiB
2017-11-13 15:16:44.168588: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1076] Device peer to peer matrix
2017-11-13 15:16:44.168635: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1082] DMA: 0 1 2 3 
2017-11-13 15:16:44.168642: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1092] 0:   Y Y Y Y 
2017-11-13 15:16:44.168647: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1092] 1:   Y Y Y Y 
2017-11-13 15:16:44.168651: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1092] 2:   Y Y Y Y 
2017-11-13 15:16:44.168655: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1092] 3:   Y Y Y Y 
2017-11-13 15:16:44.168664: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1151] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: GeForce GTX 1080 Ti, pci bus id: 0000:04:00.0, compute capability: 6.1)
2017-11-13 15:16:44.168669: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1151] Creating TensorFlow device (/device:GPU:1) -> (device: 1, name: GeForce GTX 1080 Ti, pci bus id: 0000:03:00.0, compute capability: 6.1)
2017-11-13 15:16:44.168675: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1151] Creating TensorFlow device (/device:GPU:2) -> (device: 2, name: GeForce GTX 1080 Ti, pci bus id: 0000:02:00.0, compute capability: 6.1)
2017-11-13 15:16:44.168679: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1151] Creating TensorFlow device (/device:GPU:3) -> (device: 3, name: GeForce GTX 1080 Ti, pci bus id: 0000:01:00.0, compute capability: 6.1)

为了屏蔽掉上述信息,可以在代码中添加下列代码:

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
#默认为0:输出所有log信息
#设置为1:进一步屏蔽INFO信息
#设置为2:进一步屏蔽WARNING信息
#设置为3:进一步屏蔽ERROR信息

参考来源:

https://stackoverflow.com/questions/35911252/disable-tensorflow-debugging-information

阅读更多
版权声明:本文为博主原创文章,转载请注明出处 https://blog.csdn.net/shuzfan/article/details/78521011
文章标签: python tensorflow
个人分类: TensorFlow
上一篇linux下tensorflow安装
下一篇tensorflow实现单层感知机对MNIST分类
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭