tensorflowでDebug情報をブロックする方法

13114 ワード

pythonで実行
import tensorflow as tf
tf.__version__

私のtfバージョン1.4.0を取得
Tensorflow中国語コミュニティで勉強していたとき、次の基礎コードを実行したとき(python 3.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