tf.map_fn
是TensorFlow中的一个函数,用于对给定的函数和输入进行逐元素的映射,其定义如下:
- tf.map_fn(
- fn,
- elems,
- dtype=None,
- parallel_iterations=None,
- back_prop=True,
- swap_memory=False,
- infer_shape=True,
- name=None,
- fn_output_signature=None
- )
tf1.x中tf.map_fn没有fn_output_signature参数
tf.map_fn案例
tf.map_fn(fn=lambda t: tf.range(t, t + 3), elems=tf.constant([3, 5, 2]))
https://www.tensorflow.org/api_docs/python/tf/map_fn
- array([[3, 4, 5],
- [5, 6, 7],
- [2, 3, 4]], dtype=int32)>
2、tf.pad
定义 :tf.pad()函数是TensorFlow中的一个方法,用于在张量的边界上进行填充。
tf.pad(tensor, paddings, mode='CONSTANT', constant_values=0)
参数解释:
- tensor:需要填充的张量。
- paddings:填充的大小,格式为[[pad_top, pad_bottom], [pad_left, pad_right], ...],其中每个维度的填充大小为两个元素的列表。例如,如果填充第一个维度10个元素,则paddings为[[10, 0], [0, 0]]。
- mode:填充模式。可选值有'CONSTANT'、'REFLECT'、'SYMMETRIC'。默认为'CONSTANT',表示使用常数进行填充。
- constant_values:当mode为'CONSTANT'时,用于填充的常数值,默认为0。
返回值: 返回填充后的张量。
案例:
- import tensorflow as tf
-
- # 创建一个输入张量
- x = tf.constant([[1, 2], [3, 4]])
-
- # 使用tf.pad函数在各个维度上进行填充
- result = tf.pad(x, paddings=[[1, 1], [2, 2]])
-
- print(result)
paddings=[[1, 1], [2, 2]]是指在第1维的上下均填充1维,在第2维的左右都填充2维
输出结果
- array([[0, 0, 0, 0, 0, 0],
- [0, 0, 1, 2, 0, 0],
- [0, 0, 3, 4, 0, 0],
- [0, 0, 0, 0, 0, 0]], dtype=int32)>
https://www.tensorflow.org/api_docs/python/tf/pad
3、tf.where
- tf.where(
- condition, x=None, y=None, name=None
- )
tf.where
是一个用于根据条件选择元素的函数。它的作用类似于Python中的条件表达式(ternary expression)。
tf.where
函数接受一个条件张量和两个张量(或者相同形状的数组)作为输入,并返回一个新的张量,其中根据条件选择了对应位置的元素。
以下是一个示例,演示如何使用tf.where
函数选择满足条件的元素:
- import tensorflow as tf
-
- # 创建一个条件张量和两个输入张量
- condition = tf.constant([True, False, True])
- x = tf.constant([1, 2, 3])
- y = tf.constant([4, 5, 6])
-
- # 使用tf.where根据条件选择元素
- result = tf.where(condition, x, y)
-
- print(result)
输出结果
3,), dtype=int32, numpy=array([1, 5, 3], dtype=int32)>
4、tf.expand_dims
定义
- tf.expand_dims(
- input, axis, name=None
- )
tf.expand_dims
是TensorFlow中的一个函数,用于在张量的特定维度上插入新的维度。
tf.expand_dims
函数接受一个输入张量和一个axis
参数,它在输入张量的axis
位置插入一个新的维度。
以下是一个示例,展示如何使用tf.expand_dims
函数在张量的特定维度上插入新的维度:
- import tensorflow as tf
-
- # 创建一个输入张量
- x = tf.constant([1, 2, 3, 4])
-
- # 在维度1上插入新的维度
- result = tf.expand_dims(x, axis=1)
-
- print(result)
在这个示例中,我们首先创建了一个输入张量x
,其中包含了四个元素。然后,我们使用tf.expand_dims
函数在维度1上插入新的维度。最后,打印结果。
- tf.Tensor(
- [[1]
- [2]
- [3]
- [4]], shape=(4, 1), dtype=int32)
5、tf.tile
定义
- tf.tile(
- input: _atypes.TensorFuzzingAnnotation[TV_Tile_T],
- multiples: _atypes.TensorFuzzingAnnotation[TV_Tile_Tmultiples],
- name=None
- ) -> _atypes.TensorFuzzingAnnotation[TV_Tile_T]
tf.tile
是TensorFlow中的一个函数,用于在给定维度上复制张量的值。
tf.tile
函数接受一个输入张量和一个multiples
参数,它在输入张量的每个维度上复制相应倍数的值,并返回一个新的张量。
以下是一个示例,演示如何使用tf.tile
函数在给定维度上复制张量的值
- import tensorflow as tf
-
- # 创建一个输入张量
- x = tf.constant([1, 2, 3])
-
- # 在维度0上复制2次
- result = tf.tile(x, multiples=[2])
-
- print(result)
输出结果:
tf.Tensor([1 2 3 1 2 3], shape=(6,), dtype=int32)
可以看到,通过tf.tile
函数在维度0上复制了两次输入张量的值,得到了一个形状为(6,)
的新张量。
需要注意的是,multiples
参数是一个列表,表示在每个维度上复制的倍数。如果multiples
的长度小于输入张量的维度数,则会自动在后续维度上复制一次。例如,如果输入张量的形状是(3, 4, 5, 6)
,而multiples
为[2, 3]
,则会在维度0上复制2次,在维度1上复制3次,在维度2和维度3上各复制1次。
tf.tile
函数在很多情况下非常有用,特别是在需要进行张量形状扩展或对齐操作时。
6、tf.gather_nd
tf.gather_nd
是TensorFlow中的一个函数,用于根据索引获取多维张量中的元素的值。定义
- tf.gather_nd(
- params, indices, batch_dims=0, name=None
- )
tf.gather_nd
函数接受一个输入张量和一个索引张量作为输入,它根据索引张量中指定的索引位置,从输入张量中获取对应的元素值,并返回一个新的张量。
以下是一个示例,演示如何使用tf.gather_nd
函数获取多维张量中的元素值:
- import tensorflow as tf
-
- # 创建一个输入张量
- x = tf.constant([[1, 2], [3, 4], [5, 6]])
-
- # 创建一个索引张量
- indices = tf.constant([[0, 0], [2, 1]])
-
- # 使用tf.gather_nd函数获取元素值
- result = tf.gather_nd(x, indices)
-
- print(result)
在这个示例中,我们首先创建了一个输入张量x
,它是一个 3x2 的张量。然后,我们创建了一个索引张量indices
,其中包含了两个索引位置的坐标。接下来,我们使用tf.gather_nd
函数根据索引张量获取输入张量中对应位置的元素值。最后,打印结果。
tf.Tensor([1 6], shape=(2,), dtype=int32)
7、tf.reduce_min
tf.reduce_min
是TensorFlow中的一个函数,用于计算张量在指定维度上的最小值。当在tf.reduce_min
函数中不指定axis
参数时,它会计算整个张量的最小值。定义
output = tf.reduce_min(input_tensor, axis=None, keepdims=False, name=None)
案例
不指定axis时,计算整个张量的最小值
- import tensorflow as tf
-
- # 创建一个输入张量
- x = tf.constant([[1, 2, 3], [4, 5, 6]])
-
- # 计算整个张量的最小值
- result = tf.reduce_min(x)
-
- print(result)
输出如下:
tf.Tensor(1, shape=(), dtype=int32)
指定aixs时,计算aixs的维度的最小值
- import tensorflow as tf
-
- # 创建一个输入张量
- x = tf.constant([[1, 2], [3, 4]])
-
- # 计算张量在维度0上的最小值
- result = tf.reduce_min(x, axis=0)
-
- print(result)
输出:
tf.Tensor([1 2], shape=(2,), dtype=int32)
8、tf.stack
tf.stack将一系列 R 阶张量堆叠到一个 (R+1) 阶张量中。 定义
- tf.stack(
- values, axis=0, name='stack'
- )
通过沿轴维度将值中的张量列表打包为比每个张量高一级的张量。给定长度为 N 的形状为 (A, B, C) 的张量列表;如果 axis == 0 那么输出张量将具有形状 (N, A, B, C)。如果 axis == 1 那么输出张量将具有形状 (A, N, B, C)。
案例
- x = tf.constant([1, 4])
- y = tf.constant([2, 5])
- z = tf.constant([3, 6])
- tf.stack([x, y, z])
-
- tf.stack([x, y, z], axis=1)
- tf.stack([x,y,z])的输出
- array([[1, 4],
- [2, 5],
- [3, 6]], dtype=int32)>
-
-
- tf.stack([x,y,z],axis=1)的输出
- array([[1, 2, 3],
- [4, 5, 6]], dtype=int32)>
9、tf.concat
tf.concat
是 TensorFlow 中的一个函数,用于沿指定的轴拼接张量。它接受一个张量列表,并沿着指定的轴拼接它们。定义
- tf.concat(
- values, axis, name='concat'
- )
案例
- import tensorflow as tf
-
- # 创建两个张量
- tensor1 = tf.constant([[1, 2, 3], [4, 5, 6]])
- tensor2 = tf.constant([[7, 8, 9], [10, 11, 12]])
-
- # 沿轴0拼接
- result = tf.concat([tensor1, tensor2], axis=0)
-
- with tf.Session() as sess:
- print(sess.run(result))
结果输出
- array([[ 1, 2, 3],
- [ 4, 5, 6],
- [ 7, 8, 9],
- [10, 11, 12]], dtype=int32)>
10、tensor.get_shape().as_list()
tensor.get_shape()获取tensor的维度,.as_list()以list的形式返回
- x = tf.constant([[1, 2, 3], [4, 5, 6]])
- shape = x.get_shape().as_list()
输出
[2, 3]
11、tf.unique_with_counts
tf.unique_with_counts
函数用于对输入张量中的元素进行去重,并返回去重后的元素、元素在原始张量中的索引、元素在原始张量中的计数。x为1-d tensor,定义:
y, idx, count = tf.unique_with_counts(x, out_idx=tf.int64)
https://github.com/tensorflow/docs/blob/r1.12/site/en/api_docs/python/tf/unique_with_counts.md
案例:
- x = tf.constant([1, 2, 3, 1, 2, 1, 3, 3, 3])
- y, idx, count = tf.unique_with_counts(x)
输出结果
不重复的元素y:
索引下标idx:
不重复元素对应的计数count:
12、tf.greater_equal
tf.greater_equal
是 TensorFlow 中用于比较两个张量元素是否满足大于等于的元素级别比较的函数。定义如下:
result = tf.greater_equal(x, y, name=None)
案例:
两个张量比较大小
- # 创建输入张量
- x = tf.constant([1, 2, 3, 4])
- y = tf.constant([2, 2, 2, 2])
-
- # 进行元素级别的大于等于比较
- result = tf.greater_equal(x, y)
输出:
张量和一个数值比较大小
- # 创建输入张量
- x = tf.constant([1, 2, 3, 4])
- y = 2
-
- # 进行元素级别的大于等于比较
- result = tf.greater_equal(x, y)
输出
13、tf.reshape
tf.reshape
是 TensorFlow 中用于改变张量形状的函数。它可以用来重新排列张量的维度,以适应不同的计算需求。
reshaped_tensor = tf.reshape(tensor, shape, name=None)
案例
- x = tf.constant([[1, 2, 3], [4, 5, 6]])
-
- # 改变张量形状
- reshaped_x = tf.reshape(x, [3, 2])
输出结果
- array([[1, 2],
- [3, 4],
- [5, 6]], dtype=int32)>
14、tf.cast
将一个 tensor 变为新的类型 type。定义
- tf.cast(
- x, dtype, name=None
- )
案例
- x = tf.constant([1.8, 2.2], dtype=tf.float32)
- tf.cast(x, tf.int32)
输出
15、tf.div_no_nan
计算不安全除法,如果y等于0,则返回0。定义:
- tf.div_no_nan(
- x,
- y,
- name=None
- )
案例:
- x = tf.constant([1, 2, 3, 4], dtype=tf.float32)
- y = tf.constant([0, 2, 0, 4], dtype=tf.float32)
- z=tf.div_no_nan(x,y)
输出:
16、tf.nn.softmax_cross_entropy_with_logits_v2
计算labels和logits之间的交叉熵,定义如下:
- tf.nn.softmax_cross_entropy_with_logits_v2(
- _sentinel=None,
- labels=None,
- logits=None,
- dim=-1,
- name=None
- )
案例
- import tensorflow as tf
-
- # 创建标签张量和预测得分张量
- labels = tf.constant([[0, 1, 0], [1, 0, 0]], dtype=tf.float32)
- logits = tf.constant([[1, 2, 3], [4, 5, 6]], dtype=tf.float32)
-
- # 计算 softmax 交叉熵损失
- loss = tf.nn.softmax_cross_entropy_with_logits(labels=labels, logits=logits)
-
输出
17、tf.math.log1p
tf.math.log1p
是 TensorFlow 中的一个数学函数,用于计算输入张量加1后的自然对数。
tf.math.log1p
的基本用法如下:
output = tf.math.log1p(input)
案例
- x = tf.constant([1, 2, 3],dtype=tf.float32)
- output = tf.math.log1p(x)
输出
3,), dtype=float32, numpy=array([0.6931472, 1.0986123, 1.3862944], dtype=float32)>
参考文献
https://www.tensorflow.org/api_docs/python/tf/map_fn
-
相关阅读:
华为政企服务器产品集
从趋势到挑战,资深工程师一站式解读:操作系统运维和可观测性
Delphi 安卓11 中文语音合成(中文朗读)注意内容
.NET开源的简单、快速、强大的前后端分离后台权限管理系统
SSM+便民自行车管理系统 毕业设计-附源码191633
【二叉树】层数最深叶子节点的和
QT——其他方式实现HelloWrold
半小时速通Python爬虫!GitHub开源的Python爬虫入门教程
Clusterpedia 使用心得
代码随想录算法训练营第五十九天丨 单调栈02
-
原文地址:https://blog.csdn.net/u013069552/article/details/133776011
-
最新文章
-
C++11 线程同步接口std::condition_variable和std::future的简单使用
Go runtime 调度器精讲(十一):总览全局
Spring框架漏洞总结
Angular 18+ 高级教程 – 国际化 Internationalization i18n
基于Tauri2+Vue3搭建桌面端程序|tauri2+vite5多窗口|消息提醒|托盘闪烁
ComfyUI 基础教程(五) —— 应用 IP-Adapter 实现图像风格迁移
网络空间的“边水往事”?针对华语黑产及用户进行攻击的 APT-K-UN3 活动分析
伪装“黑神话悟空修改器”传播木马的活动分析
全球蓝屏后,微软决定将安全踢出Windows内核
Java读取寄存器数据的方法