Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Lua 如何将不同的代价函数应用于卷积网络的不同输出信道?_Lua_Machine Learning_Neural Network_Torch_Conv Neural Network - Fatal编程技术网

Lua 如何将不同的代价函数应用于卷积网络的不同输出信道?

Lua 如何将不同的代价函数应用于卷积网络的不同输出信道?,lua,machine-learning,neural-network,torch,conv-neural-network,Lua,Machine Learning,Neural Network,Torch,Conv Neural Network,我有一个卷积神经网络,它的输出是4通道2D图像。我想将sigmoid激活函数应用于前两个通道,然后使用BCECriterion将生成的图像的损失与地面真实图像进行计算。我想将平方损失函数应用于最后两个通道,最后计算梯度并进行backprop。我还想用期望的标量乘以最后两个通道中每一个通道的损耗平方成本 因此,成本具有以下形式: cost = crossEntropyCh[{1, 2}] + l1 * squaredLossCh_3 + l2 * squaredLossCh_4 我的想法如下:

我有一个卷积神经网络,它的输出是4通道2D图像。我想将sigmoid激活函数应用于前两个通道,然后使用BCECriterion将生成的图像的损失与地面真实图像进行计算。我想将平方损失函数应用于最后两个通道,最后计算梯度并进行backprop。我还想用期望的标量乘以最后两个通道中每一个通道的损耗平方成本

因此,成本具有以下形式:

cost = crossEntropyCh[{1, 2}] + l1 * squaredLossCh_3 + l2 * squaredLossCh_4
我的想法如下:

criterion1 = nn.BCECriterion()
criterion2 = nn.MSECriterion()

error = criterion1:forward(model.output[{{}, {1, 2}}], groundTruth1) + l1 * criterion2:forward(model.output[{{}, {3}}], groundTruth2) + l2 * criterion2:forward(model.output[{{}, {4}}], groundTruth3)
然而,我不认为这是正确的做法,因为我将不得不做3个单独的backprop步骤,每个成本条件一个。所以我想知道,有谁能给我一个更好的解决办法来解决这个问题吗?

也许对你的问题有帮助

当前输出层后面紧跟着
nn.SplitTable
,它分割输出通道并将输出张量转换为一个表。您还可以使用
parallelCriteria
组合不同的函数,以便将每个条件应用到输出表的相应条目上

有关详细信息,我建议您阅读Torch关于表格的文档

在注释之后,我添加了以下代码段来解决原始问题

M = 100
C = 4
H = 64
W = 64
dataIn = torch.rand(M, C, H, W)

layerOfTables = nn.Sequential()
-- Because SplitTable discards the dimension it is applied on, we insert
-- an additional dimension.
layerOfTables:add(nn.Reshape(M,C,1,H,W))
-- We want to split over the second dimension (i.e. channels).
layerOfTables:add(nn.SplitTable(2, 5))

-- We use ConcatTable in order to create paths accessing to the data for 
-- numereous number of criterions. Each branch from the ConcatTable will 
-- have access to the data (i.e. the output table).
criterionPath = nn.ConcatTable()
-- Starting from offset 1, NarrowTable will select 2 elements. Since you 
-- want to use this portion as a 2 dimensional channel, we need to combine
-- then by using JoinTable. Without JoinTable, the output will be again a 
-- table with 2 elements. 
criterionPath:add(nn.Sequential():add(nn.NarrowTable(1, 2)):add(nn.JoinTable(2)))
-- SelectTable is simplified version of NarrowTable, and it fetches the desired element.
criterionPath:add(nn.SelectTable(3))
criterionPath:add(nn.SelectTable(4))

layerOfTables:add(criterionPath)

-- Here goes the criterion container. You can use this as if it is a regular
-- criterion function (Please see the examples on documentation page).
criterionContainer = nn.ParallelCriterion()
criterionContainer:add(nn.BCECriterion())
criterionContainer:add(nn.MSECriterion())
criterionContainer:add(nn.MSECriterion())
因为我几乎使用了所有可能的表操作,所以看起来有点糟糕。然而,这是我解决这个问题的唯一方法。我希望它能帮助你和其他有同样问题的人。结果是这样的:

dataOut = layerOfTables:forward(dataIn)
print(dataOut)
{
  1 : DoubleTensor - size: 100x2x64x64
  2 : DoubleTensor - size: 100x1x64x64
  3 : DoubleTensor - size: 100x1x64x64
}

我不认为这是做这件事的方法。当我将nn.SplitTable(2)添加到输出中时,它给出了一个包含4个元素的表。每个元素都包含一个二维图像。但是,我想对表的前两个元素应用一个成本函数,对第三个通道应用另一个成本函数,对最后一个通道应用另一个成本函数。你将如何实现这一点?让我们把问题简化一点:假设我有一个卷积解码器网络,它输出一个大小为[M x C x H x W]的张量,其中M是批量大小,C是通道数(2的倍数),H=W是整数。我想将输出拆分为一个表,其中包含两个元素,每个元素的大小为[mxc/2xhxw]。我认为可以使用nn.SplitTable()完成,但我没有正确地完成。在分裂张量之后,我想对它们应用两个代价函数(这部分很简单)。你能给我一些提示吗?