1.46 Tensor Ranks, Shapes, and Types

优质
小牛编辑
136浏览
2023-12-01

TensorFlow programs use a tensor data structure to represent all data. You can think of a TensorFlow tensor as an n-dimensional array or list. A tensor has a static type and dynamic dimensions. Only tensors may be passed between nodes in the computation graph.

Rank

In the TensorFlow system, tensors are described by a unit of dimensionality known as rank. Tensor rank is not the same as matrix rank. Tensor rank (sometimes referred to as order or degree or n-dimension) is the number of dimensions of the tensor. For example, the following tensor (defined as a Python list) has a rank of 2:

t = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

A rank two tensor is what we typically think of as a matrix, a rank one tensor is a vector. For a rank two tensor you can access any element with the syntax t[i, j]. For a rank three tensor you would need to address an element with t[i, j, k].

RankMath entityPython example
0Scalar (magnitude only)s = 483
1Vector (magnitude and direction)v = [1.1, 2.2, 3.3]
2Matrix (table of numbers)m = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
33-Tensor (cube of numbers)t = [[[2], [4], [6]], [[8], [10], [12]], [[14], [16], [18]]]
nn-Tensor (you get the idea)....

Shape

The TensorFlow documentation uses three notational conventions to describe tensor dimensionality: rank, shape, and dimension number. The following table shows how these relate to one another:

RankShapeDimension numberExample
0[]0-DA 0-D tensor. A scalar.
1[D0]1-DA 1-D tensor with shape [5].
2[D0, D1]2-DA 2-D tensor with shape [3, 4].
3[D0, D1, D2]3-DA 3-D tensor with shape [1, 4, 3].
n[D0, D1, ... Dn-1]n-DA tensor with shape [D0, D1, ... Dn-1].

Shapes can be represented via Python lists / tuples of ints, or with the @{tf.TensorShape}.

Data types

In addition to dimensionality, Tensors have a data type. You can assign any one of the following data types to a tensor:

Data typePython typeDescription
DT_FLOATtf.float3232 bits floating point.
DT_DOUBLEtf.float6464 bits floating point.
DT_INT8tf.int88 bits signed integer.
DT_INT16tf.int1616 bits signed integer.
DT_INT32tf.int3232 bits signed integer.
DT_INT64tf.int6464 bits signed integer.
DT_UINT8tf.uint88 bits unsigned integer.
DT_UINT16tf.uint1616 bits unsigned integer.
DT_STRINGtf.stringVariable length byte arrays. Each element of a Tensor is a byte array.
DT_BOOLtf.boolBoolean.
DT_COMPLEX64tf.complex64Complex number made of two 32 bits floating points: real and imaginary parts.
DT_COMPLEX128tf.complex128Complex number made of two 64 bits floating points: real and imaginary parts.
DT_QINT8tf.qint88 bits signed integer used in quantized Ops.
DT_QINT32tf.qint3232 bits signed integer used in quantized Ops.
DT_QUINT8tf.quint88 bits unsigned integer used in quantized Ops.