tf.Graph

本文深入解析TensorFlow中图(Graph)的概念与操作,包括如何定义运算、使用Tensor及图的各种属性与方法。通过实例演示如何创建和管理图中的运算与张量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考tensorflow官方文档的介绍

1.定义

一个TensorFlow的运算,就是一个数据流的图。

一幅图中包含一些操作(Operation)对象,这些对象是计算节点(计算单元)。前面说过的Tensor对象,则是表示在不同的操作(operation)间的数据节点(数据单元)

默认的图一开始就建好了,并可以通过调用 tf.get_default_graph来访问。要向默认图添加操作,只需调用定义了新操作的函数之一:

import tensorflow as tf

c=tf.constant(4.0)
assert c.graph is tf.get_default_graph()
# assert是断言语句,判断c.graph和tf.get_default_graph是不是同一个默认图,如果不是,会抛出错误
print(c.graph)
print(tf.get_default_graph())
# 同样,我们可以输出两个图来进行判断是不是一致,结果显示一致。

结果


很明显是一致的。

另一个典型的用法是tf.Graph.as_default上下文管理器,它能覆盖上下文的生命周期的当前默认图:

import tensorflow as tf

g = tf.Graph()
with g.as_default():
  # Define operations and tensors in `g`。定义一个操作或张量为g
  c = tf.constant(30.0)
  assert c.graph is g
  # 断言c.graph和g是不是同一个图
print(c.graph)
print(g)

结果:


很显然是的,输出都一样,而且断言也没有报错。

2.属性(Properties)

building_function:   Returns True iff this graph represents a function.

collections:       返回此图所知道的集合的名称。

finalized:       如果这幅图已经完成返回true

graph_def_versions:  此图的GraphDef版本信息

Returns:                            VersionDef
seed:         这张图的图形级随机种子
version        返回一个版本号,该版本号随着操作程序添加到图中而增加,(注意,这与tf.Graph.graph_def_versions无关)。
Returns:                            一个整数版本,随着操作程序的增加而增加。

3.方法(函数)

tf的函数很多,可以到用时去查API,先学习几个

1.get_name_scope:返回当前名称范围

举例

with tf.name_scope('scope1'):
  with tf.name_scope('scope2'):
    print(tf.get_default_graph().get_name_scope())
最后就会返回scope1/scope2

2.get_tensor_by_name(name):返回给定名称的tensor

参数:name:要返回的tensor的名称

3.name_scope(name):返回一个上下文管理器,它为操作创建分级名称。

名称参数将解释如下:

一个字符串(不以“/”结尾)将创建一个新的名称范围,在这个范围中,名称被附加到在上下文中创建的所有操作的前缀。如果名称以前被使用过,它将通过调用self.unique_name(名称)来实现。

举例:

with tf.Graph().as_default() as g:
  c = tf.constant(5.0, name="c")
  assert c.op.name == "c"
  c_1 = tf.constant(6.0, name="c")
  assert c_1.op.name == "c_1"

  # Creates a scope called "nested"
  with g.name_scope("nested") as scope:
  #g.name_scope("nested") as scope为绝对路径,在这范围内的name都是它的下级name
    nested_c = tf.constant(10.0, name="c")
    assert nested_c.op.name == "nested/c"  #只写c会断言错误,要加上g的范围

    # Creates a nested scope called "inner".
    with g.name_scope("inner"):
      nested_inner_c = tf.constant(20.0, name="c")
      assert nested_inner_c.op.name == "nested/inner/c"

    # Create a nested scope called "inner_1".
    with g.name_scope("inner"):   #前面已经存在inner,再添加系统会给重命名
      nested_inner_1_c = tf.constant(30.0, name="c")
      assert nested_inner_1_c.op.name == "nested/inner_1/c"

      # Treats `scope` as an absolute name scope, and
      # switches to the "nested/" scope.
      with g.name_scope(scope):
        nested_d = tf.constant(40.0, name="d")
        assert nested_d.op.name == "nested/d"

        with g.name_scope(""):
          e = tf.constant(50.0, name="e")
          assert e.op.name == "e" #当括号内为空字符串时,就不在g范围内,而只是单纯的操作名字
以后有用到的在更新



              
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tina姐

我就看看有没有会打赏我

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值