Torch7 Study Notes (iii) sequencialization

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.