温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》
  • 首页 > 
  • 教程 > 
  • 开发技术 > 
  • TensorFlow可视化工具中TensorBoard默认图与自定义图的示例分析

TensorFlow可视化工具中TensorBoard默认图与自定义图的示例分析

发布时间:2021-10-18 09:21:16 来源:亿速云 阅读:135 作者:小新 栏目:开发技术

这篇文章主要介绍了TensorFlow可视化工具中TensorBoard默认图与自定义图的示例分析,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

一、图

图:数据(张量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()

感谢你能够认真阅读完这篇文章,希望小编分享的“TensorFlow可视化工具中TensorBoard默认图与自定义图的示例分析”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI