• 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的元素最大整数。

    1. tf.math.floor(
    2.     x,
    3.     name=None
    4. )

    参数:

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

    返回值:

    • 与x类型相同的张量。

    2、tf.log

    计算x元素的自然对数。

    1. tf.math.log(
    2.     x,
    3.     name=None
    4. )

    例如,\tiny y=log_e x

    参数:

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

    返回值:

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

    3、tf.reduce_mean

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

    1. tf.math.reduce_mean(
    2.     input_tensor,
    3.     axis=None,
    4.     keepdims=False,
    5.     name=None
    6. )

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

    例如:

    1. x = tf.constant([[1., 1.], [2., 2.]])
    2. tf.reduce_mean(x)  # 1.5
    3. tf.reduce_mean(x, 0)  # [1.5, 1.5]
    4. 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_张量的攻击类型推断,例如:

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

    4、tf.reduce_sum

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

    1. tf.math.reduce_sum(
    2.     input_tensor,
    3.     axis=None,
    4.     keepdims=None,
    5.     name=None,
    6.     reduction_indices=None,
    7.     keep_dims=None
    8. )

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

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

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

    例:

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

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

    1. tf.add_n(
    2.     inputs,
    3.     name=None
    4. )

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

     例如:

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

    6、tf.math.top_k

    tf.math.top_k

    1. tf.math.top_k(
    2.     input,
    3.     k=1,
    4.     sorted=True,
    5.     name=None
    6. )

    查找最后一个维度的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

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

    1. tf.math.argmax(
    2.     input,
    3.     axis=None,
    4.     name=None,
    5.     dimension=None,
    6.     output_type=tf.dtypes.int64
    7. )

    参数:

    • 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类型的张量。

    例:

    1. import tensorflow as tf
    2. x = tf.constant([[1., 2., 6], [6., 2., 6]])
    3. xShape = tf.shape(x)
    4. z1 = tf.arg_max(x, 1) # 沿axis=0操作
    5. with tf.Session() as sess:
    6. xShapeValue, d1 = sess.run([xShape, z1])
    7. print('shape= %s' % (xShapeValue))
    8. print(d1)

    8、tf.math.greater_equal

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

    1. tf.math.greater_equal(
    2.     x,
    3.     y,
    4.     name=None
    5. )

    参数:

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

    返回值:

    • bool类型的张量。

    9、tf.math.pow

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

    1. tf.math.pow(
    2. x,
    3. y,
    4. name=None
    5. )

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

    1. x = tf.constant([[2, 2], [3, 3]])
    2. y = tf.constant([[8, 16], [2, 3]])
    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。

    1. tf.math.multiply(
    2.     x,
    3.     y,
    4.     name=None
    5. )

    参数:

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

    返回值:

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

    11、tf.math.sqrt

    计算x元素的平方根。

    1. tf.math.sqrt(
    2.     x,
    3.     name=None
    4. )

    参数:

    • 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元素的真值。

    1. tf.math.logical_or(
    2.     x,
    3.     y,
    4.     name=None
    5. )

    参数:

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

    返回值:

    • bool类型的张量。

    13、tf.math.truediv

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

    1. tf.math.truediv(
    2.     x,
    3.     y,
    4.     name=None
    5. )

    注意:最好使用遵循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)元素的真值。

    1. tf.math.less(
    2.     x,
    3.     y,
    4.     name=None
    5. )

    参数:

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

    返回值:

    • bool类型的张量。

    15、tf.math.count_nonzero

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

    1. tf.math.count_nonzero(
    2.     input,
    3.     axis=None,
    4.     keepdims=None,
    5.     dtype=tf.dtypes.int64,
    6.     name=None
    7. )

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

    例:

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

    例:

    1. x = tf.constant(["", "a", "  ", "b", ""])
    2. 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

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

    1. tf.math.scalar_mul(
    2.     scalar,
    3.     x,
    4.     name=None
    5. )

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

    参数:

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

    返回值:

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

    17、tf.math.conj

    返回复数的复共轭。

    1. tf.math.conj(
    2.     x,
    3.     name=None
    4. )

    返回复数的复共轭。给定一个复数张量输入,这个操作返回一个复数张量,它是输入中每个元素的复共轭。输入的复数必须是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

    1. tf.math.floormod(
    2.     x,
    3.     y,
    4.     name=None
    5. )

    的确,这遵循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:

    1. tf.math.sigmoid(
    2.     x,
    3.     name=None
    4. )

    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:

    1. tf.math.scalar_mul(
    2.     scalar,
    3.     x,
    4.     name=None
    5. )

    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:

    1. tf.math.multiply(
    2.     x,
    3.     y,
    4.     name=None
    5. )

    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:

    1. tf.math.logical_not(
    2.     x,
    3.     name=None
    4. )

    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:

    1. tf.math.add(
    2.     x,
    3.     y,
    4.     name=None
    5. )

    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:

    1. tf.math.subtract(
    2.     x,
    3.     y,
    4.     name=None
    5. )

    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:

    1. tf.math.add_n(
    2.     inputs,
    3.     name=None
    4. )

    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:

    1. a = tf.constant([[3, 5], [4, 8]])
    2. b = tf.constant([[1, 6], [2, 9]])
    3. 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.
  • 相关阅读:
    弗洛伊德算法(Floyd-Warshall)
    信息系统项目管理教程(第4版):第二章 信息技术及其发展
    什么是智能合约?新手入门指南
    RUM之SPA应用最佳可观测实践
    多线程涉及的其它知识(死锁(等待唤醒机制),内存可见性问题以及定时器)
    1103 Integer Factorization
    Godot拉伸设置
    uniapp 微信小程序分享功能(打开右上角分享)
    【Linux系统编程】——深度理解5种IO模型
    Poetry:Python依赖管理和打包工具【最好用的Python虚拟环境Poetry】【Poetry、conda、vscode混合使用】
  • 原文地址:https://blog.csdn.net/weixin_36670529/article/details/97396827