Skip to main content
 首页 » 编程设计

python之在 google colab notebook 中看不到 Tensorflow 日志

2024年12月31日33yxwkf

我在 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 notebook上好像不行。然而,它正在与本地 jupyter notebook 终端控制台一起工作。

知道如何在 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