数据统计
功能 | 函数代码 |
---|
范数计算 | tf.norm(x,ord) |
最值 | tf.reduce_max(x,axis)、 tf.reduce_min(x,axis) |
均值 | tf.reduce_mean(x,axis) |
和 | tf.reduce_sum(x,axis) |
返回统计最值索引 | tf.argmax(x, axis),tf.argmin(x, axis) |
范数
- tf.norm(x,ord)
- ord 指定为 1、 2 时计算 L1、 L2 范数,指定为 np.inf 时计算∞ −范数
- L1范数:定义为向量𝒙的所有元素绝对值之和
- L2范数:定义为向量𝒙的所有元素的平方和,再开根号
- L3范数:定义为向量𝒙的所有元素的l立方和,再开立方根
- ∞ (无穷大)范数,定义为向量𝒙的所有元素绝对值的最大值
import tensorflow as tf
import numpy as np
x = tf.ones([2,2])
print(tf.norm(x,ord=1))
print(tf.norm(x,ord=2))
print(tf.norm(x,ord=3))
print(tf.norm(x,ord=np.inf))
out:
tf.Tensor(4.0, shape=(), dtype=float32)
tf.Tensor(2.0, shape=(), dtype=float32)
tf.Tensor(1.587401, shape=(), dtype=float32)
tf.Tensor(1.0, shape=(), dtype=float32)
简单统计
- tf.reduce_*(x,axis)
- 当不指定 axis 参数时, tf.reduce_*函数会求解出全局元素的最大、最小、 均值、和等
数据
最值
- tf.reduce_max(x,axis)、 tf.reduce_min(x,axis)
- 返回axis上的最值
均值
- tf.reduce_mean(x,axis)
- 返回axis上的最值
和
- tf.reduce_sum(x,axis)
- 返回axis上的最值
返回统计最值索引
- tf.argmax(x, axis),tf.argmin(x, axis)
- 可以求解在 axis 轴上, x 的最大值、 最小值所在的索引
import tensorflow as tf
x = tf.random.normal([4, 10])
print(x)
print(tf.reduce_max(x,axis=1))
print(tf.reduce_min(x,axis=1))
print(tf.reduce_sum(x,axis=1))
print(tf.reduce_mean(x,axis=1))
print(tf.argmax(x, axis=1))
print(tf.argmin(x, axis=1))
out:
tf.Tensor(
[[ 1.6965815 0.09555305 0.33692268 -0.7822668 -1.0054542 -1.334124
0.7095199 -2.8712015 1.8947812 -0.48956573]
[-0.56082326 -1.5098412 0.27639452 1.3449372 -2.0256445 1.2122011
0.50152576 0.03570785 0.52847946 0.6525247 ]
[ 1.6948462 0.56346595 -0.56629235 0.31268176 0.96957785 1.2716382
-0.04530253 2.1147048 0.46754405 2.5528266 ]
[-0.01668303 -1.3695923 0.12339069 0.8491427 0.55647653 -1.2386454
1.9543849 -0.68476975 1.7511108 -0.31127346]], shape=(4, 10), dtype=float32)
tf.Tensor([1.8947812 1.3449372 2.5528266 1.9543849], shape=(4,), dtype=float32)
tf.Tensor([-2.8712015 -2.0256445 -0.56629235 -1.3695923 ], shape=(4,), dtype=float32)
tf.Tensor([-1.749254 0.45546162 9.3356905 1.6135417 ], shape=(4,), dtype=float32)
tf.Tensor([-0.1749254 0.04554616 0.9335691 0.16135417], shape=(4,), dtype=float32)
tf.Tensor([8 3 9 6], shape=(4,), dtype=int64)
tf.Tensor([7 4 2 1], shape=(4,), dtype=int64)