Torch / Lua中,哪种神经网络结构适用于小批量训练?

我仍在尝试实现对我自己的连体神经网络进行小批量梯度更新。之前我有一个实现问题,在这里正确解决了

现在我意识到,我的神经网络结构方面也存在错误,这与我的不完全理解正确实现有关。

到目前为止,我一直采用非小批量梯度下降方法,将训练元素一个一个地传递给梯度更新。现在,我想实现通过小批量进行梯度更新,例如从由N=2个元素组成的小批量开始。

我的问题是:我应该如何更改我的连体神经网络结构,使其能够处理由N=2个元素而不是单个元素组成的小批量?

这是我连体神经网络的(简化)结构:

nn.Sequential {
  [input -> (1) -> (2) -> output]
  (1): nn.ParallelTable {
    input
      |`-> (1): nn.Sequential {
      |      [input -> (1) -> (2) -> output]
      |      (1): nn.Linear(6 -> 3)
      |      (2): nn.Linear(3 -> 2)
      |    }
      |`-> (2): nn.Sequential {
      |      [input -> (1) -> (2) -> output]
      |      (1): nn.Linear(6 -> 3)
      |      (2): nn.Linear(3 -> 2)
      |    }
       ... -> output
  }
  (2): nn.CosineDistance
}

我有:

  • 2个相同的连体神经网络(上下两个)
  • 6个输入单元
  • 3个隐藏单元
  • 2个输出单元
  • 余弦距离函数,用于比较两个并行神经网络的输出

这是我的代码:

perceptronUpper= nn.Sequential()
perceptronUpper:add(nn.Linear(input_number, hiddenUnits))
perceptronUpper:add(nn.Linear(hiddenUnits,output_number))
perceptronLower= perceptronUpper:clone('weight', 'gradWeights', 'gradBias',
'bias')

parallel_table = nn.ParallelTable()
parallel_table:add(perceptronUpper)
parallel_table:add(perceptronLower)

perceptron = nn.Sequential()
perceptron:add(parallel_table)
perceptron:add(nn.CosineDistance())

如果我有一个梯度更新函数可以处理1个元素,则这种架构运行得非常好;我应该如何修改它,让它管理小批量呢?

编辑: 我可能应该使用nn.Sequencer()类,通过修改我代码的最后两行,如下所示:

perceptron:add(nn.Sequencer(parallel_table))
perceptron:add(nn.Sequencer(nn.CosineDistance()))。

你们觉得怎么样?

点赞
用户4850610
用户4850610

每个 nn 模块都可以使用小批量处理。有些模块仅使用小批量,例如 (Spatial)BatchNormalization。该模块知道它的输入必须包含多少个维度(假设为 D),如果该模块接收到一个 D+1 维的张量,则假定第一个维度为批处理维度。例如,请查看 nn.Linear 模块文档

forward(input) 中给出的输入张量必须是向量(1D 张量)或矩阵(2D 张量)。如果输入是一个矩阵,则假定每行是给定批次的输入样本。

function table_of_tensors_to_batch(tbl)
    local batch = torch.Tensor(#tbl, unpack(tbl[1]:size():totable()))
    for i = 1, #tbl do
       batch[i] = tbl[i]
    end
    return batch
end

inputs = {
    torch.Tensor(5):fill(1),
    torch.Tensor(5):fill(2),
    torch.Tensor(5):fill(3),
}
input_batch = table_of_tensors_to_batch(inputs)
linear = nn.Linear(5, 2)
output_batch = linear:forward(input_batch)

print(input_batch)
 1  1  1  1  1
 2  2  2  2  2
 3  3  3  3  3
[torch.DoubleTensor of size 3x5]

print(output_batch)
 0,3128 -1,1384
 0,7382 -2,1815
 1,1637 -3,2247
[torch.DoubleTensor of size 3x2]

那么容器呢(nn.Sequentialnn.Parallelnn.ParallelTable 等)?容器本身不处理输入,它只是将输入(或其相应的部分)发送到它包含的相应模块。例如,ParallelTable 将第 i 个成员模块简单地应用于第 i 个输入表元素。因此,如果要处理批次,则每个输入 [i](输入是一个表)必须是具有上述批处理维度的张量。

input_number = 5
output_number = 2

inputs1 = {
    torch.Tensor(5):fill(1),
    torch.Tensor(5):fill(2),
    torch.Tensor(5):fill(3),
}
inputs2 = {
    torch.Tensor(5):fill(4),
    torch.Tensor(5):fill(5),
    torch.Tensor(5):fill(6),
}
input1_batch = table_of_tensors_to_batch(inputs1)
input2_batch = table_of_tensors_to_batch(inputs2)

input_batch = {input1_batch, input2_batch}
output_batch = perceptron:forward(input_batch)

print(input_batch)
{
  1 : DoubleTensor - size: 3x5
  2 : DoubleTensor - size: 3x5
}
print(output_batch)
 0,6490
 0,9757
 0,9947
[torch.DoubleTensor of size 3]

target_batch = torch.Tensor({1, 0, 1})
criterion = nn.MSECriterion()
err = criterion:forward(output_batch, target_batch)
gradCriterion = criterion:backward(output_batch, target_batch)
perceptron:zeroGradParameters()
perceptron:backward(input_batch, gradCriterion)

那么 nn.Sequencer 是为什么?可以使用它吗?是的,但是强烈不建议。 Sequencer 接受一个序列表,并将模块独立地应用于表中的每个元素,不提供加速。除此之外,它还必须复制该模块,因此“批量模式”比在线(非批量)训练效率要低得多。 Sequencer 被设计为循环网络的一部分,在你的情况下使用它没有意义。

2016-02-29 13:45:38