python - 在 google colab notebook 中看不到 TensorFlow 日志
问题描述
我在 google colab 虚拟笔记本上玩 tensorflow apis。我想查看我的 colab 虚拟机的设备映射。
正如 tensorflow 开发人员指南中所述,我可以设置标志 (log_device_placement=True) 以启用日志记录。https://www.tensorflow.org/guide/using_gpu
下面是我在 colab 笔记本上运行的代码 -
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 笔记本终端控制台一起使用。
知道如何在 google colab 平台上启用日志记录吗?
解决方案
看起来像 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