形状相关 Tensor Shape
tf.reshape
1 | tf.reshape( |
给定一个tensor,该函数返回一个与输入tensor具有相同值且形状为shape的张量。
如果shape中有一个特殊值-1,则该维度的大小通过整个tensor的尺寸计算得到,特殊情况下shape为[-1]时则将tensor摊平成1-D。shape最多有一个值为-1。
如果shape是一维或更高维度,该函数返回的形状为shape的张量中的值由tensor的值填充,在这种情况下,由shape决定的元素个数必须与输入tensor中的元素个数一致。
Ref tf.reshape
Args:
- tensor: 一个
Tensor张量 。 - shape: 一个
Tensor张量,必须是int32或int64类型,决定了输出张量的形状。 - name: 操作的名字,可选参数。
Returns:
一个 Tensor张量,和输入tensor具有相同的数据类型。
tf.expand_dims
1 | tf.expand_dims( |
给定一个张量input,该函数在input的形状中维度索引为axis的位置插入一个维度为1的维度,维度索引axis从0开始,如果你为axis指定一个负数,则其从后往前数。
这个操作在你想要在某个元素上加入一个batch维度时非常有用。例如有一个形状为[height, width, channels]的图像,通过expand_dims(image, 0)可以使其成为一个只有一张图片的batch,其形状为[1, height, width, channels]。
Ref tf.expand_dims
Args:
- input: 一个张量.
- axis: 0-D (标量),指定扩展张量
input形状的维度索引,必须位于[-rank(input) - 1, rank(input)]之间。 - name: 输出张量的名字。
- dim: 0-D (标量),等价于
axis,将会被废弃。
Returns:
- 和输入张量
input具有相同数据的张量,但其形状多了一个大小为1的维度。
tf.squeeze
1 | tf.squeeze(input, |
给定一个张量input,该函数返回一个在移除所有大小为1的维度后,与输入张量具有相同数据类型的张量。如果你不想移除所有大小为1的维度,可以通过指定axis来移除部分大小为1的维度。
Ref tf.squeeze
Args:
- input: 一个张量。A
Tensor. Theinputto squeeze. - axis: 一个可选的
ints列表,默认为[]。如果指定,则只压缩列表中的维度,维度索引从0开始。当压缩大小不是1的维度时会产生错误。必须在[-rank(input), rank(input))之间。 - name: 操作的名字,可选参数。
- squeeze_dims: 废弃的参数,现在改为
axis。
Returns:
- 和输入张量
input具有相同类型、相同数据的张量,但是有一个或多个大小为1的维度被移除。
tf.transpose
1 | tf.transpose( |
对张量a进行转秩操作,根据参数perm重新排列各个维度。
返回的张量的第i个维度对应输入张量的第perm[i]个维度,如果perm参数没有指定,其默认会被设为 (n-1…0),其中n是输入张量的秩。所以默认情况下,该操作进行一个常规的矩阵转秩操作。如果参数conjugate设置为True,a.dtype 是 complex64 或者 complex128 ,则返回输入张量的共轭转秩。
Ref tf.transpose
Args:
- a: A
Tensor. - perm: A permutation of the dimensions of
a. - name: A name for the operation (optional).
- conjugate: Optional bool. Setting it to
Trueis mathematically equivalent to tf.conj(tf.transpose(input)).
Returns:
A transposed Tensor.
Numpy Compatibility
In numpy transposes are memory-efficient constant time operations as they simply return a new view of the same data with adjusted strides.
TensorFlow does not support strides, so transpose returns a new tensor with the items permuted.
tf.concat
tf.stack
tf.tile
特殊矩阵
tf.one_hot
tf.eye
运算 Arithmetic Operators
tf.add
tf.subtract
tf.tensordot
tf.matmul
tf.multiply
1 | tf.multiply( |
Returns x * y element-wise.
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 asx. - name: A name for the operation (optional).
Returns:
A Tensor. Has the same type as x.
Ref tf.multiply
tf.scalar_mul
tf.div
tf.divide
tf.truediv
tf.floordiv
tf.realdiv
tf.truncatediv
tf.floor_div
tf.truncatemod
tf.floormod
tf.mod
tf.cross
tf.einsum
1 | tf.einsum(equation, *inputs) |
Ref tf.einsum