(原)torch中的序列化

转载请注明出处:

http://www.cnblogs.com/darkknightzh/p/6591667.html

参考网址:

https://github.com/torch/torch7/blob/master/doc/serialization.md

1. 数据与文件之间的序列化/反序列化操作

1.1 torch.save(filename, object [, format, referenced])

format可选binary(默认)和ascii。binary依赖操作系统,但更容易读写。ascii不依赖操作系统。

referenced指定是否需要保存object references(https://github.com/torch/torch7/blob/master/doc/file.md#torch.File.referenced)。当保存时设置为true,则读取时若设置为false,则不能成功读取。

说明:感觉如果要保存多个变量,需要使用列表:

obj = {   -- arbitrary object
   mat = torch.randn(10,10),
   name = '10',
   test = {
      entry = 1
   }
}

torch.save('test.dat', obj)  -- save to disk

1.2 [object] torch.load(filename [, format, referenced])

format可选ascii,binary(默认),b32,b64。当保存到32/64位的系统上,可以使用b32/b64。

obj = torch.load('test.dat')  -- given serialized object from section above, reload

print(obj)
-- will print:
-- {[mat]  = DoubleTensor - size: 10x10
--  [name] = string : "10"
--  [test] = table - size: 0}

2. 数据与字符串之间的序列化/反序列化操作

2.1 [str] torch.serialize(object [, format])

format可选binary(默认)和ascii,binary依赖操作系统,但更容易读写。ascii不依赖操作系统。

obj = {   -- arbitrary object
   mat = torch.randn(10,10),
   name = '10',
   test = {
      entry = 1
   }
}

str = torch.serialize(obj)  -- serialize

2.2 [object] torch.deserialize(str [, format])

format可选ascii,binary(默认)。
obj = torch.deserialize(str)  -- given serialized object from section above, deserialize

print(obj)
-- will print:
-- {[mat]  = DoubleTensor - size: 10x10
--  [name] = string : "10"
--  [test] = table - size: 0}

 

posted on 2017-03-20 21:21  darkknightzh  阅读(1202)  评论(0编辑  收藏  举报

导航