Tensorflow框架介绍

Tensorflow的框架介绍

一张思维导图介绍本文大概内容在这里插入图片描述

· Tensorflow的整体结构以及数据流图
  • 结构分析

一个构建图阶段:定义数据(张量Tensor)和操作(节点OP)
一个执行图阶段:调用各方面资源,将定义好的数据和操作运行起来

  • 图的结构

图就是包含了一组tf.operation代表的计算单元对象和tf.Tensor代表队计算单元之间的流动数据。
简单来说 Tensorflow = Tensor +Flow = 数据 + 操作

  1. 默认图

通过调用 tf.get_default_graph() 访问,将要操作添加到默认图即可。
op、sess均有graph属性,默认在一张图中,我们可以在其后缀加上 .graph 直接访问
列如:sess.graph可以直接查看sess的图属性

  1. 自定义图

利用tf.Graph() 返回一个tensor对象
再通过上下文管理器来创建一个新的图

附上代码:

# coding=utf-8
import tensorflow as tf

def graph():
    a = tf.constant(1)
    b = tf.constant(2)
    c = tf.add(a, b)
    print(c)

# 方法一,查看默认图
    default_g = tf.get_default_graph()
    print("默认图的属性:\n", default_g)
# 方法二,直接查看
    print("a的图属性:\n", a.graph)
    print("b的图属性:\n", b.graph)
    # 开启会话
    with tf.compat.v1.Session() as sess:
        c_new = sess.run(c)
        print("c_new的值:\n", c_new)
        print("c_new的图属性:\n", sess.graph)

    # 自定义图:
    new_graph = tf.Graph()
    with new_graph.as_default():
        a_ng = tf.constant(1)
        b_ng = tf.constant(2)
        c_ng = tf.add(a, b)
        print("c_ng:\n", c_ng)

    return None
    
if __name__ == '__main__':
    graph()

输出:

Tensor("Add:0", shape=(), dtype=int32)
默认图的属性:
 <tensorflow.python.framework.ops.Graph object at 0x000001D9310E9188>
a的图属性:
 <tensorflow.python.framework.ops.Graph object at 0x000001D9310E9188>
b的图属性:
 <tensorflow.python.framework.ops.Graph object at 0x000001D9310E9188>
c_new的值:
 3
c_new的图属性:
 <tensorflow.python.framework.ops.Graph object at 0x000001D9310E9188>
c_ng:
 Tensor("Add_1:0", shape=(), dtype=int32)
· 会话

一个运行得TensorFlow operation类中,会有两种开启方式

  • tf.Session :用于完整的程序当中
  • tf.interactiveSession : 用于交互式上下文中的tensorflow

但是一般我们利用上下文管理器来开启会话。

  • 会话 run() 得方法

利用placeholder提供占位符,run的时候利用 feed_dict 来指定参数。
附上代码:

import tensorflow as tf

def run():
    # 定义占位符
    a = tf.placeholder(tf.float32)
    b = tf.placeholder(tf.float32)
    c = tf.multiply(a, b)
    # 开启上下文管理器
    with tf.compat.v1.Session() as sess:
        c_new = sess.run(c, feed_dict={a: 3.0, b: 7.0})
        print ("c_new:\n", c_new)

    return None

if __name__ == '__main__':
    run()
· 张量的操作
  • 属性与修改属性

shape形状:
修改静态形状:tensor.set_shape()
只有形状没有完全固定下来的情况下,才可以用静态形状来进行修改

# 没有完全固定下来的静态形状
a = tf.placeholder(dtype=tf.float32, shape=[None, None])
# 修改静态形状
a.set_shape([1, 2])

修改动态形状:tf.reshape()
用动态形状创建张量时,张量的个数必须匹配

a = tf.placeholder(dtype=tf.float32, shape=[2, 3])
a_p = tf.reshape(a, [1, 23]# 可以跨阶,但是不能改变张量的总数量2 * 3 = 1 * 2 * 3

代码演示:

dtype类型:
tf.cast(tensor, dtype)来修改张量类型

a = tf.placeholder(dtype=tf.float32, shape=[2, 3])
a_d = tf.cast(a, dtype=int32)
· 变量以及模型的可视化

在定义某些模型参数时,我们利用:

tf.Variable(initial_value = tf.random_normal(shape=[None, None]))

因为这是变量,不需要自己设定值,我们只需要固定它的形状shape即可

  • 在一个简单的线性回归案例中,权重和偏差就是一对简单的变量
weights = tf.Variable(initial_value=tf.random_nomal(shape=[1, 1]))
bias = tf.Variable(initial_value=tf.random_nomal(shape=[1, 1]))
y_predict = tf.matmul(x, weights) + bias

一定要记得变量使用后要显示的初始化变量:

init = tf.global_variables_initializer()
# 还要再会话中运行
sess.run(init)
Tensorflow可视化
  • 先创建事件文件:
tf.summary.FileWriter("path", graph=sess.graph)
  • 利用tensorboard启动事件文件
tensorboard --logdir=path
发布了3 篇原创文章 · 获赞 2 · 访问量 100

猜你喜欢

转载自blog.csdn.net/weixin_45004260/article/details/104458772