您的位置:首页 > 其它

Torch在使用中常见的问题hdf5安装,nn增加类

2018-01-01 11:33 393 查看
nn中增加类

CmdLine

hdf5

dp

nn

nn中增加类

可以在nn中增加class,local NewClass, Parent = torch.class(‘nn.NewClass’, ‘nn.Module’) 该类从nn.Module继承,新的类存在NewClass.lua中保存在nn这个库。另外生成的类的名字叫做NewClass,【19】,【20】,【21】调用的时候就根据init看是否需要传参,然后进行调用local new = nn.NewClass()

--创建新类,从nn.Module继承
local NewClass, Parent = torch.class('nn.NewClass', 'nn.Module')
--初始化操作
function NewClass:__init()
Parent.__init(self)
end
--前向传播
function NewClass:updateOutput(input)
end
--反向传播
function NewClass:updateGradInput(input, gradOutput)
end
--损失对参数的偏导,也就是残差,如果该层没有要学习的参数,则不需要写这个函数
function NewClass:accGradParameters(input, gradOutput)
end


CmdLine()

这个函数是在Torch中用于调参的一个函数,方便参数解析。并能保存成log,也可以load。只会保存optition里面的参数[1], [5]。

cmd = torch.CmdLine()
cmd:text()
cmd:text()
cmd:text('Training a simple network')
cmd:text()
cmd:text('Options')
cmd:option('-seed',123,'initial random seed')
cmd:option('-booloption',false,'boolean option')
cmd:option('-stroption','mystring','string option')
cmd:text()

-- parse input params
params = cmd:parse(arg)

params.rundir = cmd:string('experiment', params, {dir=true})
paths.mkdir(params.rundir)

-- create log file
cmd:log(params.rundir .. '/log', params)


结果为:

[program started on Tue Jan 10 15:33:49 2012]
[command line arguments]
booloption  false
seed    123
rundir  experiment
stroption   mystring
[----------------------]
booloption  false
seed    123
rundir  experiment
stroption   mystring


hdf5

这个库可以把Torch的数据类型和hdf5相互转化,hdf5数据结构更加方便,快捷。

In [2], [6],在包含安装hdf5的过程。我的是mac,所以安装过程如下:

brew tap homebrew/science
brew install hdf5
git clone https://github.com/deepmind/torch-hdf5 cd torch-hdf5
luarocks make hdf5-0-0.rockspec


如果是Linux,安装过程如下:

sudo apt-get install libhdf5-serial-dev hdf5-tools
git clone https://github.com/deepmind/torch-hdf5 cd torch-hdf5
luarocks make hdf5-0-0.rockspec LIBHDF5_LIBDIR="/usr/lib/x86_64-linux-gnu/"


从Torch中读取数据:

require 'hdf5'
local myFile = hdf5.open('/path/to/read.h5', 'r')
local data = myFile:read('/path/to/data'):all()
myFile:close()


安装hdf5报错,看[7], [8], [9]的解决办法。

dp

在[3]中,是dp的网站。dp是LISA实验室开发的用于神经网络深度学习的一个Torch的包。

nn

nn里面有函数torch.totable把数据转换成table,因为是在Lua里面,数据结果存储用Table。

Torch中对于Lua/Torch中的object进行了serialize/deserialize,分别是:

-- 把object存储到filename这里
-- 存储格式可以是二进制或者ascii
torch.save(filename, object [, format, referenced])

-- 和上面的保存正好相反,从filename里面load Object出来
[object] torch.load(filename [, format, referenced])

-- 把一个Object变成String
[str] torch.serialize(object)

-- 把一个string变成Object
[object] torch.deserialize(str)


nn.Sequential: 是一个模型的容器,用来存储模型。是按顺序存模型进去【11】,【12】。

model = nn.Sequential(
nn.Conv2d(1,20,5),
nn.ReLU(),
nn.Conv2d(20,64,5),
nn.ReLU()
)


nn.linear(dimX,dimY): 实现Ax + b = Y,

nn.HardTanh(a,b): 【13】 低于a则为a,大于b则为b,否则等于本身。

nn.Normalize(n) : 计算Ln norm, help(nn.Normalize)。

nn.ReinforceCategorical(): [14], [15]. torch.class(“nn.Reinforce”, “nn.Module”)

nn.ArgMax(n): [17] 返回n维的最大值下标。

nn.Dropout(doueble): 进行Dropout操作来减少过拟合。

nn.Collapse(n): [18] to concatenate the embeddings of all input words

Ref Links:

[1] CmdLine博客: http://blog.csdn.net/jiejinquanil/article/details/49659159

[2] hdf5安装: https://github.com/deepmind/torch-hdf5/blob/master/doc/usage.md

[3] dp: https://dp.readthedocs.io/en/latest/

[4] TorchVideo Tutorial: https://github.com/Atcold/torch-Video-Tutorials

[5] CmdLine 的官网: http://torch7.readthedocs.io/en/latest/cmdline/index.html

[6] hdf5安装: https://github.com/deepmind/torch-hdf5

[7] hdf5错误解决: https://github.com/karpathy/neuraltalk2/issues/123

[8] hdf5的问题解决: https://www.jianshu.com/p/729be36fdb21

[9] hdf5的问题解决: http://www.linuxidc.com/Linux/2015-04/116452.htm

[10] serialize/deserialize: https://github.com/torch/torch7/blob/master/doc/serialization.md

[11] PyTorch nn document: http://pytorch.org/docs/master/nn.html

[12] Torch nn package: https://nn.readthedocs.io/en/rtd/index.html

[13] Torch HardTanh: http://torch5.sourceforge.net/manual/nn/index-2-4-2.html

[14] Torch nn.ReinforceCategorical(): https://github.com/Element-Research/dpnn/blob/master/ReinforceCategorical.lua

[15] Torch nn.ReinforceCategorical(): https://github.com/shubhtuls/volumetricPrimitives/blob/master/modules/ReinforceCategorical.lua

[16] Torch torch.class(“nn.Reinforce”, “nn.Module”): https://github.com/Element-Research/dpnn/blob/master/Reinforce.lua

[17] Torch ArgMax: https://github.com/Element-Research/dpnn/blob/master/ArgMax.lua

[18] Torch Collapse: http://www.voidcn.com/article/p-apptufvm-brn.html

[19] Torch Class: http://blog.csdn.net/happyer88/article/details/52884586

[20] Torch Class: http://torch.ch/docs/developer-docs.html

[21] Mode Class: https://ptorch.com/docs/2/developer-docs

[22] 改变hdf5的版本: https://github.com/karpathy/neuraltalk2/issues/149

[23] hdf5的安装: https://github.com/deepmind/torch-hdf5/issues/83#issuecomment-254427843

[24] hdf5在不同语言下的安装: http://www.jeffreythompson.org/blog/2016/03/25/torch-rnn-mac-install/comment-page-2/#comment-287591

[25] hdf5 的安装: https://github.com/karpathy/neuraltalk2/issues/123

[26] Error: unable to locate HDF5 header file at hdf5.h: https://github.com/karpathy/neuraltalk2/issues/123
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Torch nn dp hdf5