lua - 在 Torch 中将 Softmax 添加到 ResNet 模型

标签 lua deep-learning torch softmax resnet

上下文:我正在尝试修改 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/

相关文章:

list - Lua - 如何识别一个列表中的任何值不存在于另一个列表中的位置

lua - Redis 链接命令

tensorflow - 如何使用 Keras 在 TensorBoard 中显示自定义图像?

machine-learning - `Torch`(即 `nn.SpatialConvolution` )中的卷积层和 `Pytorch`(即 `torch.nn.Conv2d` )中的卷积层有什么不同

lua - Lua为什么要这样调整结果呢?

types - 表中的 Lua 表显示为 Nil

python - 至少指定一个标签必须在 y_true 中

machine-learning - 如何使用Keras的ModelCheckpoint继续训练模型

python-decorators - 为什么我不能使用类对 `torch.jit.script` 装饰器下的函数参数进行类型注释?

performance - 使张量中的值符合给定范围