torch.nn.functional.normalize参数说明

这篇具有很好参考价值的文章主要介绍了torch.nn.functional.normalize参数说明。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

函数定义

torch.nn.functional.normalize(input, p=2.0, dim=1, eps=1e-12, out=None)
# type: (Tensor, float, int, float, Optional[Tensor]) -> Tensor

公式为
v max ⁡ ( ∥ v ∥ p , ϵ ) \frac{v}{\max(\lVert v \rVert_p, \epsilon)} max(∥vp,ϵ)v

参数及功能

F.normalize(data, p=2/1, dim=0/1/-1) 将某一个维度除以那个维度对应的范数(默认是2范数)
input:输入的数据(tensor)
p:L2/L1_norm运算,(默认是2范数)
dim:0表示按列操作,则每列都是除以该列下平方和的开方;1表示按行操作,则每行都是除以该行下所有元素平方和的开方,-1表示按行
eps:防止分母为0

功能:将某一个维度除以那个维度对应的范数(默认是2范数),也称为标准化。

官方说明

torch.nn.functional.normalize参数说明
先放两张图,如果能看明白就不需要看下面实例解释
torch.nn.functional.normalize参数说明

二维数据实例解释

参数dim=0

import torch
import torch.nn.functional as F

a = torch.tensor([[0.0861, 0.1087, 0.0518, 0.3551],
                   [0.8067, 0.4128, 0.0592, 0.2884],
                   [0.1072, 0.4785, 0.8890, 0.3565]])
print(a.shape)
print("=============================================")
print(a)
print("=============================================")
c = F.normalize(a, dim=0)
print(c)

结果为

torch.Size([3, 4])
=============================================
tensor([[0.0861, 0.1087, 0.0518, 0.3551],
        [0.8067, 0.4128, 0.0592, 0.2884],
        [0.1072, 0.4785, 0.8890, 0.3565]])
=============================================
tensor([[0.1052, 0.1695, 0.0580, 0.6123],
        [0.9858, 0.6438, 0.0663, 0.4973],
        [0.1310, 0.7462, 0.9961, 0.6147]])

代码中针对维度0进行归一化,也就是对二维数据的列进行归一化,具体的计算细节为

0.1052 = 0.0861 0.086 1 2 + 0.806 7 2 + 0.107 2 2 0.1052=\frac{0.0861}{\sqrt{0.0861^2+0.8067^2+0.1072^2}} 0.1052=0.08612+0.80672+0.10722 0.0861
0.1695 = 0.1087 0.108 7 2 + 0.412 8 2 + 0.478 5 2 0.1695=\frac{0.1087}{\sqrt{0.1087^2+0.4128^2+0.4785^2}} 0.1695=0.10872+0.41282+0.47852 0.1087
0.0580 = 0.0518 0.051 8 2 + 0.059 2 2 + 0.889 0 2 0.0580=\frac{0.0518}{\sqrt{0.0518^2+0.0592^2+0.8890^2}} 0.0580=0.05182+0.05922+0.88902 0.0518
0.6123 = 0.3551 0.355 1 2 + 0.288 4 2 + 0.356 5 2 0.6123=\frac{0.3551}{\sqrt{0.3551^2+0.2884^2+0.3565^2}} 0.6123=0.35512+0.28842+0.35652 0.3551

参数dim=1

import torch
import torch.nn.functional as F

a = torch.tensor([[0.0861, 0.1087, 0.0518, 0.3551],
                   [0.8067, 0.4128, 0.0592, 0.2884],
                   [0.1072, 0.4785, 0.8890, 0.3565]])
print(a.shape)
print("=============================================")
print(a)
print("=============================================")
c = F.normalize(a, dim=1)
print(c)

结果为

torch.Size([3, 4])
=============================================
tensor([[0.0861, 0.1087, 0.0518, 0.3551],
        [0.8067, 0.4128, 0.0592, 0.2884],
        [0.1072, 0.4785, 0.8890, 0.3565]])
=============================================
tensor([[0.2237, 0.2825, 0.1347, 0.9230],
        [0.8467, 0.4332, 0.0621, 0.3027],
        [0.0996, 0.4447, 0.8262, 0.3313]])

代码中针对维度1进行归一化,也就是对二维数据的行进行归一化,具体的计算细节为
torch.nn.functional.normalize参数说明

参数dim=-1

与dim=2结果一致,相当于看做逆序索引

三维数据实例解释

参数dim=0

import torch
import torch.nn.functional as F

a = torch.tensor([[[0.0861, 0.1087, 0.0518, 0.3551],
                   [0.8067, 0.4128, 0.0592, 0.2884],
                   [0.1072, 0.4785, 0.8890, 0.3565]]])
print(a.shape)
print("=============================================")
print(a)
print("=============================================")
c = F.normalize(a, dim=0)
print(c)

结果为

torch.Size([1, 3, 4])
=============================================
tensor([[[0.0861, 0.1087, 0.0518, 0.3551],
         [0.8067, 0.4128, 0.0592, 0.2884],
         [0.1072, 0.4785, 0.8890, 0.3565]]])
=============================================
tensor([[[1., 1., 1., 1.],
         [1., 1., 1., 1.],
         [1., 1., 1., 1.]]])

这里作用的是维度0;维度0上只有1个通道,因此归一化之后全为1,即
torch.nn.functional.normalize参数说明

参数dim=1

a = torch.tensor([[[0.0861, 0.1087, 0.0518, 0.3551],
                   [0.8067, 0.4128, 0.0592, 0.2884],
                   [0.1072, 0.4785, 0.8890, 0.3565]]])
print(a.shape)
print("=============================================")
print(a)
print("=============================================")
c = F.normalize(a, dim=1)
print(c)

结果为

torch.Size([1, 3, 4])
=============================================
tensor([[[0.0861, 0.1087, 0.0518, 0.3551],
         [0.8067, 0.4128, 0.0592, 0.2884],
         [0.1072, 0.4785, 0.8890, 0.3565]]])
=============================================
tensor([[[0.1052, 0.1695, 0.0580, 0.6123],
         [0.9858, 0.6438, 0.0663, 0.4973],
         [0.1310, 0.7462, 0.9961, 0.6147]]])

代码中针对维度1进行归一化。维度1有3个通道,具体的计算细节为

0.1052 = 0.0861 0.086 1 2 + 0.806 7 2 + 0.107 2 2 0.1052=\frac{0.0861}{\sqrt{0.0861^2+0.8067^2+0.1072^2}} 0.1052=0.08612+0.80672+0.10722 0.0861
0.1695 = 0.1087 0.108 7 2 + 0.412 8 2 + 0.478 5 2 0.1695=\frac{0.1087}{\sqrt{0.1087^2+0.4128^2+0.4785^2}} 0.1695=0.10872+0.41282+0.47852 0.1087
0.0580 = 0.0518 0.051 8 2 + 0.059 2 2 + 0.889 0 2 0.0580=\frac{0.0518}{\sqrt{0.0518^2+0.0592^2+0.8890^2}} 0.0580=0.05182+0.05922+0.88902 0.0518
0.6123 = 0.3551 0.355 1 2 + 0.288 4 2 + 0.356 5 2 0.6123=\frac{0.3551}{\sqrt{0.3551^2+0.2884^2+0.3565^2}} 0.6123=0.35512+0.28842+0.35652 0.3551

参数dim=2

a = torch.tensor([[[0.0861, 0.1087, 0.0518, 0.3551],
                   [0.8067, 0.4128, 0.0592, 0.2884],
                   [0.1072, 0.4785, 0.8890, 0.3565]]])
print(a.shape)
print("=============================================")
print(a)
print("=============================================")
c = F.normalize(a, dim=2)
print(c)

结果为

torch.Size([1, 3, 4])
=============================================
tensor([[[0.0861, 0.1087, 0.0518, 0.3551],
         [0.8067, 0.4128, 0.0592, 0.2884],
         [0.1072, 0.4785, 0.8890, 0.3565]]])
=============================================
tensor([[[0.2237, 0.2825, 0.1347, 0.9230],
         [0.8467, 0.4332, 0.0621, 0.3027],
         [0.0996, 0.4447, 0.8262, 0.3313]]])

这里作用的是维度2,可以认为维度2有4个通道,计算细节为:
torch.nn.functional.normalize参数说明

参数dim=-1

与dim=2结果一致,相当于看做逆序索引

参考博文及感谢

部分内容参考以下链接,这里表示感谢 Thanks♪(・ω・)ノ
参考博文1 官方文档
https://pytorch.org/docs/stable/generated/torch.nn.functional.normalize.html
参考博文2 Pytorch中关于F.normalize计算理解
https://www.jb51.net/article/274086.htm
参考博文3 【Pytorch】F.normalize计算理解
https://blog.csdn.net/lj2048/article/details/118115681文章来源地址https://www.toymoban.com/news/detail-451929.html

到了这里,关于torch.nn.functional.normalize参数说明的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 深入浅出Pytorch函数——torch.nn.init.xavier_normal_

    分类目录:《深入浅出Pytorch函数》总目录 相关文章: · 深入浅出Pytorch函数——torch.nn.init.calculate_gain · 深入浅出Pytorch函数——torch.nn.init.uniform_ · 深入浅出Pytorch函数——torch.nn.init.normal_ · 深入浅出Pytorch函数——torch.nn.init.constant_ · 深入浅出Pytorch函数——torch.nn.init.ones_ ·

    2024年02月12日
    浏览(29)
  • 深入浅出Pytorch函数——torch.softmax/torch.nn.functional.softmax

    分类目录:《深入浅出Pytorch函数》总目录 相关文章: · 机器学习中的数学——激活函数:Softmax函数 · 深入浅出Pytorch函数——torch.softmax/torch.nn.functional.softmax · 深入浅出Pytorch函数——torch.nn.Softmax 将Softmax函数应用于沿 dim 的所有切片,并将重新缩放它们,使元素位于 [ 0 ,

    2024年02月15日
    浏览(45)
  • Pytorch计算余弦相似度距离——torch.nn.CosineSimilarity函数中的dim参数使用方法

    前言 一、官方函数用法 二、实验验证 1.计算高维数组中各个像素位置的余弦距离 2.验证高维数组中任意一个像素位置的余弦距离 总结 现在要使用Pytorch中自带的 torch.nn. CosineSimilarity函数计算两个高维特征图(B,C,H,W)中各个像素位置的特征相似度,即特征图中的每个像素位置上

    2024年02月13日
    浏览(28)
  • torch.normal()函数

    torch.normal()函数:返回一个张量;是从一个给定mean(均值),std(方差)的正态分布中抽取随机数。mean和std都是属于张量类型的; 参数: mean:均值; std:标准差; out:输出张量; size:张量的大小; 源码参数:

    2024年02月16日
    浏览(37)
  • PyTorch中的torch.nn.Linear函数解析

    torch.nn是包含了构筑神经网络结构基本元素的包,在这个包中,可以找到任意的神经网络层。这些神经网络层都是nn.Module这个大类的子类。torch.nn.Linear就是神经网络中的线性层,可以实现形如y=Xweight^T+b的加和功能。 nn.Linear():用于设置网络中的全连接层,需要注意的是全连接

    2024年02月16日
    浏览(29)
  • 详解Pytorch中的torch.nn.MSELoss函,包括对每个参数的分析!

    一、函数介绍 Pytorch中MSELoss函数的接口声明如下,具体网址可以点这里。 torch.nn.MSELoss(size_average=None, reduce=None, reduction=‘mean’) 该函数 默认用于计算两个输入对应元素差值平方和的均值 。具体地,在深度学习中,可以使用该函数用来计算两个特征图的相似性。 二、使用方式

    2023年04月19日
    浏览(33)
  • 深入浅出Pytorch函数——torch.nn.Module

    分类目录:《深入浅出Pytorch函数》总目录 Pytorch中所有网络的基类,我们的模型也应该继承这个类。 Modules 也可以包含其它 Modules ,允许使用树结构嵌入他们,我们还可以将子模块赋值给模型属性。 语法 方法 torch.nn.Module.apply 实例 通过上面方式赋值的 submodule 会被注册,当调

    2024年02月12日
    浏览(44)
  • 深入浅出Pytorch函数——torch.nn.Softmax

    分类目录:《深入浅出Pytorch函数》总目录 相关文章: · 机器学习中的数学——激活函数:Softmax函数 · 深入浅出Pytorch函数——torch.softmax/torch.nn.functional.softmax · 深入浅出Pytorch函数——torch.nn.Softmax 将Softmax函数应用于 n n n 维输入张量,重新缩放它们,使得 n n n 维输出张量的

    2024年02月15日
    浏览(36)
  • 深入浅出Pytorch函数——torch.nn.Linear

    分类目录:《深入浅出Pytorch函数》总目录 对输入数据做线性变换 y = x A T + b y=xA^T+b y = x A T + b 语法 参数 in_features :[ int ] 每个输入样本的大小 out_features :[ int ] 每个输出样本的大小 bias :[ bool ] 若设置为 False ,则该层不会学习偏置项目,默认值为 True 变量形状 输入变量:

    2024年02月12日
    浏览(27)
  • 05-python之函数-函数的定义/函数的参数/函数返回值/函数说明文档/函数的嵌套使用/函数变量的作用域

    对应输出如上,没有使用len()函数,对应的子算出字符的长度,但是代码整体写的就很别扭。代码过于重复,代码中唯一不一样的地方就是被统计的字符串不同。同时对应的,代码整体也就会比较低效。可以使用函数,优化过程,先定义函数。 同样的输出,效果一样,两者

    2024年01月19日
    浏览(46)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包