1、范数tf.norm函数
计算向量、矩阵和张量的范数。
范数的定义:
1、向量
x
=
[
3
,
−
2
,
1
]
T
x = [3, -2, 1]^T
x=[3,−2,1]T的欧式范数 (Euclidean norm) 为
∣
∣
x
∣
∣
2
=
3
2
+
(
−
2
)
2
+
1
2
=
3.742
||x||_2 = \sqrt{3^2 +(-2)^2 + 1^2} = 3.742
∣∣x∣∣2=32+(−2)2+12=3.742
用于表示向量的大小,这个范数也被叫做
l
2
l_2
l2-范数。
2、为方便统一,一般将任意向量
x
x
x的
l
p
l_p
lp-范数定义为
∣
∣
x
∣
∣
p
=
∑
i
=
1
∣
x
i
∣
p
p
||x||_p = \sqrt[p]{\sum_{i=1}|x_i|^p}
∣∣x∣∣p=pi=1∑∣xi∣p
3、
l
0
l_0
l0-范数的定义:根据
l
p
l_p
lp-范数的定义,当
p
=
0
p = 0
p=0 ,我们就有了
l
0
l_0
l0-范数,即
∣
∣
x
∣
∣
0
=
∑
i
=
1
x
i
0
0
||x||_0 = \sqrt[0]{\sum_{i=1}x_i^0}
∣∣x∣∣0=0i=1∑xi0
表示向量
x
x
x中非0元素的个数
4、
l
1
l_1
l1-范数的定义: 根据
l
p
l_p
lp-范数的定义,当
p
=
1
p = 1
p=1时,任意向量
x
x
x的
l
1
l_1
l1-范数为
∣
∣
x
∣
∣
1
=
∑
i
∣
x
i
∣
||x||_1 = \sum_{i}|x_i|
∣∣x∣∣1=i∑∣xi∣
等于向量中所有元素绝对值之和。
5、
l
2
l_2
l2-范数的定义:
l
2
l_2
l2-范数表示向量(或矩阵)的元素平方和,即
∣
∣
x
∣
∣
2
=
∑
i
x
i
2
||x||_2 = \sqrt{\sum_{i}x_i^2}
∣∣x∣∣2=i∑xi2
用法:
tf.norm
(
tensor,
ord='euclidean',
axis=None,
keepdims=None,
name=None
)
参数说明:
- tensor: 要计算的张量。
- ord:指定做什么样的范数计算。支持 ‘fro’, ‘euclidean’, 1, 2, np.inf还有任意正值来计算p范数。默认是euclidean范数,即对向量而言是2-范数,对矩阵而言就是计算Frobenius范数。有如下限制:1. Frobenius 范数的声明fro对向量是未定义的;2. tensor的axis大小是2,只支持’euclidean’, ‘fro’, 1, 2, np.inf 。
- axis:如果 axis 是None (the default),输入被视为一个向量,整个tensor计算出一个范数值。如果axis是一个整数,输入被视为batch的一堆向量;如果axis是二元组(2-turple)整数,则输入视为batch的一堆矩阵。支持负的索引,比如输入的tensor在运行时,可以视为一个矩阵或者一个batch的一堆矩阵,传递axis=[-2,-1]确保计算的是矩阵范数。
示例:
# 二范数
# tf.norm(a) = tf.sqrt(tf.reduce_sum(tf.square(a)))
a = tf.ones([2, 2])
tf.norm(a)
Out[50]: <tf.Tensor: shape=(), dtype=float32, numpy=2.0>
tf.sqrt(tf.reduce_sum(tf.square(a)))
Out[51]: <tf.Tensor: shape=(), dtype=float32, numpy=2.0>
a = tf.ones([4, 28, 28, 3])
tf.norm(a)
Out[53]: <tf.Tensor: shape=(), dtype=float32, numpy=96.99484>
tf.sqrt(tf.reduce_sum(tf.square(a)))
Out[54]: <tf.Tensor: shape=(), dtype=float32, numpy=96.99484>
# 一范数
b = tf.ones([2, 2])
tf.norm(b)
Out[56]: <tf.Tensor: shape=(), dtype=float32, numpy=2.0>
tf.norm(b, ord = 2, axis = 1)
Out[57]: <tf.Tensor: shape=(2,), dtype=float32, numpy=array([1.4142135, 1.4142135], dtype=float32)>
tf.norm(b, ord = 1)
Out[59]: <tf.Tensor: shape=(), dtype=float32, numpy=4.0>
tf.norm(b, ord = 1, axis = 0)
Out[60]: <tf.Tensor: shape=(2,), dtype=float32, numpy=array([2., 2.], dtype=float32)>
tf.norm(b, ord = 1, axis = 1)
Out[61]: <tf.Tensor: shape=(2,), dtype=float32, numpy=array([2., 2.], dtype=float32)>
2、求tensor的最小值tf.reduce_min()函数、最大值tf.reduce_max()函数和平均值tf.reduce_mean()函数
求最小值、最大值和平均值函数名前面加一个reduce,为了提醒这个函数会降维。
tf.reduce_min 函数的用法:
reduce_min
(
input_tensor,
axis=None,
keep_dims=False,
name=None,
reduction_indices=None
)
参数说明:
- input_tensor:减少的张量.应该有数字类型.
- axis:要减小的尺寸.如果为None(默认),则缩小所有维度.必须在[-rank(input_tensor), rank(input_tensor))范围内.
- keep_dims:如果为true,则保留长度为1的缩小维度.
- name:操作的名称(可选).
- reduction_indices:axis的废弃的名称.
tf.reduce_max 函数的用法:
tf.math.reduce_max
(
input_tensor,
axis=None,
keepdims=False,
name=None
)
参数说明:
- input_tensor:要减少的张量.应该有数字类型.
- axis:要减小的尺寸.如果为 None(默认),则减少所有维度.必须在[-rank(input_tensor), rank(input_tensor))范围内.
- keep_dims:如果为true,则保留长度为1的减少维度.
- name:操作的名称(可选).
- reduction_indices:axis的废弃的名称.
tf.reduce_mean 函数的用法:
reduce_mean
(
input_tensor,
axis=None,
keep_dims=False,
name=None,
reduction_indices=None
)
参数说明:
- input_tensor:要减少的张量.应该有数字类型.
- axis:要减小的尺寸.如果为 None(默认),则减少所有维度.必须在[-rank(input_tensor), rank(input_tensor))范围内.
- keep_dims:如果为true,则保留长度为1的减少维度.
- name:操作的名称(可选).
- reduction_indices:axis的废弃的名称.
示例如下:
a = tf.random.normal([4, 10])
tf.reduce_min(a), tf.reduce_max(a), tf.reduce_mean(a)
Out[63]:
(<tf.Tensor: shape=(), dtype=float32, numpy=-2.031565>,
<tf.Tensor: shape=(), dtype=float32, numpy=2.3467035>,
<tf.Tensor: shape=(), dtype=float32, numpy=0.116495416>)
tf.reduce_min(a, axis = 1), tf.reduce_max(a, axis = 1), tf.reduce_mean(a, axis = 1)
Out[64]:
(<tf.Tensor: shape=(4,), dtype=float32, numpy=array([-0.8431983, -1.1093092, -2.031565 , -0.9816355], dtype=float32)>,
<tf.Tensor: shape=(4,), dtype=float32, numpy=array([1.3738796, 2.1961522, 1.9195427, 2.3467035], dtype=float32)>,
<tf.Tensor: shape=(4,), dtype=float32, numpy=array([ 0.4202599 , 0.04715918, -0.07134235, 0.0699049 ], dtype=float32)>)
3、返回最大值和最小值位置函数tf.argmax()和tf.argmin()
tf.argmax的用法:
tf.math.argmax
(
input,
axis=None,
output_type=tf.dtypes.int64,
name=None
)
在矩阵的结构中,axis可被设置为0或1,分别表示0:按列计算,1:行计算
tf.argmin的用法:
tf.argmin
(
input,
axis=None,
output_type=tf.dtypes.int64,
name=None
)
示例如下:
A=tf.constant([2,20,30,3,6])
tf.argmax(A), tf.argmin(A)
Out[66]:
(<tf.Tensor: shape=(), dtype=int64, numpy=2>,
<tf.Tensor: shape=(), dtype=int64, numpy=0>)
B=tf.constant([[2,20,30,3,6],[3,11,16,1,8],[14,45,23,5,27]])
tf.argmax(B), tf.argmin(B)
Out[68]:
(<tf.Tensor: shape=(5,), dtype=int64, numpy=array([2, 2, 0, 2, 2], dtype=int64)>,
<tf.Tensor: shape=(5,), dtype=int64, numpy=array([0, 1, 1, 1, 0], dtype=int64)>)
tf.argmax(B, axis = 1), tf.argmin(B, axis = 1)
Out[69]:
(<tf.Tensor: shape=(3,), dtype=int64, numpy=array([2, 2, 1], dtype=int64)>,
<tf.Tensor: shape=(3,), dtype=int64, numpy=array([0, 3, 3], dtype=int64)>)
4、判断矩阵是否相等tf.equal函数
equal
(
x,
y,
name=None
)
equal,相等的意思。顾名思义,就是判断,x, y 是不是相等,它的判断方法不是整体判断,
而是逐个元素进行判断,如果相等就是True,不相等,就是False。
由于是逐个元素判断,所以x,y 的维度要一致。
示例:
a = tf.constant([1, 2, 3, 4, 5])
b = tf.range(5)
tf.equal(a, b)
Out[74]: <tf.Tensor: shape=(5,), dtype=bool, numpy=array([False, False, False, False, False])>
res = tf.equal(a, b)
tf.reduce_sum(tf.cast(res, dtype = tf.int32))
Out[76]: <tf.Tensor: shape=(), dtype=int32, numpy=0>
5、在一维张量中找到唯一的元素tf.unique函数
用法:
tf.unique
(
x,
out_idx=tf.int32,
name=None
)
在一维张量中找到唯一的元素.
该操作返回一个张量 y,该张量包含所有发生在 x 中的所有唯一的元素 x,它们按照相同的顺序排序.此操作还会返回一个与 x 具有相同大小的张量 idx,包含唯一的输出 y 中 x 的每个值的索引.也就是说:
a = tf.range(5)
tf.unique(a)
Out[78]: Unique(y=<tf.Tensor: shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4])>, idx=<tf.Tensor: shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4])>)
a = tf.constant([4, 2, 2, 4, 3])
tf.unique((a))
Out[81]: Unique(y=<tf.Tensor: shape=(3,), dtype=int32, numpy=array([4, 2, 3])>, idx=<tf.Tensor: shape=(5,), dtype=int32, numpy=array([0, 1, 1, 0, 2])>)
我们还原这个张量a:
tf.gather(tf.unique((a))[0], tf.unique((a))[1])
Out[83]: <tf.Tensor: shape=(5,), dtype=int32, numpy=array([4, 2, 2, 4, 3])>