DL学习笔记【20】nn包中的各位Simple layers

2024-02-14 12:18

本文主要是介绍DL学习笔记【20】nn包中的各位Simple layers,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

来自教程:
https://github.com/torch/nn/blob/master/doc/simple.md
这个还是比较容易明白,只简单写一下咯

Parameterized Modules

Linear 
公式如下:
y = Ax + b

SparseLinear 
稀疏的,输入的x与普通的有所不同:
x = torch.Tensor({ {1, 0.1}, {2, 0.3}, {10, 0.3}, {31, 0.2} })print(x)1.0000   0.10002.0000   0.300010.0000   0.300031.0000   0.2000
第一个参数为位置,第二个参数为该位置的数值

Bilinear 
公式如下:
\forall k: y_k = x_1 A_k x_2 + b
例子代码如下:
 input = {torch.randn(128, 10), torch.randn(128, 5)}  -- 128 input examplesmodule:forward(input)

PartialLinear 
可以只取输入数据的一部分来用,比如输入是5维,我们可以只取两个维度来计算。之后还可以恢复使用5维。
例子代码:
module = nn.PartialLinear(5, 3)  -- 5 inputs, 3 outputs
module:setPartition(torch.Tensor({2,4})) -- only compute the 2nd and 4th indices out of a total of 5 indices

Add
只学习bias

CAdd
多维bias

Mul
只学习w

CMul
多维w

Euclidean
公式:
y_j = || w_j - x ||
权重和输入为什么可以相减?

WeightedEuclidean
公式:
y_j = || c_j * (w_j - x) ||

Cosine
公式:
y_j = (x · w_j) / ( || w_j || * || x || )


identity的代码没有看懂
pred_mlp = nn.Sequential()  -- A network that makes predictions given x.
pred_mlp:add(nn.Linear(5, 4))
pred_mlp:add(nn.Linear(4, 3))xy_mlp = nn.ParallelTable() -- A network for predictions and for keeping the
xy_mlp:add(pred_mlp)        -- true label for comparison with a criterion
xy_mlp:add(nn.Identity())   -- by forwarding both x and y through the network.mlp = nn.Sequential()       -- The main network that takes both x and y.
mlp:add(xy_mlp)             -- It feeds x and y to parallel networks;
cr = nn.MSECriterion()
cr_wrap = nn.CriterionTable(cr)
mlp:add(cr_wrap)            -- and then applies the criterion.for i = 1, 100 do           -- Do a few training iterationsx = torch.ones(5)        -- Make input features.y = torch.Tensor(3)y:copy(x:narrow(1,1,3))  -- Make output label.err = mlp:forward{x,y}   -- Forward both input and output.print(err)               -- Print error from criterion.mlp:zeroGradParameters() -- Do backprop...mlp:backward({x, y})mlp:updateParameters(0.05)
end


写好多啊。。。不想写啦。。。直接复制粘贴好啦,哈哈哈哈

Modules that adapt basic Tensor methods :

Copy :a copy of the input with type casting ; 看解释好像是把input复制到output中,不太了解用处嗯。。output和input的值一样多么?

Narrow : a narrow operation over a given dimension ;

Replicate : repeats input n times along its first dimension ;

Reshape : a reshape of the inputs ;

View : a view of the inputs ;

Contiguous : contiguous of the inputs ;

Select : a select over a given dimension ;

MaskedSelect : a masked select module performs the torch.maskedSelect operation ;

Index : a index over a given dimension ;

Squeeze : squeezes the input;

Unsqueeze : unsqueeze the input, i.e., insert singleton dimension;

Transpose : transposes the input ;

Modules that adapt mathematical Tensor methods :

AddConstant : adding a constant ;

MulConstant : multiplying a constant ;

Max : a max operation over a given dimension ;

Min : a min operation over a given dimension ;

Mean : a mean operation over a given dimension ;

Sum : a sum operation over a given dimension ;

Exp : an element-wise exp operation ;

Log : an element-wise log operation ;

Abs : an element-wise abs operation ;

Power : an element-wise pow operation ;

Square : an element-wise square operation ;

Sqrt : an element-wise sqrt operation ;

Clamp : an element-wise clamp operation ;

Normalize : normalizes the input to have unit L_p norm ;

MM : matrix-matrix multiplication (also supports batches of matrices) ;

Miscellaneous Modules :

BatchNormalization : mean/std normalization over the mini-batch inputs (with an optional affine transform) ;

PixelShuffle : Rearranges elements in a tensor of shape [C*r, H, W] to a tensor of shape [C, H*r, W*r] ;

Identity : forward input as-is to output (useful with ParallelTable) ;

Dropout : masks parts of the input using binary samples from a bernoulli distribution ;

SpatialDropout : same as Dropout but for spatial inputs where adjacent pixels are strongly correlated ;

VolumetricDropout : same as Dropout but for volumetric inputs where adjacent voxels are strongly correlated ;

Padding : adds padding to a dimension ;

L1Penalty : adds an L1 penalty to an input (for sparsity) ;

GradientReversal : reverses the gradient (to maximize an objective function) ;

GPU : decorates a module so that it can be executed on a specific GPU device.

TemporalDynamicKMaxPooling : selects the k highest values in a sequence. k can be calculated based on sequence length ;

这篇关于DL学习笔记【20】nn包中的各位Simple layers的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/708476

相关文章

Unity新手入门学习殿堂级知识详细讲解(图文)

《Unity新手入门学习殿堂级知识详细讲解(图文)》Unity是一款跨平台游戏引擎,支持2D/3D及VR/AR开发,核心功能模块包括图形、音频、物理等,通过可视化编辑器与脚本扩展实现开发,项目结构含A... 目录入门概述什么是 UnityUnity引擎基础认知编辑器核心操作Unity 编辑器项目模式分类工程

Python学习笔记之getattr和hasattr用法示例详解

《Python学习笔记之getattr和hasattr用法示例详解》在Python中,hasattr()、getattr()和setattr()是一组内置函数,用于对对象的属性进行操作和查询,这篇文章... 目录1.getattr用法详解1.1 基本作用1.2 示例1.3 原理2.hasattr用法详解2.

精选20个好玩又实用的的Python实战项目(有图文代码)

《精选20个好玩又实用的的Python实战项目(有图文代码)》文章介绍了20个实用Python项目,涵盖游戏开发、工具应用、图像处理、机器学习等,使用Tkinter、PIL、OpenCV、Kivy等库... 目录① 猜字游戏② 闹钟③ 骰子模拟器④ 二维码⑤ 语言检测⑥ 加密和解密⑦ URL缩短⑧ 音乐播放

PyTorch中的词嵌入层(nn.Embedding)详解与实战应用示例

《PyTorch中的词嵌入层(nn.Embedding)详解与实战应用示例》词嵌入解决NLP维度灾难,捕捉语义关系,PyTorch的nn.Embedding模块提供灵活实现,支持参数配置、预训练及变长... 目录一、词嵌入(Word Embedding)简介为什么需要词嵌入?二、PyTorch中的nn.Em

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性:

Go学习记录之runtime包深入解析

《Go学习记录之runtime包深入解析》Go语言runtime包管理运行时环境,涵盖goroutine调度、内存分配、垃圾回收、类型信息等核心功能,:本文主要介绍Go学习记录之runtime包的... 目录前言:一、runtime包内容学习1、作用:① Goroutine和并发控制:② 垃圾回收:③ 栈和

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

重新对Java的类加载器的学习方式

《重新对Java的类加载器的学习方式》:本文主要介绍重新对Java的类加载器的学习方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、介绍1.1、简介1.2、符号引用和直接引用1、符号引用2、直接引用3、符号转直接的过程2、加载流程3、类加载的分类3.1、显示

Java学习手册之Filter和Listener使用方法

《Java学习手册之Filter和Listener使用方法》:本文主要介绍Java学习手册之Filter和Listener使用方法的相关资料,Filter是一种拦截器,可以在请求到达Servl... 目录一、Filter(过滤器)1. Filter 的工作原理2. Filter 的配置与使用二、Listen