加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-14 21:50:01 所属栏目:大数据 来源:网络整理
导读:我还在努力在我的暹罗神经网络上实现小批量梯度更新.以前我有一个实施问题,那就是 correctly solved here. 现在我意识到我的神经网络架构中也存在一个错误,这与我对正确实现的不完全理解有关. 到目前为止,我总是使用非小批量梯度下降方法,其中我将训练元素逐
我还在努力在我的暹罗神经网络上实现小批量梯度更新.以前我有一个实施问题,那就是 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个元素的梯度更新函数,这个架构非常有效.如何修改它以让它管理一个小批量?

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

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

你们有什么感想?

解决方法

每个nn模块都可以使用miniatches.有些只能使用微型计算机,例如(空间)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.Sequential,nn.Paralel,nn.ParallelTable等)?容器本身不处理输入,它只是将输入(或其相应的部分)发送到它包含的相应模块.例如,ParallelTable只是将第i个成员模块应用于第i个输入表元素.因此,如果您希望它处理批处理,则每个输入[i](输入是表格)必须是具有如上所述的批处理维度的张量.

input_number = 5
output_number = 2

inputs1 = {
    torch.Tensor(5):fill(1),}
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,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被设计成复发网的一部分,在你的情况下没有必要使用它.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读