1. Serialization
Torch provides 4 advanced methods to serialize or deserialize arbitrary lua/torch objects. These methods are abstracted from the file object and created for ease of operation.
The first two methods are used to serialize or deserialize from a file:
Torch.save (Filename,object [, format])
[Object] torch.load (filename [, format])
The following two functions are serialized or deserialized from a string:
[STR] Torch.serialize (object [, format])
[Object] Torch.deserialize (str [, format])
Serialization into a file can be used to save any type of data structure, such as sharing. The advantage of serializing a string is that the data structure can be stored in a database, or in third-party software.
The usage of each function is explained in detail below.
1.1 Torch.save (Filename,object [, format])
Writes object objects to the file filename, format can be selected as ASCII and binary, and binary by default. Binary types are platform-dependent, but are compact and fast to read and write. The ASCII type is platform-independent and can be shared across platforms.
--Arbitrary object:
obj = {
Mat = Torch.randn (10,10),
Name = ' 10 ',
Test = {
Entry = 1
}
}
--Save to disk:
Torch.save (' Test.dat ', obj)
1.2 [Object] torch.load (filename [, format])
Reads the object from the file.
--Given serialized object from section above, Reload:
obj = torch.load (' Test.dat ')
Print (obj)
--Would print:
--{[Mat] = doubletensor-size:10x10
--[Name] = string: "10"
--[Test] = table-size:0}
1.3 [STR] Torch.serialize (object [, format])
Serializes an object into a string (
--Arbitrary object:
obj = {
Mat = Torch.randn (10,10),
Name = ' 10 ',
Test = {
Entry = 1
}
}
--Serialize:
str = torch.serialize (obj)
1.4 [Object] Torch.deserialize (str [, format])
Deserializes an object from a string, which is still binary by default.
--Given serialized object from section above, deserialize:
obj = torch.deserialize (str)
Print (obj)
--Would print:
--{[Mat] = doubletensor-size:10x10
--[Name] = string: "10"
--[Test] = table-size:0}
Torch7 Learning Note (iii) Sequencialization