当前位置:   article > 正文

[TensorFlow系列-18]:TensorFlow基础 - 张量的范数_张量的无穷范数

张量的无穷范数

作者主页(文火冰糖的硅基工坊):https://blog.csdn.net/HiWangWenBing

本文网址:https://blog.csdn.net/HiWangWenBing/article/details/119707541


目录

第1章 什么是范数

1.1 常见的范数与定义

1.2 范数的物理意义

1.3 代码演示的前置条件

1.4 范数的函数说明:tf.norm(tensor, ord='euclidean', axis=None)

第3章 多阶范数

3.1 一阶范数

3.2 二阶范数

3.3 无穷阶范数

第4章 指定维度方向上的范数

4.1 axis =0方向上的范数

4.2 axis =1方向上的范数



第1章 什么是范数

1.1 常见的范数与定义

范数简单的讲,就是某个向量X(x0,x1,x3.....xn)的“长度”。

为了根据向量的每个元素值计算该向量的“长度”,可以有不同的规则,即所谓的范数的阶数,这些规则,称为范数的阶数,常见的阶数有:

 

一阶范数:使用每个向量元素的绝对值的和,来作为向量的长度。

二阶范数:使用每个向量元素的平方的和,再开根号,来作为向量的长度。

无穷阶范数:使用所有向量元素中,绝对值最大的值,作为向量的长度。

其中使用最广泛的方式就是二阶范数,原因有二:(1)二阶范畴最接近几何意义;(2)平方和开根号,可以进行求导,而一阶和无穷阶范数都时绝对值,不能求导数。

1.2 范数的物理意义

一范数:先求出所有样本点的误差值的和,然后使其最小时,则找到的拟合函数是最好的。

无穷范数:先找出所有样本点的误差值中的最大值,然后使其最小,找到的拟合函数是最好的。

二阶范数:所有样本点的误差值的平方和,然后使其最小时,则找到的拟合函数是最好的。

二阶范数就是最小二乘法求误差最小值的评判方法。

1.3 代码演示的前置条件

  1. #环境准备
  2. import numpy as np
  3. import tensorflow as tf
  4. print("hello world")
  5. print("tensorflow version:", tf.__version__)

1.4 范数的函数说明:tf.norm(tensor, ord='euclidean', axis=None)

功能说明:求张量元素的范数

参数说明:

  • tensor:张量
  • ord:范数阶数,如1,2,np.inf (无穷)
  • axis:

第3章 多阶范数

3.1 一阶范数

  1. # 代码示例:
  2. print("源张量")
  3. a = tf.constant([[-1.,1.],[1.,-1.]])
  4. print(a)
  5. print("\n范数:全部元素,1阶范数")
  6. b = tf.norm(a, ord = 1) # 1阶范数:每个元素绝对值的和
  7. print(b)

输出 :

  1. 源张量
  2. tf.Tensor(
  3. [[-1. 1.]
  4. [ 1. -1.]], shape=(2, 2), dtype=float32)
  5. 范数:全部元素,1阶范数
  6. tf.Tensor(4.0, shape=(), dtype=float32)

3.2 二阶范数

  1. # 代码示例
  2. print("源张量")
  3. a = tf.constant([[-1.,1.],[1.,-1.]])
  4. print(a)
  5. print("\n范数:全部元素,2阶范数")
  6. b = tf.norm(a, ord = 2) # 2阶范数:每个元素平方的和,再开平方根号
  7. print(b)
  8. print("\n范数:默认全部元素,2阶范数")
  9. b = tf.norm(a)
  10. print(b)

输出:

  1. 源张量
  2. tf.Tensor(
  3. [[-1. 1.]
  4. [ 1. -1.]], shape=(2, 2), dtype=float32)
  5. 范数:全部元素,2阶范数
  6. tf.Tensor(2.0, shape=(), dtype=float32)
  7. 范数:默认全部元素,2阶范数
  8. tf.Tensor(2.0, shape=(), dtype=float32)

3.3 无穷阶范数

  1. #代码示例:
  2. print("源张量")
  3. a = tf.constant([[-1.,1.],[1.,-1.]])
  4. print(a)
  5. print("\n范数:全部元素,无穷范数")
  6. b = tf.norm(a, ord = np.inf) # 3阶范数:绝对值中的最大值
  7. print(b)

输出:

  1. 源张量
  2. tf.Tensor(
  3. [[-1. 1.]
  4. [ 1. -1.]], shape=(2, 2), dtype=float32)
  5. 范数:全部元素,无穷范数
  6. tf.Tensor(1.0, shape=(), dtype=float32)

第4章 指定维度方向上的范数

4.1 axis =0方向上的范数

  1. #代码示例:
  2. print("源张量")
  3. a = tf.constant([[-1.,1.],[2.,-2.]])
  4. print(a)
  5. print("\n范数:axis=0,2阶范数")
  6. b = tf.norm(a,ord=2, axis=0)
  7. print(b)

输出:

  1. 源张量
  2. tf.Tensor(
  3. [[-1. 1.]
  4. [ 2. -2.]], shape=(2, 2), dtype=float32)
  5. 范数:axis=0,2阶范数
  6. tf.Tensor([2.236068 2.236068], shape=(2,), dtype=float32)

4.2 axis =1方向上的范数

  1. #代码示例:
  2. print("源张量")
  3. a = tf.constant([[-1.,1.],[2.,-2.]])
  4. print(a)
  5. print("\n范数:axis=1,2阶范数")
  6. b = tf.norm(a,ord=2, axis=1)
  7. print(b)

输出:

  1. 源张量
  2. tf.Tensor(
  3. [[-1. 1.]
  4. [ 2. -2.]], shape=(2, 2), dtype=float32)
  5. 范数:axis=1,2阶范数
  6. tf.Tensor([1.4142135 2.828427 ], shape=(2,), dtype=float32)

 作者主页(文火冰糖的硅基工坊):https://blog.csdn.net/HiWangWenBing

本文网址:https://blog.csdn.net/HiWangWenBing/article/details/119707541

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/798378
推荐阅读
相关标签
  

闽ICP备14008679号