转载请注明出处:

http://www.cnblogs.com/darkknightzh/p/6065526.html

本部分多试几次就可以弄得清每一层具体怎么访问了。

step1. 网络定义如下:

require "dpnn"
local net = nn.Sequential()
net:add(nn.SpatialConvolution(, , , , , , , ))
net:add(nn.SpatialBatchNormalization())
net:add(nn.ReLU())
net:add(nn.SpatialMaxPooling(, , , , , ))
net:add(nn.Inception{
inputSize = ,
kernelSize = {, },
kernelStride = {, },
outputSize = {, },
reduceSize = {, , , },
pool = nn.SpatialMaxPooling(, , , , , ),
batchNorm = true
})
net:evaluate()

上面的网络,包含conv+BatchNorm+ReLU+Maxpool+Inception层。

step2. 直接通过print(net)便可得到其网络结构:

nn.Sequential {
[input -> (1) -> (2) -> (3) -> (4) -> (5) -> output]
(1): nn.SpatialConvolution(3 -> 64, 7x7, 2,2, 3,3)
(2): nn.SpatialBatchNormalization
(3): nn.ReLU
(4): nn.SpatialMaxPooling(3x3, 2,2, 1,1)
(5): nn.Inception @ nn.DepthConcat {
input
|`-> (1): nn.Sequential {
| [input -> (1) -> (2) -> (3) -> (4) -> (5) -> (6) -> output]
| (1): nn.SpatialConvolution(64 -> 96, 1x1)
| (2): nn.SpatialBatchNormalization
| (3): nn.ReLU
| (4): nn.SpatialConvolution(96 -> 128, 3x3, 1,1, 1,1)
| (5): nn.SpatialBatchNormalization
| (6): nn.ReLU
| }
|`-> (2): nn.Sequential {
| [input -> (1) -> (2) -> (3) -> (4) -> (5) -> (6) -> output]
| (1): nn.SpatialConvolution(64 -> 16, 1x1)
| (2): nn.SpatialBatchNormalization
| (3): nn.ReLU
| (4): nn.SpatialConvolution(16 -> 32, 5x5, 1,1, 2,2)
| (5): nn.SpatialBatchNormalization
| (6): nn.ReLU
| }
|`-> (3): nn.Sequential {
| [input -> (1) -> (2) -> (3) -> (4) -> output]
| (1): nn.SpatialMaxPooling(3x3, 1,1, 1,1)
| (2): nn.SpatialConvolution(64 -> 32, 1x1)
| (3): nn.SpatialBatchNormalization
| (4): nn.ReLU
| }
|`-> (4): nn.Sequential {
[input -> (1) -> (2) -> (3) -> output]
(1): nn.SpatialConvolution(64 -> 64, 1x1)
(2): nn.SpatialBatchNormalization
(3): nn.ReLU
}
... -> output
}
}

但实际上该网络还包括input,output,gradInput等参数。

step3. 使用下面代码便可输出网络比较详细的参数:

for k,curLayer in pairs(net) do
print(k,curLayer)
end

step4. 输出:

_type	torch.DoubleTensor
output [torch.DoubleTensor with no dimension] gradInput [torch.DoubleTensor with no dimension] modules {
1 :
{
dH : 2
dW : 2
nInputPlane : 3
output : DoubleTensor - empty
kH : 7
train : false
gradBias : DoubleTensor - size: 64
padH : 3
bias : DoubleTensor - size: 64
weight : DoubleTensor - size: 64x3x7x7
_type : "torch.DoubleTensor"
gradWeight : DoubleTensor - size: 64x3x7x7
padW : 3
nOutputPlane : 64
kW : 7
gradInput : DoubleTensor - empty
}
2 :
{
gradBias : DoubleTensor - size: 64
output : DoubleTensor - empty
gradInput : DoubleTensor - empty
running_var : DoubleTensor - size: 64
momentum : 0.1
gradWeight : DoubleTensor - size: 64
eps : 1e-05
_type : "torch.DoubleTensor"
affine : true
running_mean : DoubleTensor - size: 64
bias : DoubleTensor - size: 64
weight : DoubleTensor - size: 64
train : false
}
3 :
{
inplace : false
threshold : 0
_type : "torch.DoubleTensor"
output : DoubleTensor - empty
gradInput : DoubleTensor - empty
train : false
val : 0
}
4 :
{
dH : 2
dW : 2
kW : 3
gradInput : DoubleTensor - empty
indices : DoubleTensor - empty
train : false
_type : "torch.DoubleTensor"
padH : 1
ceil_mode : false
output : DoubleTensor - empty
kH : 3
padW : 1
}
5 :
{
outputSize :
{
1 : 128
2 : 32
}
inputSize : 64
gradInput : DoubleTensor - empty
modules :
{
1 :
{
train : false
_type : "torch.DoubleTensor"
output : DoubleTensor - empty
gradInput : DoubleTensor - empty
modules :
{
1 : {...}
2 : {...}
3 : {...}
4 : {...}
}
dimension : 2
size : LongStorage - size: 0
}
}
kernelStride :
{
1 : 1
2 : 1
}
_type : "torch.DoubleTensor"
module :
{
train : false
_type : "torch.DoubleTensor"
output : DoubleTensor - empty
gradInput : DoubleTensor - empty
modules :
{
1 :
{
_type : "torch.DoubleTensor"
output : DoubleTensor - empty
gradInput : DoubleTensor - empty
modules : {...}
train : false
}
2 :
{
_type : "torch.DoubleTensor"
output : DoubleTensor - empty
gradInput : DoubleTensor - empty
modules : {...}
train : false
}
3 :
{
_type : "torch.DoubleTensor"
output : DoubleTensor - empty
gradInput : DoubleTensor - empty
modules : {...}
train : false
}
4 :
{
_type : "torch.DoubleTensor"
output : DoubleTensor - empty
gradInput : DoubleTensor - empty
modules : {...}
train : false
}
}
dimension : 2
size : LongStorage - size: 0
}
poolStride : 1
padding : true
reduceStride : {...}
transfer :
{
inplace : false
threshold : 0
_type : "torch.DoubleTensor"
output : DoubleTensor - empty
gradInput : DoubleTensor - empty
val : 0
}
batchNorm : true
train : false
pool :
{
dH : 1
dW : 1
kW : 3
gradInput : DoubleTensor - empty
indices : DoubleTensor - empty
train : false
_type : "torch.DoubleTensor"
padH : 1
ceil_mode : false
output : DoubleTensor - empty
kH : 3
padW : 1
}
poolSize : 3
reduceSize :
{
1 : 96
2 : 16
3 : 32
4 : 64
}
kernelSize :
{
1 : 3
2 : 5
}
output : DoubleTensor - empty
}
}
train false

上面的modules中,分别为conv、BatchNorm、ReLU、Maxpool、Inception对应的参数。

step5. 可通过net.modules[1]来索引nn.SpatialConvolution。如print(net.modules[1])得到:

nn.SpatialConvolution(3 -> 64, 7x7, 2,2, 3,3)

step6. 如果想更进一步,输出该层的参数,可以使用如下代码(实际上step4中已经输出了):

for k,curLayer in pairs(net.modules[]) do
if type(curLayer) ~= 'userdata' then
print(k,curLayer)
else
local strval = ' '
for i = , curLayer:dim() do
strval = strval .. curLayer:size(i) .. " "
end
print(k .. " " .. type(curLayer) .. " " .. string.format("\27[31m size: %s", strval))
end
end

step7. 得到的结果为:

dH	2
dW 2
nInputPlane 3
output userdata size:
kH 7
train false
gradBias userdata size: 64
padH 3
bias userdata size: 64
weight userdata size: 64 3 7 7
_type torch.DoubleTensor
gradWeight userdata size: 64 3 7 7
padW 3
nOutputPlane 64
kW 7
gradInput userdata size:

step8. 对于Inception层,step4中并没有完全显示出来。按照step5中的方式,使用net.modules[5]来得到Inception层。将step6进行更改,可输出:

outputSize	{
1 : 128
2 : 32
}
inputSize 64
gradInput userdata size:
modules {
1 :
{
train : false
_type : "torch.DoubleTensor"
output : DoubleTensor - empty
gradInput : DoubleTensor - empty
modules :
{
1 :
{
_type : "torch.DoubleTensor"
output : DoubleTensor - empty
gradInput : DoubleTensor - empty
modules :
{
1 : {...}
2 : {...}
3 : {...}
4 : {...}
5 : {...}
6 : {...}
}
train : false
}
2 :
{
_type : "torch.DoubleTensor"
output : DoubleTensor - empty
gradInput : DoubleTensor - empty
modules :
{
1 : {...}
2 : {...}
3 : {...}
4 : {...}
5 : {...}
6 : {...}
}
train : false
}
3 :
{
_type : "torch.DoubleTensor"
output : DoubleTensor - empty
gradInput : DoubleTensor - empty
modules :
{
1 : {...}
2 : {...}
3 : {...}
4 : {...}
}
train : false
}
4 :
{
_type : "torch.DoubleTensor"
output : DoubleTensor - empty
gradInput : DoubleTensor - empty
modules :
{
1 : {...}
2 : {...}
3 : {...}
}
train : false
}
}
dimension : 2
size : LongStorage - size: 0
}
}
kernelStride {
1 : 1
2 : 1
}
_type torch.DoubleTensor
module nn.DepthConcat {
input
|`-> (1): nn.Sequential {
| [input -> (1) -> (2) -> (3) -> (4) -> (5) -> (6) -> output]
| (1): nn.SpatialConvolution(64 -> 96, 1x1)
| (2): nn.SpatialBatchNormalization
| (3): nn.ReLU
| (4): nn.SpatialConvolution(96 -> 128, 3x3, 1,1, 1,1)
| (5): nn.SpatialBatchNormalization
| (6): nn.ReLU
| }
|`-> (2): nn.Sequential {
| [input -> (1) -> (2) -> (3) -> (4) -> (5) -> (6) -> output]
| (1): nn.SpatialConvolution(64 -> 16, 1x1)
| (2): nn.SpatialBatchNormalization
| (3): nn.ReLU
| (4): nn.SpatialConvolution(16 -> 32, 5x5, 1,1, 2,2)
| (5): nn.SpatialBatchNormalization
| (6): nn.ReLU
| }
|`-> (3): nn.Sequential {
| [input -> (1) -> (2) -> (3) -> (4) -> output]
| (1): nn.SpatialMaxPooling(3x3, 1,1, 1,1)
| (2): nn.SpatialConvolution(64 -> 32, 1x1)
| (3): nn.SpatialBatchNormalization
| (4): nn.ReLU
| }
|`-> (4): nn.Sequential {
[input -> (1) -> (2) -> (3) -> output]
(1): nn.SpatialConvolution(64 -> 64, 1x1)
(2): nn.SpatialBatchNormalization
(3): nn.ReLU
}
... -> output
}
poolStride 1
padding true
reduceStride {}
transfer nn.ReLU
batchNorm true
train false
pool nn.SpatialMaxPooling(3x3, 1,1, 1,1)
poolSize 3
reduceSize {
1 : 96
2 : 16
3 : 32
4 : 64
}
kernelSize {
1 : 3
2 : 5
}
output userdata size:

step9.step8中,modules中为对应的inception各层(3*3卷积,5*5卷积,pooling,1*1reduce)。可通过net.modules[5].module来得到这些层。该层也有train,output,gradInput,modules等变量。可通过print(net.modules[5].module)来输出。

step10. 根据step5中的思路,可通过net.modules[5].module.modules[1]来得到3*3卷基层具体情况:

_type	torch.DoubleTensor
output userdata size:
gradInput userdata size:
modules {
1 :
{
dH : 1
dW : 1
nInputPlane : 64
output : DoubleTensor - empty
kH : 1
train : false
gradBias : DoubleTensor - size: 96
padH : 0
bias : DoubleTensor - size: 96
weight : DoubleTensor - size: 96x64x1x1
_type : "torch.DoubleTensor"
gradWeight : DoubleTensor - size: 96x64x1x1
padW : 0
nOutputPlane : 96
kW : 1
gradInput : DoubleTensor - empty
}
2 :
{
gradBias : DoubleTensor - size: 96
output : DoubleTensor - empty
gradInput : DoubleTensor - empty
running_var : DoubleTensor - size: 96
momentum : 0.1
gradWeight : DoubleTensor - size: 96
eps : 1e-05
_type : "torch.DoubleTensor"
affine : true
running_mean : DoubleTensor - size: 96
bias : DoubleTensor - size: 96
weight : DoubleTensor - size: 96
train : false
}
3 :
{
inplace : false
threshold : 0
_type : "torch.DoubleTensor"
output : DoubleTensor - empty
gradInput : DoubleTensor - empty
train : false
val : 0
}
4 :
{
dH : 1
dW : 1
nInputPlane : 96
output : DoubleTensor - empty
kH : 3
train : false
gradBias : DoubleTensor - size: 128
padH : 1
bias : DoubleTensor - size: 128
weight : DoubleTensor - size: 128x96x3x3
_type : "torch.DoubleTensor"
gradWeight : DoubleTensor - size: 128x96x3x3
padW : 1
nOutputPlane : 128
kW : 3
gradInput : DoubleTensor - empty
}
5 :
{
gradBias : DoubleTensor - size: 128
output : DoubleTensor - empty
gradInput : DoubleTensor - empty
running_var : DoubleTensor - size: 128
momentum : 0.1
gradWeight : DoubleTensor - size: 128
eps : 1e-05
_type : "torch.DoubleTensor"
affine : true
running_mean : DoubleTensor - size: 128
bias : DoubleTensor - size: 128
weight : DoubleTensor - size: 128
train : false
}
6 :
{
inplace : false
threshold : 0
_type : "torch.DoubleTensor"
output : DoubleTensor - empty
gradInput : DoubleTensor - empty
train : false
val : 0
}
}
train false

注意:此处有一个module和一个modules,具体不太明白。

step11. 可通过net.modules[5].module.modules[1].modules进一步查看该层的情况:

1	nn.SpatialConvolution(64 -> 96, 1x1)
2 nn.SpatialBatchNormalization
3 nn.ReLU
4 nn.SpatialConvolution(96 -> 128, 3x3, 1,1, 1,1)
5 nn.SpatialBatchNormalization
6 nn.ReLU

可见,该层包括1*1conv,BatchNorm,ReLU,3*3conv,BatchNorm,Relu这些。

step12. 若要查看step11中的3*3卷基层信息,可使用如下索引:

net.modules[5].module.modules[1].modules[4]

结果如下:

dH	1
dW 1
nInputPlane 96
output userdata size:
kH 3
train false
gradBias userdata size: 128
padH 1
bias userdata size: 128
weight userdata size: 128 96 3 3
_type torch.DoubleTensor
gradWeight userdata size: 128 96 3 3
padW 1
nOutputPlane 128
kW 3
gradInput userdata size:

step13. 到了step12,已经索引到了step1中网络的最深层。网络中每层均有input,output等。

step14. 对于net.modules[5]的Inception层,net.modules[5].output的结果和net.modules[5].module.output的结果是一样的,如(为方便显示,只显示了一小部分。如果输出net.modules[5].output,可能会有很多全为0的):

local imgBatch = torch.rand(,,,)
local infer = net:forward(imgBatch) print(net.modules[].output[][][])
print(net.modules[].module.output[][][])

结果为:

0.01 *
2.7396
2.9070
3.1895
1.5040
1.9784
4.0125
3.2874
3.3137
2.1326
2.3930
2.8170
3.5226
2.3162
2.7308
2.8511
2.5278
3.3325
3.0819
3.2826
3.5363
2.5749
2.8816
2.2393
2.4765
2.4803
3.2553
3.0837
3.1197
2.4632
1.5145
3.7101
2.1888
[torch.DoubleTensor of size 32] 0.01 *
2.7396
2.9070
3.1895
1.5040
1.9784
4.0125
3.2874
3.3137
2.1326
2.3930
2.8170
3.5226
2.3162
2.7308
2.8511
2.5278
3.3325
3.0819
3.2826
3.5363
2.5749
2.8816
2.2393
2.4765
2.4803
3.2553
3.0837
3.1197
2.4632
1.5145
3.7101
2.1888
[torch.DoubleTensor of size 32]

(原)torch中显示nn.Sequential()网络的详细情况的更多相关文章

  1. DB中耗时的 存储过程 及执行详细情况

    SELECT a.object_id, a.database_id, OBJECT_NAME(object_id, database_id) 'proc name', a.cached_time, a ...

  2. 小白学习之pytorch框架(2)-动手学深度学习(begin-random.shuffle()、torch.index_select()、nn.Module、nn.Sequential())

    在这向大家推荐一本书-花书-动手学深度学习pytorch版,原书用的深度学习框架是MXNet,这个框架经过Gluon重新再封装,使用风格非常接近pytorch,但是由于pytorch越来越火,个人又比 ...

  3. (原)torch中微调某层参数

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/6221664.html 参考网址: https://github.com/torch/nn/issues ...

  4. (原)ubuntu16在torch中使用caffe训练好的模型

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5783006.html 之前使用的是torch,由于其他人在caffe上面预训练了inception模型 ...

  5. [pytorch笔记] torch.nn vs torch.nn.functional; model.eval() vs torch.no_grad(); nn.Sequential() vs nn.moduleList

    1. torch.nn与torch.nn.functional之间的区别和联系 https://blog.csdn.net/GZHermit/article/details/78730856 nn和n ...

  6. ios 从网络上获取图片并在UIImageView中显示

    ios 从网络上获取图片   -(UIImage *) getImageFromURL:(NSString *)fileURL { NSLog(@"执行图片下载函数"); UIIm ...

  7. torch.nn.Sequential()详解

    参考:官方文档    源码 官方文档 nn.Sequential A sequential container. Modules will be added to it in the order th ...

  8. Pytorch——torch.nn.Sequential()详解

    参考:官方文档    源码 官方文档 nn.Sequential A sequential container. Modules will be added to it in the order th ...

  9. (原)torch中threads的addjob函数使用方法

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/6549452.html 参考网址: https://github.com/torch/threads#e ...

随机推荐

  1. Python正则匹配递归获得给出目录下的特定类型的文件小技巧

    需求是酱的: 输入一个目录,这个目录包含检测目录的必备信息但不准确需要获得后加工一下,如给出目录:C:\Program Files\Common Files\DESIGNER,需要检测的目录是:C:\ ...

  2. 【好程序员笔记分享】——UIView与CALayer详解

    -iOS培训,iOS学习-------型技术博客.期待与您交流!------------ UIView与CALayer详解 研究Core Animation已经有段时间了,关于Core Animati ...

  3. java实现双向链表

    PS:双向链表(每个节点含有指向前一个节点的前驱与后一个节点的后继) public class DoublyLinkedList { static class Node { private Objec ...

  4. hdu 1385 Minimum Transport Cost

    http://acm.hdu.edu.cn/showproblem.php?pid=1385 #include <cstdio> #include <cstring> #inc ...

  5. XJOI网上同步训练DAY5 T1

    思路:考虑得出,最终的集合一定是gcd=1的集合,那么我们枚举n个数中哪个数必须选,然后把它质因数分解,由于质数不会超过9个,可以状态压缩,去得出状态为0的dp值就是答案. #include<c ...

  6. tag_on_failure => [] # prevent default _grokparsefailure tag on real records

    [elk@zjtest7-frontend config]$ cat stdin04.conf input { stdin { } } filter { # drop sleep events gro ...

  7. VC++中的类的内存分布(上)

    0.序 目前正在学习C++中,对于C++的类及其类的实现原理也挺感兴趣.于是打算通过观察类在内存中的分布更好地理解类的实现.因为其实类的分布是由编译器决定的,而本次试验使用的编译器为VS2015 RC ...

  8. c语言else匹配问题

    #include <stdio.h> #include <stdlib.h> //实现 依次输入三个递增的数 然后正确输出 //为什么得不到我们想要的结果呢 这就是else匹配 ...

  9. Ajax--json(Ajax调用返回json封装代码、格式及注意事项)

    Ajax调用json封装代码<dbda.php>: //Ajax调用返回JSON public function JsonQuery($sql,$type=1,$db="mydb ...

  10. JAVA日期处理(Timestamp)

    要写一些与数据库连接时的日期处理,pstmt.setDate()的类型是java.sql.Date类型,这种符合规范的类型其实并没有把时分秒存进数据库,所以存取时就应该用setTimestamp()或 ...