深入浅出TensorFlow2函数——tf.rank

tf.rank函数用于获取Tensor或SparseTensor的维度,返回一个int32类型的张量表示输入的秩。例如,对于三维张量t,tf.rank(t)将返回3。此函数等价于numpy中的np.ndim,提供了一种获取张量阶数的方法。

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

分类目录:《深入浅出TensorFlow2函数》总目录


语法
tf.rank(input, name=None)
参数
  • inputtf.Tensortf.SparseTensor
  • name:[可选] 操作的名称
返回值

张量input的维度,是一个int32类型的张量

实例

输入:

t = tf.constant([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]])
tf.rank(t)

输出:

<tf.Tensor: shape=(), dtype=int32, numpy=3>
函数实现
@tf_export("rank")
@dispatch.add_dispatch_support
def rank(input, name=None):
  # pylint: disable=redefined-builtin
  """Returns the rank of a tensor.
  See also `tf.shape`.
  Returns a 0-D `int32` `Tensor` representing the rank of `input`.
  For example:

  **Note**: The rank of a tensor is not the same as the rank of a matrix. The
  rank of a tensor is the number of indices required to uniquely select each
  element of the tensor. Rank is also known as "order", "degree", or "ndims."
  Args:
    input: A `Tensor` or `SparseTensor`.
    name: A name for the operation (optional).
  Returns:
    A `Tensor` of type `int32`.
  @compatibility(numpy)
  Equivalent to np.ndim
  @end_compatibility
  """
  return rank_internal(input, name, optimize=True)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

von Neumann

您的赞赏是我创作最大的动力~

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

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

打赏作者

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

抵扣说明:

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

余额充值