Tensorflow函数说明(1)

本文详细介绍了TensorFlow中的图(Graph)概念及其操作方法,包括如何创建和使用图、不同图之间的区别以及如何通过会话(Session)执行图中的运算。同时,还介绍了与图相关的几个实用函数。

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

其一:

Graph 表示可计算的图,其中包含 operation (节点) 和 tensor (边)对象。开始执行程序时会有默认的图建立,可以用 tf.get_default_graph() 来访问。添加操作到默认的图里面可以用下面的方法。

c = tf.constant(4.0)
assert c.graph is tf.get_default_graph()
即不指明创建新图,则所有操作都加到默认的图中。若要创建另外的图,可用下面的方法
# 1. Using Graph.as_default():
g = tf.Graph()
with g.as_default():
  c = tf.constant(5.0)
  assert c.graph is g

# 2. Constructing and making default:
with tf.Graph().as_default() as g:
  c = tf.constant(5.0)
  assert c.graph is g
在 with tf.Graph().as_default() 下,所有的操作都会加入到该图中而不是默认的图。


另外需要注意的是

g1 = tf.Graph()
with g1.as_default():
    c1 = tf.constant([1.0])
with tf.Graph().as_default() as g2:
    c2 = tf.constant([2.0])

with tf.Session(graph=g1) as sess1:
    print sess1.run(c1)
with tf.Session(graph=g2) as sess2:
    print sess2.run(c2)

# result:
# [ 1.0 ]
# [ 2.0 ]
在这种情况下如果交换 c1 和 c2 在 Session 中的位置,则会报错,因为  c1 和 c2 是定义在不同的图中的。

额外要补充的一个是关于 Session 的函数 tf.Session.__init__(target='', graph=none, config=none)

target: 分布式会用到

graph: 如果没有指定则放入默认的图,否则放入指定的图,下面的操作都在指定的图中寻找运行

config: 不介绍


其二:

tf.Graph.add_to_collection/tf.Graph.add_to_collections/tf.add_to_collection

该函数用于将一值加到某一collection上,下面是函数原型。

tf.Graph.add_to_collection(name, value)    (tf.add_to_collection的用法与此相同

name: collection的名字,这里collection可以看做一个集合,里面存储了加进去的值

value: 要加到collection的值。


tf.Graph.add_to_collections(names, value) 

names: collections的名字,多个collection

value: 要加到collections的值。


其三:

tf.add_n(inputs, name=none) 该函数返回输入的所有的 tensor 的值的和 

inputs: 输入为多个 tensor

此外还有,tf.add(x,y,name=none), tf.add(x,y,name=none), tf.sub(x,y,name=none), tf.mul(x,y,name=none), tf.div(x,y,name=none) 等许多数学函数,可以参考这里


其他关于 graph 的函数以后碰到再补充。


文章参考:

http://blog.youkuaiyun.com/xierhacker/article/details/53860379

http://www.cnblogs.com/lienhua34/p/5998853.html

### TensorFlow常见函数列表及说明 #### pad操作 `tf.pad()` 函数用于在张量的不同维度上增加填充。此操作对于卷积网络或自然语言处理任务至关重要[^1]。 ```python import tensorflow as tf paddings = [[1, 1], [2, 2]] tensor = tf.constant([[1, 2], [3, 4]]) result = tf.pad(tensor=tensor, paddings=paddings, mode='CONSTANT', constant_values=0) print(result.numpy()) ``` #### 创建常量张量 通过 `tf.constant()` 可创建一个具有特定值和形状的常量张量。如果提供的是单个数值,整个张量将被该值填满;如果是列表形式的数据,则会依据给定的形状依次填充这些值,当列表长度不足时,默认用最后一位元素补充剩余位置[^2]。 ```python constant_tensor_1d = tf.constant([1., 2., 3.]) constant_tensor_2d = tf.constant(7, shape=[2, 3]) print(constant_tensor_1d.numpy()) print(constant_tensor_2d.numpy()) ``` #### 数据集迭代器设置 利用 `tf.keras.utils.OrderedEnqueuer` 类可以在每个训练周期结束(`on_epoch_end`)后调整数据加载方式,比如开启多进程读取或者打乱样本顺序等[^3]。 ```python from tensorflow.keras.utils import OrderedEnqueuer sequence = ... # 定义自己的Sequence对象 enqueuer = OrderedEnqueuer(sequence, use_multiprocessing=True, shuffle=True) # 后续调用 enqueuer.start() 和获取 batch 的逻辑... ``` #### 改变张量形状 借助于 `tf.reshape()` 方法能够灵活改变输入张量的空间结构而不影响原始数据内容。只需指定新的目标尺寸即可完成转换过程[^4]。 ```python original_tensor = tf.random.uniform((2, 3)) reshaped_tensor = tf.reshape(original_tensor, [-1]) print('Original:', original_tensor.shape) print('Reshaped:', reshaped_tensor.shape) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值