torch学习(二) nn类结构-Module
2015-10-21 23:36
666 查看
Module
|-Container
|-Sequential
|-Parallel
|-Concat
|-DepthConcat
安装使用torch以后,接下来就可以开始构建模型和训练了,在这之前,一定要先花半个小时的时间了解一下torch里面nn部分使用到的基本类型,否则容易混乱。
一、Module
Module是神经网络的基本组成部分,作为一个抽象类,可以通过定义成员函数实现不同的神经网络结构,nn.*都是Module的子类。
成员变量有output和gradInput,成员函数包括:
(一)、[output] forward(input)
根据input计算更新output,不建议重写该函数
1.调用updateOutput(input)
对module的输出进行处理,可以重写该函数
(二)、[gradInput] backward(input, gradOutput)
在使用backward之前需要首先调用forward,不建议重写,可以重写它调用的两个子函数
input:该层的输入
gradOutput:对该层输出的梯度
gradInput:对该层输入的梯度
1.updateGradInput(input, gradOutput)
计算相对于输入input的梯度,更新gradInput
2. accGradParameters(input, gradOutput, scale)
计算相对于该Module的参数的梯度
gradParameters:对该层参数的梯度,比如sigmoid层没有参数就没有该变量
scale: gradParameters乘以系数scale,然后累加
3.zeroGradParameters()
重置gradParameters为0
4.updateParameters(learningRate)
根据backward()累加的gradParameters,更新Module的参数
parameters = parameters - learningRate * gradients_wrt_parameters
5.accUpdateCradParameters(input,gradOutput,learningRate)
累积参数梯度,同时更新参数
......
二、Container
父类Module,子类包括Sequential、Parallel、Concat等
新增成员函数包括:
1.add(module)
增加Module
2.get(index)
根据下标获取Module
3.size()
包含的modules
三、Sequential
以feed-forward全连接的形式组织网络
1.remove([index])
删除第index层
2.insert(module,[index])
在中间插入层
四、Parallel
module = Parallel(inputDimension,outputDimension)
Creates a container module that applies its ith child module to the ith slice of the input Tensor by using select on dimension inputDimension.
It concatenates the results of its contained modules together along dimensionoutputDimension.
五、Concat
module = nn.Concat(dim)
Concat concatenates the output of one layer of "parallel" modules along the provided dimension dim: they take the same inputs, and their output is concatenated.
六、DepthConcat
DepthConcat concatenates the output of one layer of "parallel" modules along the provided dimension dim: they take the same inputs, and their output is concatenated. For dimensions other than dim having different sizes, the smaller tensors are copied in
the center of the output tensor, effectively padding the borders with zeros.
参考:
https://github.com/torch/nn/blob/master/README.md
|-Container
|-Sequential
|-Parallel
|-Concat
|-DepthConcat
安装使用torch以后,接下来就可以开始构建模型和训练了,在这之前,一定要先花半个小时的时间了解一下torch里面nn部分使用到的基本类型,否则容易混乱。
一、Module
Module是神经网络的基本组成部分,作为一个抽象类,可以通过定义成员函数实现不同的神经网络结构,nn.*都是Module的子类。
成员变量有output和gradInput,成员函数包括:
(一)、[output] forward(input)
根据input计算更新output,不建议重写该函数
1.调用updateOutput(input)
对module的输出进行处理,可以重写该函数
(二)、[gradInput] backward(input, gradOutput)
在使用backward之前需要首先调用forward,不建议重写,可以重写它调用的两个子函数
input:该层的输入
gradOutput:对该层输出的梯度
gradInput:对该层输入的梯度
1.updateGradInput(input, gradOutput)
计算相对于输入input的梯度,更新gradInput
2. accGradParameters(input, gradOutput, scale)
计算相对于该Module的参数的梯度
gradParameters:对该层参数的梯度,比如sigmoid层没有参数就没有该变量
scale: gradParameters乘以系数scale,然后累加
3.zeroGradParameters()
重置gradParameters为0
4.updateParameters(learningRate)
根据backward()累加的gradParameters,更新Module的参数
parameters = parameters - learningRate * gradients_wrt_parameters
5.accUpdateCradParameters(input,gradOutput,learningRate)
累积参数梯度,同时更新参数
......
二、Container
父类Module,子类包括Sequential、Parallel、Concat等
新增成员函数包括:
1.add(module)
增加Module
2.get(index)
根据下标获取Module
3.size()
包含的modules
三、Sequential
以feed-forward全连接的形式组织网络
1.remove([index])
删除第index层
2.insert(module,[index])
在中间插入层
四、Parallel
module = Parallel(inputDimension,outputDimension)
Creates a container module that applies its ith child module to the ith slice of the input Tensor by using select on dimension inputDimension.
It concatenates the results of its contained modules together along dimensionoutputDimension.
五、Concat
module = nn.Concat(dim)
Concat concatenates the output of one layer of "parallel" modules along the provided dimension dim: they take the same inputs, and their output is concatenated.
六、DepthConcat
DepthConcat concatenates the output of one layer of "parallel" modules along the provided dimension dim: they take the same inputs, and their output is concatenated. For dimensions other than dim having different sizes, the smaller tensors are copied in
the center of the output tensor, effectively padding the borders with zeros.
参考:
https://github.com/torch/nn/blob/master/README.md
相关文章推荐
- IIS URL Rewrite redirect from one Domain to another
- Lua面向对象编程
- 最最简单的~WordCount¬
- 流媒体相关技术了解入门
- Shodan搜索引擎介绍
- Java中的内存泄漏
- 矩阵输出
- 深入理解Android之Gradle
- 成员资格、授权 – ASP.NET MVC 4 系列
- 147_多线程(线程间通信-生产者消费者JDK5.0升级版)
- ios面试题集锦(一)
- MyEclipse发布项目时报'Publishi to Tomat ...'has encountered a problem.Could not publish to the server错误
- 当你输入一个网址后回车,实际会发生什么?
- Opensuse13.2开启ssh
- 转:sublime2 官方网址
- IOS-UI控件大全
- A Knight's Journey(dfs+最小字典序)
- CANopen学习笔记
- HelloSCDN
- CS结构与BS结构的特点分析