上下文:我正在尝试修改 this Facebook's ResNet feature extractor script对图像进行分类并打印 ImageNet 类标签。 假设我在 torch 中有模型:
local model = torch.load('resnet-101.t7')
local output = model:forward(img:cuda()):squeeze(1)
这给了我每门课的分数。我想获得前 5 个类别及其概率。我认为要将分数转换为概率,我应该首先使用 SoftMax 层。
所以我这样做:
local model = torch.load('resnet-101.t7')
local softMaxLayer = cudnn.LogSoftMax()
model:add(softMaxLayer)
local output = model:forward(img:cuda()):squeeze(1)
但是当我运行它时,我得到:
/SpatialSoftMax.lua:38: bad argument #1 to 'resizeAs' (torch.DoubleTensor expected, got torch.CudaTensor)
该模型对我来说看起来不错:(仅显示最后一层)
...
(9): cudnn.SpatialAveragePooling(7,7,1,1)
(10): nn.View(2048)
(11): nn.Linear(2048 -> 1000)
(12): cudnn.LogSoftMax
}
对可能出现的问题有什么想法吗?
最佳答案
图层具有与其关联的类型。默认情况下,您会获得 double 类型
local softMaxLayer = cudnn.LogSoftMax():cuda()
model:add(softMaxLayer)
关于lua - 在 Torch 中将 Softmax 添加到 ResNet 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35527360/