Tensorflow2 math常用操作
张量的二元操作可以进行广播,与numpy的广播规则一样
一、tf.math
1.abs
返回张量的绝对值,输出类型、形状与输入一致
tf.math.abs(
x,
name=None
)
a = tf.constant([[1, 2, -1], [4, -3, 2]], dtype=tf.float32)
tf.abs(a)
>>>
<tf.Tensor: id=4, shape=(2, 3), dtype=float32, numpy=
array([[1., 2., 1.],
[4., 3., 2.]], dtype=float32)>
2.accumulate_n
同add_n()
,对多个张量对应位置求和
a = tf.constant([[1, 2], [3, 4]])
b = tf.constant([[5, 0], [0, 6]])
tf.math.accumulate_n([a, b, a]) # [[7, 4], [6, 14]]
# Explicitly pass shape and type
tf.math.accumulate_n([a, b, a], shape=[2, 2], tensor_dtype=tf.int32) # [[7, 4],[6, 14]]
3.add
x和y对应元素相加,相当于+
tf.math.add(
x,
y,
name=None
)
4.add_n
同accumulate_n()
5.argmax
返回张量沿某一轴的最大值的下标
tf.math.argmax(
input,
axis=None,
output_type=tf.dtypes.int64,
name=None
)
A=tf.constant([2,20,30,3,6]) # Constant 1-D Tensor
tf.math.argmax(A) # output 2 as index 2 (A[2]) is maximum in tensor A
B=tf.constant([[2,20,30,3,6],[3,11,16,1,8],[14,45,23,5,27]])
tf.math.argmax(B,0) # [2, 2, 0, 2, 2]
tf.math.argmax(B,1) # [2, 2, 1]
axis原则
设axis=i ,则numpy沿着第i个下标变化的方向进行操作;所以,axis=0时,按列操作
6.argmin
与argmax()
类似
7.bincount
8.ceil
对每个元素向上取整
输入类型必须是float
tf.math.ceil(
x,
name=None
)
9.floor和round
floor: 向下取整
round: 最近取整
10.confusion_matrix
计算标签和输出的混淆矩阵
tf.math.confusion_matrix(
labels,
predictions,
num_classes=None,
weights=None,
dtype=tf.dtypes.int32,
name=None
)
# num_class自动计算为4+1,标签自动假定为[0, 1, 2, 3, 4]
tf.math.confusion_matrix([1, 2, 4], [2, 2, 4]) ==>
[[0 0 0 0 0]
[0 0 1 0 0]
[0 0 1 0 0]
[0 0 0 0 0]
[0 0 0 0 1]]
11. equal
x = tf.constant([2, 4])
y = tf.constant([2, 4])
tf.math.equal(x, y)
>>>
<tf.Tensor: id=96, shape=(2,), dtype=bool, numpy=array([ True, True])>
12. 逻辑操作
logical_and
、logical_or
、logical_not
、logical_xor
x = tf.constant([False, False, True, True], dtype = tf.bool)
y = tf.constant([False, True, False, True], dtype = tf.bool)
z = tf.logical_xor(x, y, name="LogicalXor")
# here z = [False True True False]
13. maximum和minimum
返回元素级 x > y ? x : y 结果
tf.math.maximum(
x,
y,
name=None
)
x = tf.constant([[1, 3], [3, 2]], dtype=tf.float32)
y = tf.constant([[2, 3], [0, 4]], dtype=tf.float32)
tf.maximum(x, y)
>>>
<tf.Tensor: id=106, shape=(2, 2), dtype=float32, numpy=
array([[2., 3.],
[3., 4.]], dtype=float32)>
14.pow
指数运算,相当于 x**y
tf.math.pow(
x,
y,
name=None
)
x = tf.constant([[2, 2], [3, 3]])
y = tf.constant([[8, 16], [2, 3]])
tf.pow(x, y) # [[256, 65536], [9, 27]]
15.reduce_max和reduce_min
张量沿某一轴的最大或最小值
设置keepdims=True,保持维度
tf.math.reduce_max(
input_tensor,
axis=None,
keepdims=False,
name=None
)
x = tf.constant([[1, 3], [5, 2]], dtype=tf.float32)
tf.reduce_max(x, axis=0)
>>>
<tf.Tensor: id=137, shape=(2,), dtype=float32, numpy=array([5., 3.], dtype=float32)>
tf.reduce_max(x, axis=0, keepdims=True)
>>>
<tf.Tensor: id=143, shape=(1, 2), dtype=float32, numpy=array([[5., 3.]], dtype=float32)>
16.reduce_mean和reduce_sum
沿张量某一轴求平均值/和
17.top_k
返回最后一维上的前K大的值和下标
tf.math.top_k(
input,
k=1,
sorted=True,
name=None
)
x = tf.constant([[1, 3], [5, 2]], dtype=tf.float32)
v, i =tf.math.top_k(x, 1)
print(v)
print(i)
>>>
tf.Tensor(
[[3.]
[5.]], shape=(2, 1), dtype=float32) tf.Tensor(
[[1]
[0]], shape=(2, 1), dtype=int32)