我使用nosetests对我的Tensorflow代码进行单元测试,但它会产生如此冗长的输出,使其无用。
以下测试
import unittest
import tensorflow as tf
class MyTest(unittest.TestCase):
def test_creation(self):
self.assertEquals(True, False)
在使用nosetests
运行时会产生大量无用的日志记录:
FAIL: test_creation (tests.test_tf.MyTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/cebrian/GIT/thesis-nilm/code/deepmodels/tests/test_tf.py", line 10, in test_creation
self.assertEquals(True, False)
AssertionError: True != False
-------------------- >> begin captured logging << --------------------
tensorflow: Level 1: Registering Const (<function _ConstantShape at 0x7f4379131c80>) in shape functions.
tensorflow: Level 1: Registering Assert (<function no_outputs at 0x7f43791319b0>) in shape functions.
tensorflow: Level 1: Registering Print (<function _PrintGrad at 0x7f4378effd70>) in gradient.
tensorflow: Level 1: Registering Print (<function unchanged_shape at 0x7f4379131320>) in shape functions.
tensorflow: Level 1: Registering HistogramAccumulatorSummary (None) in gradient.
tensorflow: Level 1: Registering HistogramSummary (None) in gradient.
tensorflow: Level 1: Registering ImageSummary (None) in gradient.
tensorflow: Level 1: Registering AudioSummary (None) in gradient.
tensorflow: Level 1: Registering MergeSummary (None) in gradient.
tensorflow: Level 1: Registering ScalarSummary (None) in gradient.
tensorflow: Level 1: Registering ScalarSummary (<function _ScalarShape at 0x7f4378f042a8>) in shape functions.
tensorflow: Level 1: Registering MergeSummary (<function _ScalarShape at 0x7f4378f042a8>) in shape functions.
tensorflow: Level 1: Registering AudioSummary (<function _ScalarShape at 0x7f4378f042a8>) in shape functions.
tensorflow: Level 1: Registering ImageSummary (<function _ScalarShape at 0x7f4378f042a8>) in shape functions.
tensorflow: Level 1: Registering HistogramSummary (<function _ScalarShape at 0x7f4378f042a8>) in shape functions.
tensorflow: Level 1: Registering HistogramAccumulatorSummary (<function _ScalarShape at 0x7f4378f042a8>) in shape functions.
tensorflow: Level 1: Registering Pack (<function _PackShape at 0x7f4378f047d0>) in shape functions.
tensorflow: Level 1: Registering Unpack (<function _UnpackShape at 0x7f4378f048c0>) in shape functions.
tensorflow: Level 1: Registering Concat (<function _ConcatShape at 0x7f4378f04938>) in shape functions.
tensorflow: Level 1: Registering ConcatOffset (<function _ConcatOffsetShape at 0x7f4378f049b0>) in shape functions.
......
而使用来自ipython控制台的tensorflow似乎并不那么详细:
$ ipython
Python 2.7.11+ (default, Apr 17 2016, 14:00:29)
Type "copyright", "credits" or "license" for more information.
IPython 4.2.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: import tensorflow as tf
I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcublas.so locally
I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcudnn.so locally
I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcufft.so locally
I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcuda.so locally
I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcurand.so locally
In [2]:
运行nosetests时如何抑制以前的日志记录?
答案 0 :(得分:25)
1.0更新(5/20/17):
在TensorFlow 1.0中,根据此issue,您现在可以通过名为TF_CPP_MIN_LOG_LEVEL
的环境变量控制日志记录。它默认为0(显示所有日志),但可以设置为1以过滤掉INFO
个日志,2可以设置为另外过滤掉WARNING
个日志,3个过滤掉ERROR
个日志。使用Python查看以下通用OS示例:
import os
import tensorflow as tf
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
有关TensorFlow或TF-Learn Logging的早期版本,请参阅以下内容:
查看下面的页面,了解有关TensorFlow日志记录的信息;使用新的更新,您可以将日志记录详细程度设置为DEBUG
,INFO
,WARN
,ERROR
或FATAL
。例如:
tf.logging.set_verbosity(tf.logging.ERROR)
该页面另外还有可以与TF-Learn模型一起使用的监视器。 Here is the page
此不会阻止所有日志记录(仅限TF-Learn)。我有两个解决方案;一个是技术上正确的&#39;解决方案(Linux)和另一个涉及重建TensorFlow。
script -c 'python [FILENAME].py' | grep -v 'I tensorflow/'
另一方面,请参阅this answer,其中涉及修改源代码并重建TensorFlow。
答案 1 :(得分:9)
使用nosetests --nologcapture
运行测试将禁用这些日志的显示。
有关测试的测试的更多信息:
https://nose.readthedocs.io/en/latest/plugins/logcapture.html
答案 2 :(得分:0)
这是执行此操作的an example。不幸的是,这需要修改源和重建。这是一个tracking bug,可以让它更容易