How to save a tensor to a binary file

3,230 views
Skip to first unread message

fireman

unread,
Oct 15, 2016, 6:57:25 PM10/15/16
to torch7
Hi everyone:

I am new to Lua/Torch and hope  you guys can help me. I would like to save the weight and bias of a nn.SpatialConvolution to a raw binary file. But I just could not find any thing that can do this in Lua. Do I miss anything?

Thanks,

Ge

alban desmaison

unread,
Oct 16, 2016, 5:03:21 AM10/16/16
to torch7
Hi,

torch.save already save data in a byte format. The option to save as "ascii" can optionally be used (https://github.com/torch/torch7/blob/master/doc/serialization.md#torchsavefilename-object--format-referenced)

fireman

unread,
Oct 17, 2016, 12:53:22 PM10/17/16
to torch7
I would like to save the weights and bias in a more compact format and read it somewhere else. I do not want to use torch saving format. 

I am asking a general lua question about how to save some data in binary file. 

fireman

unread,
Oct 30, 2016, 1:51:43 AM10/30/16
to torch7
I can not believe Lua has no way to save data to a binary file????

soumith

unread,
Oct 30, 2016, 10:35:03 PM10/30/16
to torch7 on behalf of fireman
you can open a torch.DiskFile and write weight:storage() and bias:storage() to the diskfile using: https://github.com/torch/torch7/blob/master/doc/file.md#write-methods

For example:

require 'nn'
a=torch.DiskFile('abc.t7', 'w'):binary()
m = nn.SpatialConvolution(3,16,5,5)
m:float()
a:writeFloat(m.weight:storage())
a:writeFloat(m.bias:storage())
a:close()

--
You received this message because you are subscribed to the Google Groups "torch7" group.
To unsubscribe from this group and stop receiving emails from it, send an email to torch7+unsubscribe@googlegroups.com.
To post to this group, send email to tor...@googlegroups.com.
Visit this group at https://groups.google.com/group/torch7.
For more options, visit https://groups.google.com/d/optout.

Daniel Galvez

unread,
Nov 2, 2016, 8:24:56 PM11/2/16
to torch7
Note that saving like this leaves out the associated dimensions of the tensor, since it's just the storage. The data is stored row-major.
To unsubscribe from this group and stop receiving emails from it, send an email to torch7+un...@googlegroups.com.

fireman

unread,
Nov 3, 2016, 5:53:21 PM11/3/16
to torch7
I tried that but seems there is something wrong. I just want to load a previous trained model, find the Convolution layer and dump the weight and bias to a file. Here is my code

local model = //local a previous trained torch cnn mode from file

 local a = torch.DiskFile(opt.output..'.t7', 'w'):binary()

  for i = 1, #model do
    local layer = model:get(i)
    local layer_type = torch.type(layer)
    print(string.format('Layer %d: %s', i, layer_type))
    if layer_type == 'nn.SpatialConvolution' then
      layer:float()
      if opt.writedata then
        a:writeFloat(layer.weight:storage())
        a:writeFloat(layer.bias:storage())
      end
    end

It did write to a file, but the file size is very big, it is even bigger than my original model.

soumith

unread,
Nov 3, 2016, 6:33:07 PM11/3/16
to torch7 on behalf of fireman
a:writeFloat(layer.weight:storage()) is wrong. you need to do:

local w = layer.weight
a:writeFloat(w:storage().new(w:storage(), w:storageOffset(), w:nElement())

--

fireman

unread,
Nov 4, 2016, 12:41:04 PM11/4/16
to torch7
Still does not seem correct. When I read the file from matlab on windows, the weights are 

   1.0e+37 *

    0.0000
       NaN
    0.0000
   -0.0000
    3.0767
    0.0000
    0.0000
    0.0000
    0.0000
   -0.0000


On Thursday, November 3, 2016 at 3:33:07 PM UTC-7, smth chntla wrote:
a:writeFloat(layer.weight:storage()) is wrong. you need to do:

local w = layer.weight
a:writeFloat(w:storage().new(w:storage(), w:storageOffset(), w:nElement())
On Thu, Nov 3, 2016 at 5:53 PM, fireman via torch7 <torch7+APn2wQeW3esQD-dFTzB55wMyBRX8xJbzqz3UhAPNuLWPpnI6ph...@googlegroups.com> wrote:
I tried that but seems there is something wrong. I just want to load a previous trained model, find the Convolution layer and dump the weight and bias to a file. Here is my code

local model = //local a previous trained torch cnn mode from file

 local a = torch.DiskFile(opt.output..'.t7', 'w'):binary()

  for i = 1, #model do
    local layer = model:get(i)
    local layer_type = torch.type(layer)
    print(string.format('Layer %d: %s', i, layer_type))
    if layer_type == 'nn.SpatialConvolution' then
      layer:float()
      if opt.writedata then
        a:writeFloat(layer.weight:storage())
        a:writeFloat(layer.bias:storage())
      end
    end

It did write to a file, but the file size is very big, it is even bigger than my original model.

--
You received this message because you are subscribed to the Google Groups "torch7" group.
To unsubscribe from this group and stop receiving emails from it, send an email to torch7+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages