tensorflow基本结构

编程入门 行业动态 更新时间:2024-10-24 22:27:08

tensorflow基本<a href=https://www.elefans.com/category/jswz/34/1771419.html style=结构"/>

tensorflow基本结构

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 前言
  • 一、TensorFlow结构
  • 二、图
    • 1.图结构
    • 2.TensorBoard
  • 三、会话(Session)
  • 总结


前言

导入模块(要兼容低版本)

import os
import tensorflowpat.v1 as tf   # 兼容低版本
tf.disable_v2_behavior()
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'  # 去警告

一、TensorFlow结构

TensorFlow程序通常被组织成一个构件图阶段和一个执行图阶段。在构建阶段,数据与操作的执行步骤被描述为一个图; 在执行阶段,使用会话执行构建好的图中的操作

术语介绍:

图:TensorFlow将计算表示为指令之间的依赖关系的一种表示方法
会话:TensorFlow跨一个或多个本地或远程设备运行数据流图的机制
张量:TensorFlow中的基本数据对象
节点:提供图当中执行的操作

二、图

1.图结构

图包含了一组tf.Operation代表的计算单元对象和tf.Tensor代表的计算单元之间流动的数据

图一般分为默认图和自定义图

查看默认图:
(1)通过调用tf.get_default_graph()访问,要将操作添加到默认图形中,直接创建OP即可

(2)op、sess都含有graph属性,默认都在一张图中

def graph():a = tf.constant(2)b = tf.constant(3)c = a + bprint("c=",c)# 查看默认图g = tf.get_default_graph()print("g=", g)# 查看属性print("a:", a.graph)print("b:", b.graph)# 开启会话with tf.Session() as sess:c_val = sess.run(c)print("c_val :", c_val)print("sess:", sess.graph)
'''
c= Tensor("add:0", shape=(), dtype=int32)
g= <tensorflow.python.framework.ops.Graph object at 0x000001474F6A03D0>
a: <tensorflow.python.framework.ops.Graph object at 0x000001474F6A03D0>
b: <tensorflow.python.framework.ops.Graph object at 0x000001474F6A03D0>
c_val : 5
sess: <tensorflow.python.framework.ops.Graph object at 0x000001474F6A03D0>'''

可以通过tf.Graph()自定义创建图 , 如果要在这张图中创建OP,典型用法是使用tf.Graph.as_default()上下文管理器

a = tf.constant(2)b = tf.constant(3)c = a + b# 自定义图new_g = tf.Graph()# 定义数据和操作with new_g.as_default():a_new = tf.constant(20)b_new = tf.constant(80)c_new = a_new + b_newprint("a_new:", a_new.graph)print("b_new:", b_new.graph)with tf.Session(graph=new_g) as new_sess:c_val = new_sess.run(c_new)print("c:", c_val)print("new_sess", new_sess.graph)
'''
a_new: <tensorf
low.python.framework.ops.Graph object at 0x0000025737982AF0>
b_new: <tensorflow.python.framework.ops.Graph object at 0x0000025737982AF0>
c: 100
new_sess <tensorflow.python.framework.ops.Graph object at 0x0000025737982AF0>
'''

2.TensorBoard

tensorflow用于训练大规模深度神经网络所需的计算,为了更方便tensorflow程序的理解、调试与优化,tensorflow提供了TensorBoard可视化工具

实现程序可视化过程

(1)实现程序可视化过程

TensorBoard通过读取TensorFlow的事件文件来运行,需要将数据生成一个序列化的Summary protobuf对象

tf.summary.FileWriter(path, graph=sess.graph)

(2)启动TensorBoard

tensorboard --logdir=path
def graph():a = tf.constant(2)b = tf.constant(3)c = a + bprint("c=",c)# 查看默认图g = tf.get_default_graph()print("g=", g)# 查看属性print("a:", a.graph)print("b:", b.graph)# 开启会话with tf.Session() as sess:c_val = sess.run(c)print("c_val :", c_val)print("sess:", sess.graph)# 将图写如本地生成events文件tf.summary.FileWriter("summary", graph=sess.graph)

运行后终端执行tensorboard --logdir=path,出现如图

打开网址即可

三、会话(Session)

Session允许执行图形或部分图形,为此分配资源并保存中间结果和变量的实际值

tf.Session:用于完整的程序当中
tf.InteractiveSession:用于交互式上下文中的TensorFlow,如shell

由上面知道创建一个图形,如下代码

	 # 自定义图new_g = tf.Graph()# 定义数据和操作with new_g.as_default():a_new = tf.constant(20)b_new = tf.constant(80)c_new = a_new + b_newprint("a_new:", a_new.graph)print("b_new:", b_new.graph)

要为该 Graph(图形)创建一个Session(会话),会话还将分配内存来存储变量的当前值,会话掌握资源,用完要回收 - 上下文管理器

建立会话

with tf.Session() as sess:sess.run(sth)

target:如果将此参数留空(默认设置),会话将仅使用本地计算机中的设备。可以指定grpc://网址,以便指定TensorFlow服务器的地址,这使得会话可以访问该服务器控制的计算机上的所有设备
graph:默认情况下,新的tf.Session将绑定到当前的默认图
config:此参数允许您指定一个tf.ConfigProto以便控制会话的行为。例如,ConfigProto协议用于打印设备使用信息

运行会话
通过使用sess.run(0)来运行operation

run(fetches, feed_dict=None, options=None, run_metadata=None)

fetches:单一的operation,或者列表、元组(其他不属于tensorflow的类型不行)
feed_dict:参数运行调用者覆盖图中张量的值,运行时赋值,与tf.placeholder搭配使用,则会检查值的形式是否与占位符兼容

def session():a = tf.constant(6.8, name='a')b = tf.constant(7.9, name='b')c = tf.add(a, b, name='c')print("a:\n", a)print("b:\n", b)print("c:\n", c)print(".........")a1 = tf.placeholder(tf.float32)b1 = tf.placeholder(tf.float32)c1 = tf.add(a1,b1)print("a1:\n", a)print("b1:\n", b)print("c1:\n", c)print(".........")# 查看图default_g = tf.get_default_graph()print("default:",default_g)print("a的属性\n",a.graph)print("b的属性\n", b.graph)print(".........")with tfpat.v1.Session(config=tf.ConfigProto(allow_soft_placement=True,log_device_placement=True)) as sess:# placeholderc_val = sess.run(c1,feed_dict={a1:788.2, b1:82.2})print("c_val:",c_val)abc = sess.run([a1,b1,c1])'''a:Tensor("a:0", shape=(), dtype=float32)
b:Tensor("b:0", shape=(), dtype=float32)
c:Tensor("c:0", shape=(), dtype=float32)
.........
a1:Tensor("a:0", shape=(), dtype=float32)
b1:Tensor("b:0", shape=(), dtype=float32)
c1:Tensor("c:0", shape=(), dtype=float32)
.........
default: <tensorflow.python.framework.ops.Graph object at 0x00000177C189F3D0>
a的属性<tensorflow.python.framework.ops.Graph object at 0x00000177C189F3D0>
b的属性<tensorflow.python.framework.ops.Graph object at 0x00000177C189F3D0>
c: (AddV2): /job:localhost/replica:0/task:0/device:CPU:0
Add: (AddV2): /job:localhost/replica:0/task:0/device:CPU:0
a: (Const): /job:localhost/replica:0/task:0/device:CPU:0
b: (Const): /job:localhost/replica:0/task:0/device:CPU:0
Placeholder: (Placeholder): /job:localhost/replica:0/task:0/device:CPU:0
Placeholder_1: (Placeholder): /job:localhost/replica:0/task:0/device:CPU:0c_val: 870.4
'''

虽然在Pycharm报了许多错,但是最终还是能正确运行,


总结

提示:这里对文章进行总结:

更多推荐

tensorflow基本结构

本文发布于:2024-02-11 19:17:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1682873.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:结构   tensorflow

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!