【问题标题】:Cannot see Tensorflow logs in google colab notebook在 google colab notebook 中看不到 TensorFlow 日志
【发布时间】:2019-04-20 06:08:08
【问题描述】:

我在 google colab 虚拟笔记本上玩 tensorflow api。我想查看我的 colab 虚拟机的设备映射。

正如 tensorflow 开发人员指南中所述,我可以设置标志 (log_device_placement=True) 以启用日志记录。 https://www.tensorflow.org/guide/using_gpu

下面是我在 colab notebook 上运行的代码 -

import tensorflow as tf
# Creates a graph.
a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
c = tf.matmul(a, b)

tf.logging.set_verbosity(tf.logging.INFO)
# Creates a session with log_device_placement set to True.
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
# Runs the op.
print(sess.run(c))

但它似乎不适用于 colab 笔记本。但是它可以与本地 jupyter notebook 终端控制台一起使用。

知道如何在 google colab 平台上启用登录吗?

【问题讨论】:

    标签: python tensorflow google-colaboratory


    【解决方案1】:

    看起来像是 TensorFlow 问题:https://github.com/tensorflow/tensorflow/issues/3047

    或者,一个 jupyter 问题:https://github.com/ipython/ipython/issues/1230

    这是使用第三方库的解决方法:

    !pip install wurlitzer
    
    import tensorflow as tf
    # Creates a graph.
    a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
    b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
    c = tf.matmul(a, b)
    
    tf.logging.set_verbosity(tf.logging.INFO)
    # Creates a session with log_device_placement set to True.
    sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
    
    # Runs the op.
    from wurlitzer import pipes
    
    with pipes() as (out, err):
      print(sess.run(c))
    
    print (out.read())
    

    完整的笔记本: https://colab.research.google.com/drive/1Z5FVCD_z8EMmyd31PsjQffQV_K7dDLfj

    【讨论】:

    • 感谢@bob-smith,wurlitzer 的工作就像一个魅力。我还有一个问题。即使我在 colab 中选择了 TPU 作为运行时,根据日志,TF 会话作业仍然在 CPU 上运行。我应该将什么额外的配置参数传递给 tf session 以使其在 TPU 上运行这些作业?
    • 请针对不同的问题使用不同的问题。谢谢!