tf.math

参考 tf.math - 云+社区 - 腾讯云

目录

一、函数列表

二、重要的API

1、tf.floor

2、tf.log

3、tf.reduce_mean

4、tf.reduce_sum

5、tf.add_n

6、tf.math.top_k

7、tf.math.argmax

8、tf.math.greater_equal

9、tf.math.pow

10、tf.math.multiply

11、tf.math.sqrt

12、tf.math.logical_or

13、tf.math.truediv

14、tf.math.less

15、tf.math.count_nonzero

16、tf.math.scalar_mul

17、tf.math.conj

18、tf.math.floormod

19、tf.math.sigmoid

20、tf.math.scalar_mul

21、tf.math.multiply

22、tf.math.logical_not

23、tf.math.add

24、tf.math.subtract

25、tf.math.add_n


一、函数列表

二、重要的API

1、tf.floor

返回不大于x的元素最大整数。

tf.math.floor(
    x,
    name=None
)

参数:

  • x: 张量。必须是以下类型之一:bfloat16、half、float32、float64。
  • name: 操作的名称(可选)。

返回值:

  • 与x类型相同的张量。

2、tf.log

计算x元素的自然对数。

tf.math.log(
    x,
    name=None
)

例如,\tiny y=log_e x

参数:

  • x: 张量。必须是以下类型之一:bfloat16、half、float32、float64、complex64、complex128。
  • name: 操作的名称(可选)。

返回值:

  • 一个与x类型相同的张量。

3、tf.reduce_mean

计算元素跨张量维数的平均值。

tf.math.reduce_mean(
    input_tensor,
    axis=None,
    keepdims=False,
    name=None
)

沿着坐标轴给出的维数减少input_张量。除非keepdims为真,否则对于轴上的每一项,张量的秩都会减少1。如果keepdims为真,则使用长度1保留缩减后的维度。如果轴为空,则所有维数都被缩减,并返回一个只有一个元素的张量。

例如:

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.]

参数:

  • input_tensor: 要减少的张量。应该具有数值类型。
  • axis: 要缩小的尺寸。如果没有(默认值),则减少所有维度。必须在[-rank(input_张量),rank(input_张量)]范围内。
  • keepdims: 如果为真,则保留长度为1的缩减维度。
  • name: 操作的名称(可选)。

返回值:

  • 一个减少的张量。

请注意np.mean有一个dtype参数,可用于指定输出类型。默认情况下,这是dtype=float64。另一方面,tf.reduce_mean有一个来自input_张量的攻击类型推断,例如:

x = tf.constant([1, 0, 1, 0])
tf.reduce_mean(x)  # 0
y = tf.constant([1., 0., 1., 0.])
tf.reduce_mean(y)  # 0.5

4、tf.reduce_sum

计算张量维数中元素的和。

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

警告:一些参数是不支持的:(keep_dims)。它们将在未来的版本中被删除。

更新说明:不推荐使用keep_dims,而是使用keepdims。

沿着坐标轴给出的维数减少input_张量。除非keepdims为真,否则对于轴上的每一项,张量的秩都会减少1。如果keepdims为真,则使用长度1保留缩减后的维度。如果轴为空,则所有维数都被缩减,并返回一个只有一个元素的张量。

例:

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, keepdims=True)  # [[3], [3]]
tf.reduce_sum(x, [0, 1])  # 6
x = tf.constant([[1, 2, 4], [8, 16, 32]])
a = tf.reduce_sum(x, -1)  # [ 9 18 36]

参数:

  • input_tensor:要减少的张量。应该具有数值类型。
  • axis:要缩小的尺寸。如果没有(默认值),则减少所有维度。必须在[-rank(input_张量),rank(input_张量)]范围内。
  • keepdims:如果为真,则保留长度为1的缩减维度。
  • name:操作的名称(可选)。
  • reduction_indices: axis的旧名称(已弃用)。
  • keep_dims: keepdims的弃用别名。

返回值:

  • 简化张量,与input_tensor具有相同的d型。

5、tf.add_n

按顺序对输入的张量进行求和。

tf.add_n(
    inputs,
    name=None
)

在添加之前将indexedslice对象转换为密集张量。

 例如:

a = tf.constant([[3, 5], [4, 8]])
b = tf.constant([[1, 6], [2, 9]])
tf.math.add_n([a, b, a])  # [[7, 16], [10, 25]]

6、tf.math.top_k

tf.math.top_k

tf.math.top_k(
    input,
    k=1,
    sorted=True,
    name=None
)

查找最后一个维度的k个最大项的值和索引。如果输入是一个向量(rank=1),找到向量中k个最大的元素,并将它们的值和索引作为向量输出。因此value [j]是输入的第j个最大的条目,它的索引是index [j]。矩阵(分别地。,计算每一行的前k个条目(resp)。沿着最后一个维度的向量)。因此,

values.shape = indices.shape = input.shape[:-1] + [k]

如果两个元素相等,则首先出现下标元素。

参数:

  • input:一维或更高张量,最后维数至少为k。
  • k: 0-D int32张量。要沿着最后一个维度查找的顶部元素的数量(对于矩阵,沿着每一行查找)。
  • sorted:如果为真,则得到的k个元素将按降序排列。
  • name:操作的可选名称。

返回值:

  • values: 沿最后一个维度切片的k个最大元素。
  • indices: 输入的最后一个维度内的值的索引。

7、tf.math.argmax

返回一个张量在轴上的最大值的指标。

tf.math.argmax(
    input,
    axis=None,
    name=None,
    dimension=None,
    output_type=tf.dtypes.int64
)

参数:

  • input:一个张量。必须是以下类型之一:float32、float64、int32、uint8、int16、int8、complex64、int64、qint8、quint8、qint32、bfloat16、uint16、complex128、half、uint32、uint64。
  • axis:张量。必须是下列类型之一:int32、int64。int32或int64,必须在[-rank(输入),rank(输入)]范围内。描述输入张量的哪个轴要缩小。对于向量,使用axis = 0。
  • output_type:一个可选的tf.DType from: tf.int32, tf.int64。默认为tf.int64。
  • name:操作的名称(可选)。

返回值:

  • 一个输出t_type类型的张量。

例:

import tensorflow as tf

x = tf.constant([[1., 2., 6], [6., 2., 6]])

xShape = tf.shape(x)
z1 = tf.arg_max(x, 1)  # 沿axis=0操作

with tf.Session() as sess:
    xShapeValue, d1 = sess.run([xShape, z1])
    print('shape= %s' % (xShapeValue))
    print(d1)

8、tf.math.greater_equal

在生成的文件中定义:python/ops/gen_math_ops.py,返回元素的真值(x >= y)。

tf.math.greater_equal(
    x,
    y,
    name=None
)

参数:

  • x:张量。必须是下列类型之一:float32、float64、int32、uint8、int16、int8、int64、bfloat16、uint16、half、uint32、uint64。
  • y:张量。必须具有与x相同的类型。
  • name:操作的名称(可选)。

返回值:

  • bool类型的张量。

9、tf.math.pow

计算一个值对另一个值的幂。

tf.math.pow(
    x,
    y,
    name=None
)

给定一个张量x和一个张量y,这个操作计算x和y中对应的\large x^y元素。例如:

x = tf.constant([[2, 2], [3, 3]])
y = tf.constant([[8, 16], [2, 3]])
tf.pow(x, y)  # [[256, 65536], [9, 27]]

参数:

  • x:类型为float16、float32、float64、int32、int64、complex64或complex128的张量
  • y:类型为float16、float32、float64、int32、int64、complex64或complex128的张量
  • name:操作的名称(可选)

返回值:

  • 一个张量

10、tf.math.multiply

逐元素的返回x*y。

tf.math.multiply(
    x,
    y,
    name=None
)

参数:

  • x:张量。必须是以下类型之一:bfloat16、half、float32、float64、uint8、int8、uint16、int16、int32、int64、complex64、complex128
  • y:张量。必须具有与x相同的类型
  • name:操作的名称(可选)

返回值:

  • 一个张量。与x类型相同

11、tf.math.sqrt

计算x元素的平方根。

tf.math.sqrt(
    x,
    name=None
)

参数:

  • x:张量。必须是以下类型之一:bfloat16、half、float32、float64、complex64、complex128。
  • name:操作的名称(可选)。

返回值:

  • 一个张量。与x类型相同。

如果x是稀疏张量,返回稀疏张量(x。指标,tf.math.sqrt (x.value,…),x.dense_shape)

12、tf.math.logical_or

返回x或y元素的真值。

tf.math.logical_or(
    x,
    y,
    name=None
)

参数:

  • x: bool型张量。
  • y: bool型张量。
  • name:操作的名称(可选)。

返回值:

  • bool类型的张量。

13、tf.math.truediv

使用Python 3的除法运算符语义来分割x / y元素。

tf.math.truediv(
    x,
    y,
    name=None
)

注意:最好使用遵循Python除法运算符语义的张量运算符或tf.divide。该函数强制python3除法运算符语义,其中所有整数参数首先转换为浮点类型。这个op是由python3中的普通x / y除法和python2.7中的来自于_future__导入除法生成的。如果需要向下舍入的整数除法,请使用x // y或tf.math.floordiv。x和y必须具有相同的数字类型。如果输入是浮点数,则输出将具有相同的类型。如果输入是整数,则将int8和int16的输入转换为float32, int32和int64的输入转换为float64(匹配Numpy的行为)。

参数:

  • x:数值型张量分子。
  • y:数值型张量分母。
  • name:操作的名称(可选)。

返回值:

  • 用浮点数表示x / y。

可能产生的异常:

  • TypeError: If x and y have different dtypes.

14、tf.math.less

返回(x < y)元素的真值。

tf.math.less(
    x,
    y,
    name=None
)

参数:

  • x:张量。必须是下列类型之一:float32、float64、int32、uint8、int16、int8、int64、bfloat16、uint16、half、uint32、uint64。
  • y:张量。必须具有与x相同的类型。
  • name:操作的名称(可选)。

返回值:

  • bool类型的张量。

15、tf.math.count_nonzero

计算张量维上非零元素的个数。

tf.math.count_nonzero(
    input,
    axis=None,
    keepdims=None,
    dtype=tf.dtypes.int64,
    name=None
)

减少沿轴方向给出的尺寸的输入。除非keepdims为真,否则对于轴上的每一项,张量的秩都会减少1。如果keepdims为真,则使用长度1保留缩减后的维度。如果轴没有项,所有的维数都被缩减,并且返回一个只有一个元素的张量。

例:

x = tf.constant([[0, 1, 0], [1, 1, 0]])
tf.math.count_nonzero(x)  # 3
tf.math.count_nonzero(x, 0)  # [1, 2, 0]
tf.math.count_nonzero(x, 1)  # [1, 2]
tf.math.count_nonzero(x, 1, keepdims=True)  # [[1], [2]]
tf.math.count_nonzero(x, [0, 1])  # 3

例:

x = tf.constant(["", "a", "  ", "b", ""])
tf.math.count_nonzero(x) # 3, with "a", "  ", and "b" as nonzero strings.

参数:

  • input:要减少的张量。应该是数字类型、bool或字符串。
  • axis:要缩小的尺寸。如果没有(默认值),则减少所有维度。必须在[-rank(输入),rank(输入)]范围内。
  • keepdims:如果为真,则保留长度为1的缩减维度。
  • dtype:输出dtype;默认为tf.int64。
  • name:操作的名称(可选)。

返回值:

  • 简化张量(非零值的数目)。

16、tf.math.scalar_mul

将标量乘以张量或索引切片对象。

tf.math.scalar_mul(
    scalar,
    x,
    name=None
)

用于梯度代码中,该代码可能处理indexedslice对象,这些对象很容易乘以标量,但与任意张量相乘的代价更高。

参数:

  • scalar:0-D标量张量。一定知道形状。
  • x:要缩放的张量或索引切片。
  • name:操作的名称(可选)。

返回值:

  • 与x相同类型的标量* x(张量或索引切片)。

17、tf.math.conj

返回复数的复共轭。

tf.math.conj(
    x,
    name=None
)

返回复数的复共轭。给定一个复数张量输入,这个操作返回一个复数张量,它是输入中每个元素的复共轭。输入的复数必须是a+bj的形式,其中a是实数,b是虚数。这个运算返回的复共轭是a-bj的形式。

例如:

tensor 'input' is [-2.25 + 4.75j, 3.25 + 5.75j]

tf.math.conj(输入)= = > [-2.25 - 4.75,3.25 - 5.75 j]

如果x是实数,则返回值不变。

参数:

  • x:共轭张量。必须具有数值类型或变体类型。
  • name:操作的名称(可选)。

返回值:

  • 一个张量,它是x的共轭(具有相同类型)。

可能产生的异常:

  • TypeError: If x is not a numeric tensor.

18、tf.math.floormod

tf.math.floormod(
    x,
    y,
    name=None
)

的确,这遵循Python语义,因为这里的结果与地板划分一致。例如,floor(x / y) * y + mod(x, y) = x。注意:数学。floormod支持广播。

参数:

  • x:张量。必须是下列类型之一:int32、int64、bfloat16、half、float32、float64。
  • y:张量。必须具有与x相同的类型。
  • name:操作的名称(可选)。

返回值:

  • 一个张量。与x类型相同。

19、tf.math.sigmoid

Computes sigmoid of x element-wise.

Aliases:

tf.math.sigmoid(
    x,
    name=None
)

Specifically, y = 1 / (1 + exp(-x)).

Args:

  • x: A Tensor with type float16, float32, float64, complex64, or complex128.
  • name: A name for the operation (optional).

Returns:

A Tensor with the same type as x.

Scipy Compatibility

Equivalent to scipy.special.expit

20、tf.math.scalar_mul

Multiplies a scalar times a Tensor or IndexedSlices object.

Aliases:

tf.math.scalar_mul(
    scalar,
    x,
    name=None
)

Intended for use in gradient code which might deal with IndexedSlices objects, which are easy to multiply by a scalar but more expensive to multiply with arbitrary tensors.

Args:

  • scalar: A 0-D scalar Tensor. Must have known shape.
  • x: A Tensor or IndexedSlices to be scaled.
  • name: A name for the operation (optional).

Returns:

scalar * x of the same type (Tensor or IndexedSlices) as x.

Raises:

  • ValueError: if scalar is not a 0-D scalar.

21、tf.math.multiply

Returns x * y element-wise.

Aliases:

tf.math.multiply(
    x,
    y,
    name=None
)

NOTE: tf.multiply supports broadcasting. More about broadcasting here

Args:

  • x: A Tensor. Must be one of the following types: bfloat16, half, float32, float64, uint8, int8, uint16, int16, int32, int64, complex64, complex128.
  • y: A Tensor. Must have the same type as x.
  • name: A name for the operation (optional).

Returns:

A Tensor. Has the same type as x.

22、tf.math.logical_not

Defined in generated file: python/ops/gen_math_ops.py

Returns the truth value of NOT x element-wise.

Aliases:

tf.math.logical_not(
    x,
    name=None
)

Args:

  • x: A Tensor of type bool.
  • name: A name for the operation (optional).

Returns:

  • A Tensor of type bool.

23、tf.math.add

Defined in generated file: python/ops/gen_math_ops.py

Returns x + y element-wise.

Aliases:

tf.math.add(
    x,
    y,
    name=None
)

NOTE: math.add supports broadcasting. AddN does not. More about broadcasting here

Args:

  • x: A Tensor. Must be one of the following types: bfloat16, half, float32, float64, uint8, int8, int16, int32, int64, complex64, complex128, string.
  • y: A Tensor. Must have the same type as x.
  • name: A name for the operation (optional).

Returns:

  • A Tensor. Has the same type as x.

24、tf.math.subtract

Returns x - y element-wise.

Aliases:

tf.math.subtract(
    x,
    y,
    name=None
)

NOTE: Subtract supports broadcasting. More about broadcasting here

Args:

  • x: A Tensor. Must be one of the following types: bfloat16, half, float32, float64, uint8, int8, uint16, int16, int32, int64, complex64, complex128.
  • y: A Tensor. Must have the same type as x.
  • name: A name for the operation (optional).

Returns:

  • A Tensor. Has the same type as x.

25、tf.math.add_n

Adds all input tensors element-wise.

Aliases:

tf.math.add_n(
    inputs,
    name=None
)

Used in the guide:

Used in the tutorials:

Converts IndexedSlices objects into dense tensors prior to adding.

tf.math.add_n performs the same operation as tf.math.accumulate_n, but it waits for all of its inputs to be ready before beginning to sum. This buffering can result in higher memory consumption when inputs are ready at different times, since the minimum temporary storage required is proportional to the input size rather than the output size.

This op does not broadcast its inputs. If you need broadcasting, use tf.math.add (or the + operator) instead.

For example:

a = tf.constant([[3, 5], [4, 8]])
b = tf.constant([[1, 6], [2, 9]])
tf.math.add_n([a, b, a])  # [[7, 16], [10, 25]]

Args:

  • inputs: A list of tf.Tensor or tf.IndexedSlices objects, each with same shape and type.
  • name: A name for the operation (optional).

Returns:

  • A Tensor of same shape and type as the elements of inputs.

Raises:

  • ValueError: If inputs don't all have same shape and dtype or the shape cannot be inferred.
  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Wanderer001

ROIAlign原理

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

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

打赏作者

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

抵扣说明:

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

余额充值