jupyter notebook tensorflow打印device信息实例

作者:li_sh1000 时间:2021-02-06 09:34:40 

juypter notebook中直接使用log_device_placement=True打印不出来device信息


# Creates a graph.
with tf.device('/device:CPU:0'):
 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)
# Creates a session with log_device_placement set to True.
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True,allow_soft_placement=True))
# Runs the op.

print(sess.run(c))

需要使用output_partition_graphs来输出device信息


# Creates a graph.
with tf.device('/device:GPU:0'):
 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)
# Creates a session with log_device_placement set to True.
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True,allow_soft_placement=True))

# Runs the op.
options = tf.RunOptions(output_partition_graphs=True)
metadata = tf.RunMetadata()
c_val = sess.run(c, options=options, run_metadata=metadata)

print metadata.partition_graphs

补充知识:Jupyter无法在控制台打印

因为数据有中文,所以我特意在jupter前面设置了

reload(sys)
sys.setdefaultencoding("utf-8")


结果使用print语句的时候无法输入内容。究其原因,是因为reload的时候把sdout变为ipython的对象了,所以要临时储存一下stdout的对象。

不妨试一试以下代码


import sys
stdo = sys.stdout
reload(sys)
sys.setdefaultencoding('utf-8')
sys.stdout= stdo

来源:https://blog.csdn.net/li_sh1000/article/details/81586175

标签:jupyter,notebook,tensorflow,device
0
投稿

猜你喜欢

  • Python脚本利用adb进行手机控制的方法

    2023-01-31 20:49:47
  • 浅谈tensorflow 中tf.concat()的使用

    2023-07-21 20:24:08
  • 详细介绍Python进度条tqdm的使用

    2022-12-13 19:45:08
  • Python continue语句实例用法

    2022-03-30 02:22:58
  • Python实现提取音乐频谱的方法详解

    2022-01-27 07:03:08
  • python获取磁盘号下盘符步骤详解

    2022-10-05 08:32:32
  • Oracle AS关键字 提示错误

    2011-04-18 12:42:00
  • 备战618!用Python脚本帮你实现淘宝秒杀

    2022-05-15 04:43:40
  • 基于JS实现html中placeholder属性提示文字效果示例

    2023-09-07 22:50:58
  • PyTorch实现卷积神经网络的搭建详解

    2021-03-12 16:36:03
  • python 获取字典特定值对应的键的实现

    2022-07-01 19:25:21
  • python 如何做一个识别率百分百的OCR

    2023-08-12 15:16:18
  • JavaScript导出Excel实例详解

    2023-09-06 15:51:27
  • python偏函数partial用法

    2023-09-24 22:25:06
  • django使用图片延时加载引起后台404错误

    2023-11-13 14:51:08
  • Mobile Web下的编码设计

    2010-01-28 10:42:00
  • 详细解读Python中的__init__()方法

    2023-03-25 17:10:27
  • django 修改server端口号的方法

    2022-05-08 15:49:10
  • 服务器端的代码是如何被解释成客户端的?

    2009-11-01 15:15:00
  • Zend Framework教程之资源(Resources)用法实例详解

    2023-11-06 02:49:50
  • asp之家 网络编程 m.aspxhome.com