Tensorflow中的降维函数tf.reduce_*使用总结

Fiorenza ·
更新时间:2024-09-20
· 764 次阅读

在使用tensorflow时常常会使用到tf.reduce_*这类的函数,在此对一些常见的函数进行汇总

1.tf.reduce_sum

tf.reduce_sum(input_tensor , axis = None , keep_dims = False , name = None , reduction_indices = None)

参数:

input_tensor:要减少的张量。应该有数字类型。 axis:要减小的尺寸。如果为None(默认),则缩小所有尺寸。必须在范围[-rank(input_tensor), rank(input_tensor))内。 keep_dims:如果为true,则保留长度为1的缩小尺寸。 name:操作的名称(可选)。 reduction_indices:axis的废弃的名称。

返回:

该函数返回减少的张量,相当于np.sum

功能:

此函数计算一个张量的各个维度上元素的总和。

说明:

函数中的input_tensor是按照axis中已经给定的维度来减少的;除非 keep_dims 是true,否则张量的秩将在axis的每个条目中减少1;如果keep_dims为true,则减小的维度将保留为长度1。 如果axis没有条目,则缩小所有维度,并返回具有单个元素的张量。

举例:

x = tf.constant([[1, 1, 1], [1, 1, 1]]) tf.reduce_sum(x) # 6 tf.reduce_sum(x, 0) # [2, 2, 2] tf.reduce_sum(x, 1) # [3, 3] tf.reduce_sum(x, 1, keep_dims=True) # [[3], [3]] tf.reduce_sum(x, [0, 1]) # 6

2.reduce_min

reduce_min(input_tensor, axis=None, keep_dims=False, name=None, reduction_indices=None)

参数:

input_tensor:减少的张量。应该有数字类型。 axis:要减小的尺寸。如果为None(默认),则缩小所有维度。必须在[-rank(input_tensor), rank(input_tensor))范围内。 keep_dims:如果为true,则保留长度为1的缩小维度。 name:操作的名称(可选)。 reduction_indices:axis的废弃的名称。

返回:

该函数返回减少的张量,相当于np.min

功能:

tf.reduce_min函数用来计算一个张量的各个维度上元素的最小值。 

说明:

同样按照axis给定的维度减少input_tensor。除非 keep_dims 是true,否则张量的秩将在axis的每个条目中减少1。如果keep_dims为true,则减小的维度将保留为长度1。 如果axis没有条目,则缩小所有维度,并返回具有单个元素的张量。

3.reduce_max

reduce_max(input_tensor, axis=None, keep_dims=False, name=None, reduction_indices=None)

参数:

input_tensor:要减少的张量。应该有数字类型。 axis:要减小的尺寸。如果为 None(默认),则减少所有维度。必须在[-rank(input_tensor), rank(input_tensor))范围内。 keep_dims:如果为true,则保留长度为1的减少维度。 name:操作的名称(可选)。 reduction_indices:axis的废弃的名称。

返回:

该函数返回减少的张量,相当于np.max。

功能:

计算一个张量的各个维度上元素的最大值。 

说明:

按照axis给定的维度减少input_tensor。除非 keep_dims 是true,否则张量的秩将在axis的每个条目中减少1。如果keep_dims为true,则减小的维度将保留为长度1。如果axis没有条目,则减少所有维度,并返回具有单个元素的张量。

4.reduce_mean

reduce_mean

5.reduce_all

reduce_all(input_tensor, axis=None, keep_dims=False, name=None, reduction_indices=None)

参数:

input_tensor:要减少的张量。应该有数字类型。 axis:要减小的尺寸。如果为None(默认),则减少所有维度。必须在[-rank(input_tensor), rank(input_tensor))范围内。 keep_dims:如果为true,则保留长度为1的缩小尺寸。 name:操作的名称(可选)。 reduction_indices:axis的不支持使用的名称。

返回:

该函数返回减少的张量,相当于np.mean

功能:

计算张量的各个维度上的元素的平均值。

说明:

axis是tf.reduce_mean函数中的参数,按照函数中axis给定的维度减少input_tensor。除非keep_dims是true,否则张量的秩将在axis的每个条目中减少1。如果keep_dims为true,则缩小的维度将保留为1。 如果axis没有条目,则减少所有维度,并返回具有单个元素的张量。

举例:

x = tf.constant([[1., 1.], [2., 2.]]) tf.reduce_mean(x) # 1.5 tf.reduce_mean(x, 0) # [1.5, 1.5] tf.reduce_mean(x, 1) # [1., 2.]

6.reduce_any

reduce_any(input_tensor, axis=None, keep_dims=False, name=None, reduction_indices=None)

参数:

input_tensor:要减少的布尔张量。 axis:要减小的尺寸。如果为None(默认),则减少所有维度。必须在范围[-rank(input_tensor), rank(input_tensor))内。 keep_dims:如果为true,则保留长度为1的缩小维度。 name:操作的名称(可选)。 reduction_indices:axis的已经弃用的名称。

返回:

减少张量,相当于np.any

功能:

在张量的维度上计算元素的 "逻辑或"。 

说明:

按照axis给定的维度减少input_tensor。除非 keep_dims 是 true,否则张量的秩将在axis的每个条目中减少1。如果keep_dims为true,则缩小的维度将保留为1。如果axis没有条目,则会减少所有维度,并返回具有单个元素的张量。

举例:

x = tf.constant([[True, True], [False, False]]) tf.reduce_any(x) # True tf.reduce_any(x, 0) # [True, True] tf.reduce_any(x, 1) # [True, False]

7.reduce_logsumexp

reduce_logsumexp(input_tensor, axis=None, keep_dims=False, name=None, reduction_indices=None)

参数:

input_tensor:张量减少。应该有数字类型。 axis:要减小的维度。如果为None(默认),则减少所有维度。必须在[-rank(input_tensor), rank(input_tensor))范围内。 keep_dims:如果为true,则保留长度为1的减少尺寸。 name:操作的名称(可选)。 reduction_indices:axis的弃用名称。

返回:

减少的张量。

功能:

计算log(sum(exp(张量的各维数的元素)))。 

说明:

按照给定的axis上的维度减少input_tensor。除非keep_dims是true,否则张量的秩在axis上的每一项都减少1。如果keep_dims为 true,则减少的尺寸将保留为1。如果axis没有条目,则缩小所有维度,并返回具有单个元素的张量。这个函数在数值上比 log(sum(exp(input)))更稳定。它避免了大量输入的 exp 引起的溢出和小输入日志带来的下溢。

举例:

x = tf.constant([[0., 0., 0.], [0., 0., 0.]]) tf.reduce_logsumexp(x) # log(6) tf.reduce_logsumexp(x, 0) # [log(2), log(2), log(2)] tf.reduce_logsumexp(x, 1) # [log(3), log(3)] tf.reduce_logsumexp(x, 1, keep_dims=True) # [[log(3)], [log(3)]] tf.reduce_logsumexp(x, [0, 1]) # log(6)

8.reduce_prod

reduce_prod(input_tensor, axis=None, keep_dims=False, name=None, reduction_indices=None)

参数:

input_tensor:要减少的张量。应该有数字类型。 axis:要减小的尺寸。如果为None(默认),则将缩小所有尺寸。必须在[-rank(input_tensor), rank(input_tensor))范围内。 keep_dims:如果为true,则保留长度为1的缩小维度。 name:操作的名称(可选)。 reduction_indices:axis的废弃的名称。

返回:

结果返回减少的张量,相当于np.prod

功能:

此函数计算一个张量的各个维度上元素的乘积。 

说明:

函数中的input_tensor是按照axis中已经给定的维度来减少的;除非 keep_dims 是true,否则张量的秩将在axis的每个条目中减少1;如果keep_dims为true,则减小的维度将保留为长度1。 如果axis没有条目,则缩小所有维度,并返回具有单个元素的张量。

到此这篇关于Tensorflow中的降维函数tf.reduce_*使用总结的文章就介绍到这了,更多相关Tensorflow 降维函数tf.reduce_*内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!

您可能感兴趣的文章:关于Tensorflow中的tf.train.batch函数的使用tensorflow 用矩阵运算替换for循环 用tf.tile而不写for的方法对tf.reduce_sum tensorflow维度上的操作详解Tensorflow 利用tf.contrib.learn建立输入函数的方法tensorflow中tf.slice和tf.gather切片函数的使用tensorflow实现在函数中用tf.Print输出中间值tensorflow实现读取模型中保存的值 tf.train.NewCheckpointReaderTensorFlow tf.nn.max_pool实现池化操作方式tensorflow tf.train.batch之数据批量读取方式TensorFlow tf.nn.conv2d实现卷积的方式Tensorflow tf.dynamic_partition矩阵拆分示例(Python3)



reduce tensorflow

需要 登录 后方可回复, 如果你还没有账号请 注册新账号
相关文章