【问题标题】:Torch / Lua, which neural network structure for mini-batch training?Torch / Lua,mini-batch 训练的神经网络结构是什么?
【发布时间】:2016-05-29 22:37:18
【问题描述】:

我仍在努力在我的连体神经网络上实现小批量梯度更新。以前我有一个实现问题,那就是correctly solved here

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

到目前为止,我一直使用非小批量梯度下降方法,其中我将训练元素一个一个传递给梯度更新。现在,我想通过小批量实现梯度更新,从由 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 个元素的梯度更新函数,这个架构就可以很好地工作;应该如何修改它以让它管理 minibatch

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

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

你们觉得呢?

【问题讨论】:

    标签: lua neural-network torch


    【解决方案1】:

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

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

    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 等)呢?容器本身不处理输入,它只是将输入(或其对应部分)发送到它包含的相应模块。例如,ParallelTable 只是将第 i 个成员模块应用于第 i 个输入表元素。因此,如果您希望它处理批次,则每个 input[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 被设计为循环网络的一部分,在您的情况下使用它没有意义。

    【讨论】:

    • 嗨@Alexander,感谢您的回复。我正在尝试实施您的解决方案,但我陷入了渐变更新perceptron:backward(input_batch, targets) 指令。 targets 应该包含我的训练目标,例如0,1。如果input_batch 是2 个大小为3x5 的DoubleTensor 的列表,那么target 的正确尺寸应该是多少?谢谢
    • @DavideChicco.it,目的是最小化输入对之间的距离,对吧?那你的目标是什么?我会假设它是零。 0,1 来自哪里?
    • 我正在比较成对的向量。每个向量由 6 个实数值组成。每对可以为真 (target=1) 或假 (target=0)。在训练期间,我打电话给perceptron:forward(input_batch),然后是perceptron:zeroGradParameters()perceptron:backward(input_batch, targets)。我在targets 的尺寸方面遇到问题,我必须适应新的设置。大小为 1 的 #input_batch DoubleTensors 向量不起作用,我应该使用什么?谢谢
    • [torch.DoubleTensor 大小 BatchSize] 必须有效。我正在使用 MSECriterion 并且它有效。你的标准是什么?
    • 实现起来非常费力(我不得不从 N 对张量的向量切换到一对 N 张量)但最后我认为我能够以正确的方式做到这一点.谢谢亚历山大!
    猜你喜欢
    • 2011-04-07
    • 1970-01-01
    • 2017-10-12
    • 2010-11-20
    • 2019-09-15
    • 1970-01-01
    • 1970-01-01
    • 2016-01-22
    • 1970-01-01
    相关资源
    最近更新 更多