加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
utils.lua 1.36 KB
一键复制 编辑 原始数据 按行查看 历史
local utils = {}
function utils.istorchclass(x)
return type(x) == 'table' and torch.typename(x)
end
function utils.istable(x)
return type(x) == 'table' and not torch.typename(x)
end
--[[ Returns a useful error message when a nngraph.Node is expected. ]]
function utils.expectingNodeErrorMessage(badVal, array, idx)
if badVal == nil then
return string.format('%s[%d] is nil (typo / bad index?)', array, idx)
elseif torch.isTypeOf(badVal, 'nn.Module') then
local errStr = '%s[%d] is an nn.Module, specifically a %s, but the ' ..
'only valid thing to pass is an instance of ' ..
'nngraph.Node. Did you forget a second set of parens, ' ..
'which convert a nn.Module to a nngraph.Node?'
return string.format(errStr, array, idx, torch.typename(badVal))
else
local errStr = '%s[%d] should be an nngraph.Node but is of type %s'
return string.format(errStr, array, idx,
torch.typename(badVal) or type(badVal))
end
end
--[[ Lua 5.2+ removed table.maxn, provide fallback implementation. ]]
if table.maxn then
utils.tableMaxN = table.maxn
else
function utils.tableMaxN(tbl)
local max = 0
for k, v in pairs(tbl) do
if type(k) == 'number' and k > max then
max = k
end
end
return max
end
end
return utils
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化