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

news/2025/2/12 1:25:12/

分类目录:《深入浅出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不会将其考虑在内。

该函数用 Dirac δ \text{Dirac}\delta Diracδ 函数来填充3-5维输入张量或变量,在卷积层尽可能多的保存输入通道特征。

语法

torch.nn.init.dirac_(tensor, groups=1)

参数

  • tensor:[Tensor] 一个3~5维张量torch.Tensor
  • groups:[int] conv层中的组数,默认值为1

返回值

一个torch.Tensor且参数tensor也会更新

实例

w = torch.empty(3, 16, 5, 5)
nn.init.dirac_(w)
w = torch.empty(3, 24, 5, 5)
nn.init.dirac_(w, 3)

函数实现

def dirac_(tensor, groups=1):r"""Fills the {3, 4, 5}-dimensional input `Tensor` with the Diracdelta function. Preserves the identity of the inputs in `Convolutional`layers, where as many input channels are preserved as possible. In caseof groups>1, each group of channels preserves identityArgs:tensor: a {3, 4, 5}-dimensional `torch.Tensor`groups (int, optional): number of groups in the conv layer (default: 1)Examples:>>> w = torch.empty(3, 16, 5, 5)>>> nn.init.dirac_(w)>>> w = torch.empty(3, 24, 5, 5)>>> nn.init.dirac_(w, 3)"""dimensions = tensor.ndimension()if dimensions not in [3, 4, 5]:raise ValueError("Only tensors with 3, 4, or 5 dimensions are supported")sizes = tensor.size()if sizes[0] % groups != 0:raise ValueError('dim 0 must be divisible by groups')out_chans_per_grp = sizes[0] // groupsmin_dim = min(out_chans_per_grp, sizes[1])with torch.no_grad():tensor.zero_()for g in range(groups):for d in range(min_dim):if dimensions == 3:  # Temporal convolutiontensor[g * out_chans_per_grp + d, d, tensor.size(2) // 2] = 1elif dimensions == 4:  # Spatial convolutiontensor[g * out_chans_per_grp + d, d, tensor.size(2) // 2,tensor.size(3) // 2] = 1else:  # Volumetric convolutiontensor[g * out_chans_per_grp + d, d, tensor.size(2) // 2,tensor.size(3) // 2, tensor.size(4) // 2] = 1return tensor

http://www.ppmy.cn/news/1044515.html

相关文章

element问题总结2

20.隐藏el-calendar 的多余的日子 /deep/ .el-calendar-table:not(.is-range) td.next {display: none;}/deep/ .el-calendar-table:not(.is-range) td.prev {visibility: hidden;}/deep/ .el-calendar-table td {border: 1px solid #dfe6ec;} 21.点击今天,上个月,下个月的时候…

回归预测 | MATLAB实现CSO-BP布谷鸟优化算法优化BP神经网络多输入单输出回归预测(多指标,多图)

回归预测 | MATLAB实现CSO-BP布谷鸟优化算法优化BP神经网络多输入单输出回归预测(多指标,多图) 目录 回归预测 | MATLAB实现CSO-BP布谷鸟优化算法优化BP神经网络多输入单输出回归预测(多指标,多图)效果一览…

Python基础语法入门(第二十三天)——正则表达式

正则表达式是一种文本模式,用于匹配字符串,它是由字符和特殊字符组成的模式。正则表达式可以用于验证、搜索、替换和提取字符串。其能够应用于各种编程语言和文本处理工具中,如Python、Java、JavaScript等。 正则表达式在线测试工具&#xf…

【模拟集成电路】反馈系统——基础到进阶(一)

【模拟集成电路】反馈系统——基础到进阶 前言1 概述2 反馈电路特性2.1增益灵敏度降低2.2 终端阻抗变化2.3 带宽拓展2.4 非线性减小 3 放大器分类4 反馈检测和返回机制4.1 按照检测物理量分类4.2 按照检测拓扑连接分类 5 反馈结构分析6 二端口方法7 波特方法6 麦德布鲁克方法 前…

Linux:shell脚本数组和脚本免交互

目录 一、shell数组的定义 二、定义数组的方式 (1)数组名(value1 value2 value3 value4 ...) (2)获取数组的长度 (3)获取数组下标对应的值 (4)数组的遍历 (5&#x…

数据库: MySQL安装部署、主从

单机部署 mkdir -p /opt/soft/archive cd /opt/soft/archivewget -i -c https://dev.mysql.com/get/mysql80-community-release-el7-7.noarch.rpm yum install -y mysql80-community-release-el7-7.noarch.rpm yum-config-manager --enable mysql80-community yum install -y …

【AIGC 讯飞星火 | 百度AI|ChatGPT| 】智能对比

AI智能对比 🍸 前言🍺 概念类对比🍵 讯飞🍵 百度AI🍵 chatGPT 🍹 功能类对比☕ 讯飞☕ 百度AI☕ chatGPT 🥃 可输入字数对比🥤 百度AI🥤 讯飞🥤 chatGPT &…

音视频 FFmpeg音视频处理流程

ffmpeg -i test_1920x1080.mp4 -acodec copy -vcodec libx264 -s 1280x720 test_1280x720.flv推荐一个零声学院项目课,个人觉得老师讲得不错,分享给大家: 零声白金学习卡(含基础架构/高性能存储/golang云原生/音视频/Linux内核&am…