我在Google colab虚拟笔记本上玩tensorflow API。我想查看我的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平台上启用登录功能?
答案 0 :(得分: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