Created
November 3, 2016 23:26
-
-
Save lebedov/85fb05bec33b8ee7d1b2daa4a3e747e3 to your computer and use it in GitHub Desktop.
How to copy a Torch model to a new type without explicit cloning.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- How to copy a Torch model to a new type without explicit cloning. | |
-- Based on code in checkpoints.lua in http://github.com/facebook/fb.resnet.torch | |
require 'torch' | |
function copy_convert(obj, t) | |
local copy = {} | |
for k, v in pairs(obj) do | |
if type(v) == 'table' then | |
copy[k] = copy_convert(v, t) | |
elseif torch.isTensor(v) then | |
copy[k] = v:type(t) | |
elseif k == '_type' then | |
copy[k] = t | |
else | |
copy[k] = v | |
end | |
end | |
if torch.typename(obj) then | |
torch.setmetatable(copy, torch.typename(obj)) | |
end | |
return copy | |
end | |
require 'cunn' | |
m_gpu = nn.Sequential() | |
m_gpu:add(nn.VolumetricConvolution(1, 4, 3, 3, 3, 1, 1, 1)) | |
m_gpu:add(nn.VolumetricMaxPooling(2, 2, 2, 2, 2, 2)) | |
m_gpu:cuda() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment