TensorFlow可视化工具TensorBoard默认图与自定义图

作者:_睿智_ 时间:2023-03-03 06:53:22 

一、图

图:数据(张量Tenrsor)+ 操作(节点Operation) (静态)

图可以用:1、默认图;2、自定义图。

1、默认图

查看默认图的方式:

  • 1、调用方法:tf.get_default_graph()

  • 2、查看属性:.graph

1、调用方法查看默认图属性


# 方法一:调用方法
   default = tf.get_default_graph()
   print('default:', default)

TensorFlow可视化工具TensorBoard默认图与自定义图

2、.graph查看图属性


# 方法二:查看属性
   # 查看节点属性
   print('a的属性:', a.graph)
   print('c的属性:', c.graph)
   # 查看会话属性
   print('会话sess的图属性:', sess.graph)

TensorFlow可视化工具TensorBoard默认图与自定义图

TensorFlow可视化工具TensorBoard默认图与自定义图

可以发现这些图的地址都是同一个地址,是因为它们都是默认使用了默认图。

代码


# 查看默认图
def View_Graph():
   # 方法一:调用方法
   default = tf.get_default_graph()
   print('default:', default)  
   # 方法二:查看属性
   # 查看节点属性
   print('a的属性:', a.graph)
   print('c的属性:', c.graph)
   # 查看会话属性
   print('会话sess的图属性:', sess.graph)

2、自定义图(创建图)

1、创建自定义图


# 1 创建自定义图
   new_graph = tf.Graph()
   print(new_graph)

TensorFlow可视化工具TensorBoard默认图与自定义图

2、创建静态图


# 2 创建静态图(张量和节点)
   with new_graph.as_default():
       a = tf.constant(10)
       b = tf.constant(20)
       c = a + b
       print(c)

3、开启会话(运行)


# 3 开启对话(运行)
   with tf.Session(graph=new_graph) as sess:
       print('c=', sess.run(c))

TensorFlow可视化工具TensorBoard默认图与自定义图

4、查看自定义图


# 4 查看自定义图
   View_Graph(a, b, c, sess)

# 查看图
def View_Graph(a, b, c, sess):
   # 方法一:调用方法
   default = tf.get_default_graph()
   print('default:', default)

# 方法二:查看属性
   # 查看节点属性
   print('a的属性:', a.graph)
   print('c的属性:', c.graph)
   # 查看会话属性
   print('会话sess的图属性:', sess.graph)

TensorFlow可视化工具TensorBoard默认图与自定义图

代码


# 自定义图
def Create_myGraph():
   # 1 创建自定义图
   new_graph = tf.Graph()
   print(new_graph)

# 2 创建静态图(张量和节点)
   with new_graph.as_default():
       a = tf.constant(10)
       b = tf.constant(20)
       c = a + b
       print(c)

# 3 开启对话(运行)
   with tf.Session(graph=new_graph) as sess:
       print('c=', sess.run(c))

# 4 查看自定义图
   View_Graph(a, b, c, sess)

二、TensorBoard可视化

1、可视化处理


tf.summary.FileWriter(path, graph=)

# 可视化
       tf.summary.FileWriter("C:\\Users\\Administrator\\Desktop\\summary", graph=sess.graph)            #path                                            图

2、 打开TensorBoard

在cmd中操作:

1、先移到文件夹的前面


cd C://Users//Administrator//Desktop

2、 打开TensorBoard(从文件中获取数据)


tensorboard --logdir=summary

TensorFlow可视化工具TensorBoard默认图与自定义图

3、打开给定的网址

http://localhost:6006/(cmd中给的网址)

得到可视化结果:

TensorFlow可视化工具TensorBoard默认图与自定义图

总代码


import tensorflow as tf
# 创建TensorFlow框架
def Create_Tensorflow():
   # 图(静态)
   a = tf.constant(2)  # 数据1(张量)
   b = tf.constant(6)  # 数据2(张量)
   c = a + b  # 操作(节点)  
   # 会话(执行)
   with tf.Session() as sess:
       print('c=', sess.run(c))
       # 可视化
       tf.summary.FileWriter("C:\\Users\\Administrator\\Desktop\\summary", graph=sess.graph)
   # 查看默认图
   View_Graph(a, b, c, sess)
# 查看图
def View_Graph(a, b, c, sess):
   # 方法一:调用方法
   default = tf.get_default_graph()
   print('default:', default)
   # 方法二:查看属性
   # 查看节点属性
   print('a的属性:', a.graph)
   print('c的属性:', c.graph)
   # 查看会话属性
   print('会话sess的图属性:', sess.graph)
# 自定义图
def Create_myGraph():
   # 1 创建自定义图
   new_graph = tf.Graph()
   print(new_graph)    
   # 2 创建静态图(张量和节点)
   with new_graph.as_default():
       a = tf.constant(10)
       b = tf.constant(20)
       c = a + b
       print(c)  
   # 3 开启对话(运行)
   with tf.Session(graph=new_graph) as sess:
       print('c=', sess.run(c))
   # 4 查看自定义图
   View_Graph(a, b, c, sess)
if __name__ == '__main__':
   # 创建TensorFlow框架
   Create_Tensorflow()
   # 创建自定义图
   Create_myGraph()

来源:https://blog.csdn.net/great_yzl/article/details/120486792

标签:TensorFlow,可视化,TensorBoard,图
0
投稿

猜你喜欢

  • python爬虫---requests库的用法详解

    2022-11-19 10:08:19
  • 如何用ASP实现文章点击数?

    2008-08-11 21:17:00
  • python调用百度语音识别实现大音频文件语音识别功能

    2023-11-29 00:59:53
  • Javascript实现动态菜单添加的实例代码

    2024-04-22 22:23:25
  • Python类继承和多态原理解析

    2023-04-30 17:31:38
  • mysql处理添加外键时提示error 150 问题的解决方法

    2024-01-19 03:40:27
  • golang API请求队列的实现

    2024-03-11 16:39:50
  • golang常用库之操作数据库的orm框架-gorm基本使用详解

    2024-01-28 21:22:19
  • sogou地图API用法实例教程

    2024-04-16 10:30:08
  • 关于多元线性回归分析——Python&SPSS

    2023-03-11 17:03:34
  • 关于document.createDocumentFragment()

    2009-04-05 16:04:00
  • python调用matlab的方法详解

    2023-10-18 06:39:02
  • python缺失值的解决方法总结

    2023-07-28 23:47:38
  • go语言interface接口继承多 态示例及定义 解析

    2023-10-14 02:49:27
  • Pytorch 如何训练网络时调整学习率

    2022-01-31 20:08:48
  • 修改Linux下MySQL 5.0的默认连接数

    2009-09-01 10:16:00
  • 一文带你深入理解Go语言中的sync.Cond

    2024-04-25 15:28:48
  • 网址导航的组织方法

    2008-09-27 12:35:00
  • Python输出由1,2,3,4组成的互不相同且无重复的三位数

    2021-07-26 10:54:42
  • sqlserver对字段的添加修改删除、以及字段的说明

    2024-01-25 23:43:41
  • asp之家 网络编程 m.aspxhome.com