TensorFlow使用Graph的基本操作的实现

作者:Baby-Lily 时间:2023-04-10 22:22:37 

1.创建图

在tensorflow中,一个程序默认是建立一个图的,除了系统自动建立图以外,我们还可以手动建立图,并做一些其他的操作。

下面我们使用tf.Graph函数建立图,使用tf.get_default_graph函数来获取图,使用reset_default_graph对图进行重置。


import tensorflow as tf
import numpy as np

c = tf.constant(1.5)
g = tf.Graph()

with g.as_default():

c1 = tf.constant(2.0)
 print(c1.graph)
 print(g)
 print(c.graph)

g2 = tf.get_default_graph()
print(g2)

tf.reset_default_graph()
g3 = tf.get_default_graph()
print(g3)

上述的代码运行结果如下所示:

TensorFlow使用Graph的基本操作的实现

根据上述的运行结果,c是在刚开始的默认图中建立的,所以打印的结果就是13376A1FE10,和g2获取的默认图的值是一样的,然后使用tf.Graph建立了一个新的图,并添加了变量c1,最后又对图进行了重置,替代了原来的默认图。

在使用reset_default_graph()函数的时候,要保证当前图中资源都已经全部进行了释放,否则将会报错。

2.获取张量

我们可以在图中通过名字得到其对应的元素,比如获取图中的变量和OP等元素。


import tensorflow as tf
import numpy as np

g = tf.Graph()

with g.as_default():
 c1 = tf.constant(2.5, name='c1_constant')
 c2 = tf.Variable(1.5, dtype=tf.float32, name='c2_constant')
 add = tf.multiply(c1, c2, name='op_add')

c_1 = g.get_tensor_by_name(name='c1_constant:0')
 c_2 = g.get_tensor_by_name(name='c2_constant:0')
 c_3 = g.get_tensor_by_name(name='op_add:0')

print(c_1)
 print(c_2)
 print(c_3)

TensorFlow使用Graph的基本操作的实现

在进行测试时,我们为元素添加了变量名,在设置变量名的时候,设置好的名字会自动添加后面的:0字符。一般我们可以将名字打印出来,在将打印好的名字进行回填。

3.获取节点操作

获取节点操作OP的方法和获取张量的方法非常类似,使用get_operation_by_name.下面是运行实例:


import tensorflow as tf
import numpy as np

a = tf.constant([[1.0, 2.0]])
b = tf.constant([[1.0], [3.0]])

tensor_1 = tf.matmul(a, b, name='matmul_1')

with tf.Session() as sess:
 sess.run(tf.global_variables_initializer())
 t1 = tf.get_default_graph().get_operation_by_name(name='matmul_1')
 t2 = tf.get_default_graph().get_tensor_by_name(name='matmul_1:0')
 print(t1)
 print('t1: ', sess.run(t1))
 print('t2: ', sess.run(t2))

在上述的代码中,定义了一个OP操作,命名为matmul_1,在运行时我们将op打印出来,在使用名字后面加上:0我们就能得到OP运算的结果的tensor,注意这两者的区别。

我们还可以通过get_opreations函数获取图中的所有信息。此外,我们还可以使用tf.Grapg.as_graph_element函数将传入的对象返回为张量或者op。该函数具有验证和转换功能。

来源:https://www.cnblogs.com/baby-lily/p/10960054.html

标签:TensorFlow,Graph
0
投稿

猜你喜欢

  • SQL提取数据库表名及字段名等信息代码示例

    2024-01-26 23:18:54
  • 格式化数字ASP,PHP版

    2009-01-19 14:17:00
  • mysql 数据同步 出现Slave_IO_Running:No问题的解决方法小结

    2024-01-22 22:59:34
  • python目标检测IOU的概念与示例

    2022-04-19 07:41:25
  • linux下mysql开启远程访问权限 防火墙开放3306端口

    2024-01-24 10:59:14
  • Node.js多进程的方法与参数实例说明

    2024-05-02 17:37:37
  • python中关于时间和日期函数的常用计算总结(time和datatime)

    2022-01-02 05:50:08
  • django请求返回不同的类型图片json,xml,html的实例

    2021-05-17 10:03:23
  • JS代码混淆加密工具

    2008-05-25 13:49:00
  • python groupby 函数 as_index详解

    2023-09-13 21:04:07
  • JavaScript中的toDateString()方法使用详解

    2024-05-13 10:37:51
  • python执行get提交的方法

    2022-08-09 01:53:12
  • python中lambda()的用法

    2022-07-19 05:15:45
  • Flaks基础之在URL中添加变量的实现详解

    2023-07-22 00:42:20
  • 13个最常用的Python深度学习库介绍

    2023-08-04 03:08:09
  • Tensorflow中的dropout的使用方法

    2021-04-22 01:21:58
  • 滚动条样式的css代码介绍

    2008-10-21 10:56:00
  • Python入门教程(九)Python字符串介绍

    2023-02-05 22:34:43
  • Spring boot连接MySQL 8.0可能出现的问题

    2024-01-17 17:04:28
  • python绘制散点图并标记序号的方法

    2023-12-22 09:59:49
  • asp之家 网络编程 m.aspxhome.com