赞
踩
作者主页(文火冰糖的硅基工坊):https://blog.csdn.net/HiWangWenBing
本文网址:https://blog.csdn.net/HiWangWenBing/article/details/119707541
目录
1.4 范数的函数说明:tf.norm(tensor, ord='euclidean', axis=None)
范数简单的讲,就是某个向量X(x0,x1,x3.....xn)的“长度”。
为了根据向量的每个元素值计算该向量的“长度”,可以有不同的规则,即所谓的范数的阶数,这些规则,称为范数的阶数,常见的阶数有:
一阶范数:使用每个向量元素的绝对值的和,来作为向量的长度。
二阶范数:使用每个向量元素的平方的和,再开根号,来作为向量的长度。
无穷阶范数:使用所有向量元素中,绝对值最大的值,作为向量的长度。
其中使用最广泛的方式就是二阶范数,原因有二:(1)二阶范畴最接近几何意义;(2)平方和开根号,可以进行求导,而一阶和无穷阶范数都时绝对值,不能求导数。
一范数:先求出所有样本点的误差值的和,然后使其最小时,则找到的拟合函数是最好的。
无穷范数:先找出所有样本点的误差值中的最大值,然后使其最小,找到的拟合函数是最好的。
二阶范数:所有样本点的误差值的平方和,然后使其最小时,则找到的拟合函数是最好的。
二阶范数就是最小二乘法求误差最小值的评判方法。
- #环境准备
- import numpy as np
- import tensorflow as tf
- print("hello world")
- print("tensorflow version:", tf.__version__)
功能说明:求张量元素的范数
参数说明:
- # 代码示例:
-
- print("源张量")
- a = tf.constant([[-1.,1.],[1.,-1.]])
- print(a)
-
- print("\n范数:全部元素,1阶范数")
- b = tf.norm(a, ord = 1) # 1阶范数:每个元素绝对值的和
- print(b)
输出 :
- 源张量
- tf.Tensor(
- [[-1. 1.]
- [ 1. -1.]], shape=(2, 2), dtype=float32)
-
- 范数:全部元素,1阶范数
- tf.Tensor(4.0, shape=(), dtype=float32)
- # 代码示例
-
- print("源张量")
- a = tf.constant([[-1.,1.],[1.,-1.]])
- print(a)
-
- print("\n范数:全部元素,2阶范数")
- b = tf.norm(a, ord = 2) # 2阶范数:每个元素平方的和,再开平方根号
- print(b)
-
- print("\n范数:默认全部元素,2阶范数")
- b = tf.norm(a)
- print(b)
输出:
- 源张量
- tf.Tensor(
- [[-1. 1.]
- [ 1. -1.]], shape=(2, 2), dtype=float32)
-
- 范数:全部元素,2阶范数
- tf.Tensor(2.0, shape=(), dtype=float32)
-
- 范数:默认全部元素,2阶范数
- tf.Tensor(2.0, shape=(), dtype=float32)
- #代码示例:
-
- print("源张量")
- a = tf.constant([[-1.,1.],[1.,-1.]])
- print(a)
-
- print("\n范数:全部元素,无穷范数")
- b = tf.norm(a, ord = np.inf) # 3阶范数:绝对值中的最大值
- print(b)
输出:
- 源张量
- tf.Tensor(
- [[-1. 1.]
- [ 1. -1.]], shape=(2, 2), dtype=float32)
-
- 范数:全部元素,无穷范数
- tf.Tensor(1.0, shape=(), dtype=float32)
- #代码示例:
-
- print("源张量")
- a = tf.constant([[-1.,1.],[2.,-2.]])
- print(a)
-
- print("\n范数:axis=0,2阶范数")
- b = tf.norm(a,ord=2, axis=0)
- print(b)
输出:
- 源张量
- tf.Tensor(
- [[-1. 1.]
- [ 2. -2.]], shape=(2, 2), dtype=float32)
-
- 范数:axis=0,2阶范数
- tf.Tensor([2.236068 2.236068], shape=(2,), dtype=float32)
- #代码示例:
-
- print("源张量")
- a = tf.constant([[-1.,1.],[2.,-2.]])
- print(a)
-
- print("\n范数:axis=1,2阶范数")
- b = tf.norm(a,ord=2, axis=1)
- print(b)
输出:
- 源张量
- tf.Tensor(
- [[-1. 1.]
- [ 2. -2.]], shape=(2, 2), dtype=float32)
-
- 范数:axis=1,2阶范数
- tf.Tensor([1.4142135 2.828427 ], shape=(2,), dtype=float32)
作者主页(文火冰糖的硅基工坊):https://blog.csdn.net/HiWangWenBing
本文网址:https://blog.csdn.net/HiWangWenBing/article/details/119707541
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。