Skip to content

Instantly share code, notes, and snippets.

@andreaskoepf
Last active November 27, 2015 23:03
Show Gist options
  • Save andreaskoepf/8a2df7daea695d98d062 to your computer and use it in GitHub Desktop.
Save andreaskoepf/8a2df7daea695d98d062 to your computer and use it in GitHub Desktop.
Demo for custom 'read' function and versioning of torch objects.
--
local Foo, parent = torch.class('Foo')
Foo.__version = 1
function Foo:__init()
parent.__init(self)
end
-- serialize 'old' object
old = Foo.new()
s = torch.serialize(old)
-- simulate addition of 'newField':
Foo.__version = 2 -- only to show how to specify a version (which is serialized with the obj)
function Foo:__init()
parent.__init(self)
self.newField = 101
end
function Foo:restore(version)
self.newField = self.newField or 101
end
function Foo.read(obj, file, version)
local var = file:readObject()
for k,v in pairs(var) do
obj[k] = v
end
obj:restore(version)
return obj
end
-- deserialize old obj and check upgrade done by restored()
restored = torch.deserialize(s)
print(restored.newField)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment