在Google Colab中运行TensorFlow测试

时间:2019-01-31 16:16:58

标签: tensorflow google-colaboratory

我想在Google Colab上运行测试以确保可重复性,但是最后我得到系统错误,而我不在本地计算机上。

我通过以下方式在Google Colab中设置了TensorFlow

!pip install tensorflow==1.12.0
import tensorflow as tf
print(tf.__version__)

在安装了几行之后,它会打印:

1.12.0

然后我要运行一个简单的测试:

import tensorflow as tf

class Tests(tf.test.TestCase):

    def test_gpu(self):
        self.assertEqual(False, tf.test.is_gpu_available())

tf.test.main()

该测试在本地计算机上以及在Colab上均通过了测试(以及默认的会话测试),但是此后内核返回了系统错误:

..
----------------------------------------------------------------------
Ran 2 tests in 0.005s

OK
An exception has occurred, use %tb to see the full traceback.

SystemExit: False

在调用%tb之后,我在下面粘贴了很长的堆栈跟踪信息,这几乎没有指示。我该如何解决?

堆栈跟踪为:

SystemExit                                Traceback (most recent call last)
<ipython-input-20-6a87bf6320f2> in <module>()
      7         self.assertEqual(False, tf.test.is_gpu_available())
      8 
----> 9 tf.test.main()
     10 
     11 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/platform/test.py in main(argv)
     62   """Runs all unit tests."""
     63   _test_util.InstallStackTraceHandler()
---> 64   return _googletest.main(argv)
     65 
     66 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/platform/googletest.py in main(argv)
     98       args = sys.argv
     99     return app.run(main=g_main, argv=args)
--> 100   benchmark.benchmarks_main(true_main=main_wrapper)
    101 
    102 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/platform/benchmark.py in benchmarks_main(true_main, argv)
    342     app.run(lambda _: _run_benchmarks(regex), argv=argv)
    343   else:
--> 344     true_main()

/usr/local/lib/python3.6/dist-packages/tensorflow/python/platform/googletest.py in main_wrapper()
     97     if args is None:
     98       args = sys.argv
---> 99     return app.run(main=g_main, argv=args)
    100   benchmark.benchmarks_main(true_main=main_wrapper)
    101 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/platform/app.py in run(main, argv)
    123   # Call the main function, passing through any arguments
    124   # to the final program.
--> 125   _sys.exit(main(argv))
    126 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/platform/googletest.py in g_main(argv)
     68   if ('TEST_TOTAL_SHARDS' not in os.environ or
     69       'TEST_SHARD_INDEX' not in os.environ):
---> 70     return unittest_main(argv=argv)
     71 
     72   total_shards = int(os.environ['TEST_TOTAL_SHARDS'])

/usr/lib/python3.6/unittest/main.py in __init__(self, module, defaultTest, argv, testRunner, testLoader, exit, verbosity, failfast, catchbreak, buffer, warnings, tb_locals)
     93         self.progName = os.path.basename(argv[0])
     94         self.parseArgs(argv)
---> 95         self.runTests()
     96 
     97     def usageExit(self, msg=None):

/usr/lib/python3.6/unittest/main.py in runTests(self)
    256         self.result = testRunner.run(self.test)
    257         if self.exit:
--> 258             sys.exit(not self.result.wasSuccessful())
    259 
    260 main = TestProgram

SystemExit: False

1 个答案:

答案 0 :(得分:1)

您看到的错误来自于试图退出python进程的unittest,Jupyter代表您进行了阻止。您可以通过以下方式避免这种情况:

import tensorflow as tf
class Tests(tf.test.TestCase):
    def test_gpu(self):
        self.assertEqual(False, tf.test.is_gpu_available())
import unittest
unittest.main(argv=['first-arg-is-ignored'], exit=False)

(请注意,最后一行与您的不同,并从enter image description here引出)