Search code examples
neural-networktorch

torch7 : print matrix in text file with lines longer than 80 characters


I'm trying to recuperate the parameters of this NN:

    nn.Sequential {
      [input -> (1) -> (2) -> (3) -> (4) -> (5) -> (6) -> output]
      (1): nn.Linear(4 -> 200)
      (2): nn.Tanh
      (3): nn.Linear(200 -> 200)
      (4): nn.Tanh
      (5): nn.Linear(200 -> 3)
      (6): nn.LogSoftMax         
    }

using this code :

print(mlp:get(1).weight)
print(mlp:get(1).bias)

print(mlp:get(3).weight)
print(mlp:get(3).bias)

print(mlp:get(5).weight)
print(mlp:get(5).bias)

When saving the output .lua file into text file using this command line:

>>th  'MyScript.lua' > NNParameters.txt

I get all weight matrix wrapped into segments of six columns each (Columns 1 to 6 ... Columns 193 to 198 ... Columns 199 to 200).

Is there anyway to prevent text from being wrapped and displaying weight matrix in only one block?

Thank you.


Solution

  • printT = function(t)
       t = t:view(-1)
       for i=1,t:nElement() do
          io.write(t[i] .. ',')
       end
    end
    
    printT(mlp:get(1).weight)
    printT(mlp:get(1).bias)
    
    printT(mlp:get(3).weight)
    printT(mlp:get(3).bias)
    
    printT(mlp:get(5).weight)
    printT(mlp:get(5).bias)