Torch在使用中常見的問題hdf5安裝,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