当前位置: 首页 > news >正文

深度学习基础之《TensorFlow框架(2)—图》

一、什么是图结构

1、图包含了一组tf.Operation代表的计算单元对象和tf.Tensor代表的计算单元之间流动的数据
图结构:数据(Tensor) + 操作(Operation)

二、图相关操作

1、默认图
通常TensorFlow会默认帮我们创建一张图

查看默认图的两种方法:
(1)通过调用tf.compat.v1.get_default_graph()访问,要将操作添加到默认图形中,直接创建OP即可
(2)op、sess都含有graph属性,默认都在一张图中
注:2.x版本(使用默认图)不支持调用属性,会报错“AttributeError: Tensor.graph is meaningless when eager execution is enabled.”

import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
import tensorflow as tfdef tensorflow_demo():"""TensorFlow的基本结构"""# TensorFlow实现加减法运算a_t = tf.constant(2)b_t = tf.constant(3)c_t = a_t + b_tprint("TensorFlow加法运算结果:\n", c_t)print(c_t.numpy())# 2.0版本不需要开启会话,已经没有会话模块了return Nonedef graph_demo():"""图的演示"""# TensorFlow实现加减法运算a_t = tf.constant(2)b_t = tf.constant(3)c_t = a_t + b_tprint("TensorFlow加法运算结果:\n", c_t)print(c_t.numpy())# 查看默认图# 方法1:调用方法default_g = tf.compat.v1.get_default_graph()print("default_g:\n", default_g)# 方法2:查看属性# print("a_t的图属性:\n", a_t.graph)# print("c_t的图属性:\n", c_t.graph)return Noneif __name__ == "__main__":# 代码1:TensorFlow的基本结构# tensorflow_demo()# 代码2:图的演示graph_demo()
python3 day01_deeplearning.pyTensorFlow加法运算结果:tf.Tensor(5, shape=(), dtype=int32)
5
default_g:<tensorflow.python.framework.ops.Graph object at 0x7f27651b5be0>

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

import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
import tensorflow as tfdef tensorflow_demo():"""TensorFlow的基本结构"""# TensorFlow实现加减法运算a_t = tf.constant(2)b_t = tf.constant(3)c_t = a_t + b_tprint("TensorFlow加法运算结果:\n", c_t)print(c_t.numpy())# 2.0版本不需要开启会话,已经没有会话模块了return Nonedef graph_demo():"""图的演示"""# TensorFlow实现加减法运算a_t = tf.constant(2)b_t = tf.constant(3)c_t = a_t + b_tprint("TensorFlow加法运算结果:\n", c_t)print(c_t.numpy())# 查看默认图# 方法1:调用方法default_g = tf.compat.v1.get_default_graph()print("default_g:\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_newprint("c_new:\n", c_new)print("a_new的图属性:\n", a_new.graph)print("b_new的图属性:\n", b_new.graph)# 开启new_g的会话with tf.compat.v1.Session(graph=new_g) as sess:c_new_value = sess.run(c_new)print("c_new_value:\n", c_new_value)print("我们自己创建的图为:\n", sess.graph)return Noneif __name__ == "__main__":# 代码1:TensorFlow的基本结构# tensorflow_demo()# 代码2:图的演示graph_demo()
python3 day01_deeplearning.pyTensorFlow加法运算结果:tf.Tensor(5, shape=(), dtype=int32)
5
default_g:<tensorflow.python.framework.ops.Graph object at 0x7f19806c4d68>
c_new:Tensor("add:0", shape=(), dtype=int32)
a_new的图属性:<tensorflow.python.framework.ops.Graph object at 0x7f19809f5748>
b_new的图属性:<tensorflow.python.framework.ops.Graph object at 0x7f19809f5748>
c_new_value:50
我们自己创建的图为:<tensorflow.python.framework.ops.Graph object at 0x7f19809f5748>

说明:
(1)默认图执行结果是tf.Tensor(5, shape=(), dtype=int32)
(2)自定义图执行结果是Tensor("add:0", shape=(), dtype=int32)
(3)自定义图没有即时执行,需要开启Session指定图来执行
(4)可以看到默认图地址为0x7f19806c4d68,自定义图地址为0x7f19809f5748
 

http://www.lryc.cn/news/302856.html

相关文章:

  • Web3区块链游戏:创造虚拟世界的全新体验
  • 单机启动/开机启动SpringBoot服务的正确方式
  • [C#]winform基于opencvsharp结合CSRNet算法实现低光图像增强黑暗图片变亮变清晰
  • 抓包分析 TCP 协议
  • 代码随想录算法训练营day27 | 93.复原IP地址、78.子集、90.子集II
  • RuntimeError: CUDA out of memory.【多种场景下的解决方案】
  • LeetCode刷题| Leetcode 45. 跳跃游戏,1190. 反转每对括号间的子串,781. 森林中的兔子,739. 每日温度
  • Redis(03)——发布订阅
  • ⭐北邮复试刷题LCR 034. 验证外星语词典__哈希思想 (力扣119经典题变种挑战)
  • ECMAScript 6+ 新特性 ( 二 )
  • JS游戏项目合集【附源码】
  • React中hooks使用限制及保存函数组件状态
  • 用git命令来上传项目到GitHub我自己的仓库
  • .NET有哪些微服务框架
  • uniapp中打开蓝牙需要哪些权限
  • virtualbox虚拟机运行中断,启动报错“获取 VirtualBox COM 对象失败”
  • 【JVM篇】什么是运行时数据区
  • Jetpack 之Glance+Compose实现一个小组件
  • 实时矢量搜索如何彻底改变各行各业?
  • 【Linux】指令 【scp】
  • 文件IO,目录IO的学习
  • leetcode(动态规划)53.最大子数组和(C++详细解释)DAY12
  • BUGKU-WEB bp
  • 代码的复用——Mixin使用例子
  • easyx 枪声模拟器
  • python 与 neo4j 交互(py2neo 使用)
  • Python基础笔记11
  • vulhub中Apache Log4j2 lookup JNDI 注入漏洞(CVE-2021-44228)
  • 智慧城市驿站:智慧公厕升级版,打造现代化城市生活的便捷配套
  • 大模型爆款应用fabric_构建优雅的提示