(原)torch中的序列化

轉載請註明出處:html

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

參考網址:github

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

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,則不能成功讀取。code

說明:感受若是要保存多個變量,須要使用列表:orm

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。htm

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不依賴操做系統。blog

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}
相關文章
相關標籤/搜索