在 Torch/Lua 中,我可以像流经网络一样拆分/连接张量吗?

我是一名 Lua/Torch 初学者。我有一个包括最大池化层的现有模型。我想将该层的输入拆分成几块,并将每块馈入新的最大池化层。

我编写了一个独立的 Lua 脚本,可以将一个张量拆分成两个部分,并将这两个部分转发到具有两个最大池化层的网络中。

但是,尝试将其集成回现有模型时,我无法弯曲数据“中流而过”,以执行张量分裂。我已阅读文档,但找不到在某个地方将张量拆分为两个部分,并分别将每个部分转发的函数或架构示例。

有什么想法吗?谢谢!

点赞
用户7194361
用户7194361

如果您想要自定义一个层,如下所示,如果您的层输入只有一个维度:

CSplit,parent = torch.class('nn.CSplit''nn.Module'function CSplit:__init(firstCount)
    self.firstCount = firstCount
    parent.__init(自我)
end

function CSplit:updateOutput(输入)
    local inputSize = input:size()[1]
    local firstCount = self.firstCount
    local secondCount = inputSize  -  firstCount
    local first = torch.Tensor(self.firstCount)
    local second = torch.Tensor(secondCount)
    for i = 1,inputSize do
        if i <= firstCount then
            first [i] = input [i]
        其他
            second [i  -  firstCount] = 输入[i]
        end
    end
    self.output = {first,second}
    返回 self.output
end

function CSplit:updateGradInput(输入,gradOutput)
    local inputSize = input :size()[1]
    self.gradInput = torch.Tensor(input)
    for i = 1,inputSize do
        if i <= self.firstCount then
            self.gradInput [i] = gradOutput [1] [i]
        其他
            self.gradInput [i] = gradOutput [2] [i  -  self.firstCount]
        end
    end
    返回 self.gradInput
end

如何使用?您需要像下面的代码一样指定第一个块的大小。

testNet = nn.CSplit(4)
input = torch.randn(10)
output = testNet:forward(inputprintinputprintoutput [1])
printoutput [2])
testNet:backward(input,{torch.randn(4),torch.randn(6)})

您可以在这里看到可运行的iTorch笔记本代码。

2017-11-28 14:03:19