二、TensorFlow结构分析(1)

目录

1、TF数据流图

1.1 TensorFlow结构分析

1.2 案例

2、图与TensorBoard

2.1 图结构

2.2 图相关操作

2.2.1 默认图

2.2.2 创建图

2.3 TensorBoard:可视化学习

2.3.1 数据序列化 - events文件

2.3.2 启动TensorBoard

2.4 OP

2.4.1 常见OP

2.4.2 指令名称


  • TF数据流图
  • 图与TensorBoard
  • 会话
  • 张量
  • 变量OP
  • 高级API

1、TF数据流图

1.1 TensorFlow结构分析

1.2 案例

import tensorflow as tf

def tensorflow_demo():
    # tensorflow基本结构
    # 原生python加法计算
    a = 3
    b = 4
    c = a +b
    print("c:\n",c)

    # tensorflow实现加法计算
    a_t = tf.constant(2)
    b_t = tf.constant(3)
    c_t = a_t + b_t
    print("tensorflow:\n",c_t)

    # 开启会话
    with tf.Session() as sess:
        c_t_value = sess.run(c_t)
        print("c_t_value:\n",c_t_value)
    return None

if __name__ == "__main__":
    # 代码1 :tensorflow基本结构
    tensorflow_demo()

2、图与TensorBoard

2.1 图结构

2.2 图相关操作

2.2.1 默认图

import tensorflow as tf

def graph_demo():
    # 图的演示
    # Tensorflow实现加法运算
    a_t = tf.constant(2)
    b_t = tf.constant(3)
    c_t = a_t + b_t
    print("tensorflow:\n", c_t)
    # 查看默认图
    # 方法1:调用方法
    default_g = tf.get_default_graph()
    print("default:\n",default_g)
    # 方法2:查看属性
    print("a_t的图属性:\n",a_t.graph)
    print("c_t的图属性:\n",c_t.graph)
    # 开启会话
    with tf.Session() as sess:
        c_t_value = sess.run(c_t)
        print("c_t_value:\n", c_t_value)
        print("sess的图属性:\n", sess.graph)
    return None


if __name__ == "__main__":
    # 代码2:图的演示
    graph_demo()

2.2.2 创建图

import tensorflow as tf

def graph_demo():
    # 图的演示
    # Tensorflow实现加法运算
    a_t = tf.constant(2)
    b_t = tf.constant(3)
    c_t = a_t + b_t
    print("tensorflow:\n", c_t)
    
    # 查看默认图
    # 方法1:调用方法
    default_g = tf.get_default_graph()
    print("default:\n",default_g)
    # 方法2:查看属性
    print("a_t的图属性:\n",a_t.graph)
    print("c_t的图属性:\n",c_t.graph)
    # 开启会话
    with tf.Session() as sess:
        c_t_value = sess.run(c_t)
        print("c_t_value:\n", c_t_value)
        print("sess的图属性:\n", sess.graph)

    #  自定义图
    new_g = tf.Graph()
    # 在自己的图中定义数据和操作
    with new_g.as_default():
        a_new = tf.constant(20)
        b_new = tf.constant(30)
        c_new = a_new + b_new
        print("c_new:\n",c_new)
    return None


if __name__ == "__main__":
    # 代码2:图的演示
    graph_demo()
import tensorflow as tf

def graph_demo():
    # 图的演示
    # Tensorflow实现加法运算
    a_t = tf.constant(2)
    b_t = tf.constant(3)
    c_t = a_t + b_t
    print("tensorflow:\n", c_t)
    
    # 查看默认图
    # 方法1:调用方法
    default_g = tf.get_default_graph()
    print("default:\n",default_g)
    # 方法2:查看属性
    print("a_t的图属性:\n",a_t.graph)
    print("c_t的图属性:\n",c_t.graph)

    #  自定义图
    new_g = tf.Graph()
    # 在自己的图中定义数据和操作
    with new_g.as_default():
        a_new = tf.constant(20)
        b_new = tf.constant(30)
        c_new = a_new + b_new
        print("c_new:\n",c_new)
        print("a_new的图属性:\n", a_new.graph)
        print("c_new的图属性:\n", c_new.graph)

    # 开启会话
    with tf.Session() as sess:
        c_t_value = sess.run(c_t)
        print("c_t_value:\n", c_t_value)
        print("sess的图属性:\n", sess.graph)

    # 开启new_g的会话
    with tf.Session(graph = new_g) as new_sess:
        c_new_value = new_sess.run((c_new))
        print("c_new_value:\n",c_new_value)
        print("new_sess的图属性:\n",new_sess.graph)

    return None


if __name__ == "__main__":
    # 代码2:图的演示
    graph_demo()

2.3 TensorBoard:可视化学习

2.3.1 数据序列化 - events文件

2.3.2 启动TensorBoard

2.4 OP

2.4.1 常见OP

2.4.2 指令名称

  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值