当前位置: 首页 > news >正文

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

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

根据Martens, J等人在《Deep learning via Hessian-free optimization》中描述的方法,将2维的输入张量或变量当做稀疏矩阵填充,其中非零元素生成自 N ( 0 , std 2 ) N(0, \text{std}^2) N(0,std2)

语法

torch.nn.init.sparse_(tensor, sparsity, std=0.01)

参数

  • tensor:[Tensor] 一个 N N N维张量torch.Tensor
  • sparsity:每列中需要被设置成零的元素比例
  • std:用于生成非零值的正态分布的标准差

返回值

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

实例

w = torch.empty(3, 5)
nn.init.sparse_(w, sparsity=0.1)

函数实现

def sparse_(tensor, sparsity, std=0.01):r"""Fills the 2D input `Tensor` as a sparse matrix, where thenon-zero elements will be drawn from the normal distribution:math:`\mathcal{N}(0, 0.01)`, as described in `Deep learning viaHessian-free optimization` - Martens, J. (2010).Args:tensor: an n-dimensional `torch.Tensor`sparsity: The fraction of elements in each column to be set to zerostd: the standard deviation of the normal distribution used to generatethe non-zero valuesExamples:>>> w = torch.empty(3, 5)>>> nn.init.sparse_(w, sparsity=0.1)"""if tensor.ndimension() != 2:raise ValueError("Only tensors with 2 dimensions are supported")rows, cols = tensor.shapenum_zeros = int(math.ceil(sparsity * rows))with torch.no_grad():tensor.normal_(0, std)for col_idx in range(cols):row_indices = torch.randperm(rows)zero_indices = row_indices[:num_zeros]tensor[zero_indices, col_idx] = 0return tensor
http://www.lryc.cn/news/130904.html

相关文章:

  • OpenCV实现BGR2BayerGB/BG格式的转换
  • Gateway网关路由以及predicates用法(项目中使用场景)
  • 深入浅出Pytorch函数——torch.nn.init.constant_
  • centos mysql8解决Access denied for user ‘root‘@‘localhost‘ (using password: YES)
  • Docker实战:Docker常用命令
  • 基于51单片机直流电机转速数码管显示控制系统
  • 小程序商品如何指定打印机
  • LLaMA-7B微调记录
  • 域名子目录发布问题(nginx、vue-element-admin、uni-app)
  • 【环境配置】Windows 10 安装 PyTorch 开发环境,以及验证 YOLOv8
  • 数学建模之“层次分析法”原理和代码详解
  • 使用IText导出复杂pdf
  • 多线程并发服务器(TCP)
  • uni-app的Vue.js实现微信小程序的紧急事件登记页面功能
  • 面试题 17.16.按摩师
  • vscode里配置C#环境并运行.cs文件
  • uniapp配置添加阿里巴巴图标icon流程步骤
  • 大模型基础02:GPT家族与提示学习
  • 算法基础课——基础算法(模板整理)
  • 如何解决使用npm出现Cannot find module ‘XXX\node_modules\npm\bin\npm-cli.js’错误
  • 【华为认证数通高级证书实验-分享篇2】
  • ui设计需要学编程吗难不难学习 优漫动游
  • 什么是线程优先级?Java中的线程优先级是如何定义和使用的?
  • 无涯教程-TensorFlow - XOR实现
  • 计算机组成与设计 Patterson Hennessy 笔记(二)MIPS 指令集
  • 【设计模式】模板方法模式(Template Method Pattern)
  • 【潮州饶平】联想 IBM x3850 x6 io主板故障 服务器维修
  • 【AIGC】 国内版聊天GPT
  • 如何在Vue中进行单元测试?什么是Vue的模块化开发?
  • Matlab编程示例3:Matlab求二次积分的编程示例