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

分类目录:《深入浅出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_
· 深入浅出Pytorch函数——torch.nn.init.zeros_
· 深入浅出Pytorch函数——torch.nn.init.eye_
· 深入浅出Pytorch函数——torch.nn.init.dirac_
· 深入浅出Pytorch函数——torch.nn.init.xavier_uniform_
· 深入浅出Pytorch函数——torch.nn.init.xavier_normal_
· 深入浅出Pytorch函数——torch.nn.init.kaiming_uniform_
· 深入浅出Pytorch函数——torch.nn.init.kaiming_normal_
· 深入浅出Pytorch函数——torch.nn.init.trunc_normal_
· 深入浅出Pytorch函数——torch.nn.init.orthogonal_
· 深入浅出Pytorch函数——torch.nn.init.sparse_


torch.nn.init模块中的所有函数都用于初始化神经网络参数,因此它们都在torc.no_grad()模式下运行,autograd不会将其考虑在内。

该函数对于给定的非线性函数,返回推荐的增益值。这些值如下所示:

NonlinearityGain
Linear / Identity 1 1 1
Conv1D / Conv2D / Conv3D 1 1 1
Sigmoid 1 1 1
Tanh 5 3 \frac{5}{3} 35
ReLU 2 \sqrt{2} 2
Leaky Relu 2 1 + negative_slope 2 \sqrt{\frac{2}{1+\text{negative\_slope}^2}} 1+negative_slope22
SELU 4 3 \frac{4}{3} 34

为了实现自归一化神经网络,应该使用nonlinearity='linear'而不是nonlinearity='selu'。这使得初始权重的方差为 1 N \frac{1}{N} N1,这对于在前向通道中引入稳定的固定点是必要的。相比之下,SELU的默认增益牺牲了矩形层中更稳定梯度流的归一化效应。

语法

torch.nn.init.calculate_gain(nonlinearity, param=None)

参数

  • nonlinearity:[nn.functional] 非线性函数名称
  • param:非线性函数的可选参数

实例

# leaky_relu with negative_slope=0.2
gain = nn.init.calculate_gain('leaky_relu', 0.2)  

函数实现

def calculate_gain(nonlinearity, param=None):r"""Return the recommended gain value for the given nonlinearity function.The values are as follows:================= ====================================================nonlinearity      gain================= ====================================================Linear / Identity :math:`1`Conv{1,2,3}D      :math:`1`Sigmoid           :math:`1`Tanh              :math:`\frac{5}{3}`ReLU              :math:`\sqrt{2}`Leaky Relu        :math:`\sqrt{\frac{2}{1 + \text{negative\_slope}^2}}`SELU              :math:`\frac{3}{4}`================= ====================================================.. warning::In order to implement `Self-Normalizing Neural Networks`_ ,you should use ``nonlinearity='linear'`` instead of ``nonlinearity='selu'``.This gives the initial weights a variance of ``1 / N``,which is necessary to induce a stable fixed point in the forward pass.In contrast, the default gain for ``SELU`` sacrifices the normalisationeffect for more stable gradient flow in rectangular layers.Args:nonlinearity: the non-linear function (`nn.functional` name)param: optional parameter for the non-linear functionExamples:>>> gain = nn.init.calculate_gain('leaky_relu', 0.2)  # leaky_relu with negative_slope=0.2.. _Self-Normalizing Neural Networks: https://papers.nips.cc/paper/2017/hash/5d44ee6f2c3f71b73125876103c8f6c4-Abstract.html"""linear_fns = ['linear', 'conv1d', 'conv2d', 'conv3d', 'conv_transpose1d', 'conv_transpose2d', 'conv_transpose3d']if nonlinearity in linear_fns or nonlinearity == 'sigmoid':return 1elif nonlinearity == 'tanh':return 5.0 / 3elif nonlinearity == 'relu':return math.sqrt(2.0)elif nonlinearity == 'leaky_relu':if param is None:negative_slope = 0.01elif not isinstance(param, bool) and isinstance(param, int) or isinstance(param, float):# True/False are instances of int, hence check abovenegative_slope = paramelse:raise ValueError("negative_slope {} not a valid number".format(param))return math.sqrt(2.0 / (1 + negative_slope ** 2))elif nonlinearity == 'selu':return 3.0 / 4  # Value found empirically (https://github.com/pytorch/pytorch/pull/50664)else:raise ValueError("Unsupported nonlinearity {}".format(nonlinearity))

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/44294.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

创建和运行 Ansible 临时命令

创建和运行 Ansible 临时命令 作为系统管理员,您需要在受管节点上安装软件。 请按照正文所述,创建一个名为 /home/curtis/ansible/adhoc.sh 的 shell 脚本,该脚本将使用 Ansible 临时命令在各个受管节点上安装 yum 存储库: 存储库…

mysql的隐式连接和显式连接的区别

隐式连接(Implicit Join)和显式连接(Explicit Join)是 SQL 查询中用于联结多个表的两种不同语法方式。它们的区别主要体现在语法的书写风格和可读性上。 隐式连接: 隐式连接使用逗号 , 将多个表名放在 FROM 子句中&am…

k8s的pv和pvc创建

//NFS使用PV和PVC 1、配置nfs存储 2、定义PV 实现 下图的pv和pvc测试 pv的定义 这里定义5个PV,并且定义挂载的路径以及访问模式,还有PV划分的大小 vim /pv.yamlapiVersion: v1 kind: PersistentVolume metadata:name: pv001 spec:capacity:storage: …

TiDB数据库从入门到精通系列之四:SQL 基本操作

TiDB数据库从入门到精通系列之四:SQL 基本操作 一、SQL 语言分类二、查看、创建和删除数据库三、创建、查看和删除表四、创建、查看和删除索引五、记录的增删改六、查询数据七、创建、授权和删除用户 成功部署 TiDB 集群之后,便可以在 TiDB 中执行 SQL 语…

● 1143.最长公共子序列 ● 1035.不相交的线 ● 53. 最大子序和 动态规划

1143.最长公共子序列 class Solution { public:int longestCommonSubsequence(string text1, string text2) {int res0;vector<vector<int>> dp(text1.size() 1, vector<int>(text2.size() 1, 0));for (int i 1; i < text1.size(); i) {for (int j 1…

Linux网络编程(epoll的ET模式和LT模式)

文章目录 前言一、ET模式和LT模式概念讲解1.水平触发模式&#xff08;LT&#xff0c;Level-Triggered&#xff09;2.边缘触发模式&#xff08;ET&#xff0c;Edge-Triggered&#xff09; 二、边缘触发和水平触发适用的场景总结 前言 本篇文章主要来讲解epoll的ET模式和LT模式&…

原型和原型链

好久没记了有点忘记了&#xff0c;来记录一下。 1、函数和对象的关系&#xff1a;对象都是通过函数创建的&#xff0c;函数也是一个对象。 2、原型和原型链 1.原型&#xff1a;原型分为两种 prototype&#xff1a;每一个函数都会有prototype属性&#xff0c;它指向函数的原型…

Three.js程序化3D城市建模【OpenStreetMap】

对于我在 Howest 的研究项目&#xff0c;我决定构建一个 3D 版本的 Lucas Bebber 的“交互式讲故事的动画地图路径”项目。 我将使用 OSM 中的矢量轮廓来挤出建筑物的形状并将它们添加到 3js 场景中&#xff0c;随后我将对其进行动画处理 推荐&#xff1a;用 NSDT编辑器 快速搭…

C++坦克大战源代码

源码: #include <iostream> #include <time.h> #include <windows.h>#define W 1 //上 #define S 2 //下 #define A 3 //左 #define D 4 //右 #define L 5 // 坦克有4条命void HideCursor() { //隐藏光标 …

Flask-flask系统运行后台轮询线程

对于有些flask系统&#xff0c;后台需要启动轮询线程&#xff0c;执行特定的任务&#xff0c;以下是一个简单的例子。 globals/daemon.py import threading from app.executor.ops_service import find_and_run_ops_task_todo_in_redisdef context_run_func(app, func):with …

【会议征稿信息】第二届信息学,网络与计算技术国际学术会议(ICINC2023)

2023年第二届信息学&#xff0c;网络与计算技术国际学术会议(ICINC2023) 2023 2nd International Conference on Informatics,Networking and Computing (ICINC 2023) 2023年第二届信息学&#xff0c;网络与计算技术国际学术会议(ICINC2023)将于2023年10月27-29日于中国武汉召…

首起针对国内金融企业的开源组件投毒攻击事件

简述 2023年8月9日&#xff0c;墨菲监控到用户名为 snugglejack_org (邮件地址&#xff1a;SnuggleBearrxxhotmail.com&#xff09;的用户发布到 NPM 仓库中的 ws-paso-jssdk 组件包具有发向 https://ql.rustdesk[.]net 的可疑流量&#xff0c;经过确认该组件包携带远控脚本&a…

.NET Core6.0使用NPOI导入导出Excel

一、使用NPOI导出Excel //引入NPOI包 HTML <input type"button" class"layui-btn layui-btn-blue2 layui-btn-sm" id"ExportExcel" onclick"ExportExcel()" value"导出" />JS //导出Excelfunction ExportExcel() {…

aardio开发语言Excel数据表读取修改保存实例练习

import win.ui; /*DSG{{*/ var winform win.form(text"aardio form";right759;bottom479) winform.add( buttonEnd{cls"button";text"末页";left572;top442;right643;bottom473;z6}; buttonExcelRead{cls"button";text"读取Exce…

迷路的机器人(递归回溯+动态规划两个方法实现)

题目&#xff1a; 设想有个机器人坐在一个网格的左上角&#xff0c;网格 r 行 c 列。机器人只能向下或向右移动&#xff0c;但不能走到一些被禁止的网格&#xff08;有障碍物&#xff09;。设计一种算法&#xff0c;寻找机器人从左上角移动到右下角的路径。 示例&#xff1a;…

Qt实现简单的漫游器

文章目录 Qt的OpenGL窗口GLSL的实现摄像机类的实现简单的漫游器 Qt的OpenGL窗口 Qt主要是使用QOpenGLWidget来实现opengl的功能。  QOpenGLWidget 提供了三个便捷的虚函数&#xff0c;可以重载&#xff0c;用来重新实现典型的OpenGL任务&#xff1a; paintGL&#xff1a;渲染…

数据结构,链表,单链表,双链表,循环链表,双向循环链表

链表是数据结构中的一个基本概念&#xff0c;它是线性表的一种实现。链表与顺序表&#xff08;基于数组的线性表&#xff09;不同&#xff0c;链表的元素不需要在内存中连续存储。链表的每个元素由一个节点来表示&#xff0c;每个节点都有一个数据部分和一个指针部分。 以下是…

【数据库系统】--【5】DBMS查询处理

DBMS查询处理 01查询处理概述02查询编译词法、语法分析语义分析查询重写查询优化 03查询执行算法04查询执行模型 01查询处理概述 02查询编译 词法、语法分析 语义分析 查询重写 查询优化 03查询执行算法 04查询执行模型 小结 ● 查询处理概述 ● 查询编译 词法、语法分析语义分…

2021年06月 C/C++(三级)真题解析#中国电子学会#全国青少年软件编程等级考试

第1题&#xff1a;数对 给定2到15个不同的正整数&#xff0c;你的任务是计算这些数里面有多少个数对满足&#xff1a;数对中一个数是另一个数的两倍。 比如给定1 4 3 2 9 7 18 22&#xff0c;得到的答案是3&#xff0c;因为2是1的两倍&#xff0c;4是2个两倍&#xff0c;18是9的…

音视频 FFmpeg如何查询命令帮助文档

FFmpeg如何查询命令帮助文档 一、ffmpeg/ffplay/ffprobe区别二、ffmpeg命令查看帮助文档三、ffplay命令查看帮助文档四、ffprobe命令查看帮助文档注意 一、ffmpeg/ffplay/ffprobe区别 ffmpeg:超快音视频编码器ffplay:简单媒体播放器ffprobe:简单多媒体流分析器 二、ffmpeg命令…