文章目錄

Torch7 代码入门级介绍
代码地址:https://github.com/torch/tutorials 网站地址:http://code.madbits.com/wiki/doku.php?id=start
torch-tutorials 是网上不多的有关torch7的demo之一,本文主要以Deep Learning训练过程为切入点,介绍利用Torch7训练Deep Learning network。
这里介绍 2_supervised\4_train.lua 中的核心函数 train( )
代码全文如下:(我把不重要的地方去掉了,由于CSDN对Lua的支持不太好,很多地方无法高亮)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
function train()  

-- epoch tracker
epoch = epoch or 1

-- shuffle at each epoch
shuffle = torch.randperm(trsize)

-- do one epoch
print('==> doing epoch on training data:')
print("==> online epoch # " .. epoch .. ' [batchSize = ' .. opt.batchSize .. ']')
for t = 1,trainData:size(),opt.batchSize do
-- disp progress
xlua.progress(t, trainData:size())

-- create mini batch
local inputs = {}
local targets = {}
for i = t,math.min(t+opt.batchSize-1,trainData:size()) do
-- load new sample
local input = trainData.data[shuffle[i]]
local target = trainData.labels[shuffle[i]]
if opt.type == 'double' then input = input:double()
elseif opt.type == 'cuda' then input = input:cuda() end
table.insert(inputs, input)
table.insert(targets, target)
end

-- create closure to evaluate f(X) and df/dX
local feval = function(x)
-- get new parameters
if x ~= parameters then
parameters:copy(x)
end

-- reset gradients
gradParameters:zero()

-- f is the average of all criterions
local f = 0

-- evaluate function for complete mini batch
for i = 1,#inputs do
-- estimate f
local output = model:forward(inputs[i])
local err = criterion:forward(output, targets[i])
f = f + err

-- estimate df/dW
local df_do = criterion:backward(output, targets[i])
model:backward(inputs[i], df_do)
end

-- normalize gradients and f(X)
gradParameters:div(#inputs)
f = f/#inputs

-- return f and df/dX
return f,gradParameters
end

-- optimize on current mini-batch
optimMethod(feval, parameters, optimState)
end

-- next epoch
epoch = epoch + 1
end

下面分段介绍

1
2
3
4
5
6
7
8
9
for i = t,math.min(t+opt.batchSize-1,trainData:size()) do  
-- load new sample
local input = trainData.data[shuffle[i]]
local target = trainData.labels[shuffle[i]]
if opt.type == 'double' then input = input:double()
elseif opt.type == 'cuda' then input = input:cuda() end
table.insert(inputs, input)
table.insert(targets, target)
end

这部分实际上是选择一个batch作为训练最小单位。在Torch中一般用“table”数据结构保存数据。“table”是Lua下的一个数据结构,它比较特殊,可以用作数组,字典(哈希表),类等解构在这里
[plain] view plaincopy
table.insert(inputs, input)
实际上是讲一个batch的数据放在一个inputs里面,作为一次update的数据。同样targets保存了对应的label。

然后是描述目标函数,描述了feval这个目标函数就可以用优化方法对feval进行优化求解,也就是对网络进行训练,下面介绍如何描述目标函数feval:

1
2
3
local feval = function(x)  
.....
end

下面是函数里面的代码说明

1
2
3
if x ~= parameters then   
parameters:copy(x)
end

这里的parameters和下面的gradParameters都是通过函数model:getParameters()获得的,他们分表保存着model的权重参数以及梯度参数,代码我没有贴上来,这里说明一下

1
gradParameters:zero()

将梯度参数初始化为0

1
local f = 0

f里面保存了平均criterions这个可以理解为误差。

1
2
3
4
5
6
7
for i = 1,#inputs do  
local output = model:forward(inputs[i])
local err = criterion:forward(output, targets[i])
f = f + err的loss
local df_do = criterion:backward(output, targets[i])
model:backward(inputs[i], df_do)
end

model:forward()表示对model作正向传导得到output,criterion:forward()表示用criterion准则计算loss,这里的准则需要我们之前选择好。criterion:backward()用来计算梯度,这里返回的是最后一层的梯度即df_do,model:backward()函数即为CNN的BP算法反向传导梯度,返回值直接保存到gradParameters中。

1
2
gradParameters:div(#inputs)  
f = f/#inputs

梯度和loss的规范化,这里gradParameters这个参数保存了上面获得的梯度返回

1
2
3
4
5
6
7
8
return f,gradParameters 
```
这里返回f和gradParameters的是因为优化函数规定了返回值。


好的,描述清楚了目标函数,下一步就是选择合适的方法求解了。
``` lua
optimMethod(feval, parameters, optimState)

这部分代码非常好理解,选择好了优化方法optimMethod后,就可以用optimMethod函数进行一次参数的优化求解,这里是操作一次optimMethod进行一次update,至于优化方法的类型以及参数的设置则保存在了optimMethod和optimState,这个将在后面的文档中介绍。

文章目錄