Torch / Lua,mini-batch 训练的神经网络结构有哪些?

Torch / Lua, which neural network structure for mini-batch training?

我仍在致力于在我的孪生神经网络上实现小批量梯度更新。以前我有一个实现问题,那就是

现在我意识到我的神经网络架构也有错误,这与我对正确实现的理解不完整有关。

到目前为止,我一直使用非小批量梯度下降方法,其中我将训练元素一个一个地传递给梯度更新。现在,我想通过小批量实现梯度更新,从由 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
}

我有:

这是我的代码:

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 个元素的梯度更新函数,那么这个架构工作得很好;应该如何修改它让它管理一个minibatch?

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

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

大家怎么看?

每个 nn 模块都可以使用小批量。有些只适用于小批量,例如(Spatial)BatchNormalization。一个模块知道它的输入必须包含多少维(假设是 D),如果模块接收到 D+1 维张量,它会假定第一个维是批次维。例如,看看 nn.Linear module documentation:

The input tensor given in forward(input) must be either a vector (1D tensor) or matrix (2D tensor). If the input is a matrix, then each row is assumed to be an input sample of given batch.

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.Paralelnn.ParallelTable 等)呢? Container 本身不处理输入,它只是将输入(或其对应部分)发送到它包含的相应模块。例如ParallelTable,简单地将第i个成员模块应用到第i个输入table元素。因此,如果你想让它处理一个批次,每个 input[i](输入是一个 table)必须是一个如上所述具有批次维度的张量。

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 采用序列 table 并将模块应用于 table 中的每个元素,独立地提供不加速。此外,它必须复制该模块,因此 "batch mode" 的效率远低于在线(非批处理)培训。 Sequencer 被设计为循环网络的一部分,在您的情况下没有必要使用它。