nn.graph and nn.module

nn.graph and nn.module

https://www.cs.ox.ac.uk/people/nando.defreitas/machinelearning/practicals/practical5.pdf

https://github.com/torch/nngraph

———————————————————————-

net
nn.Sequential {
  [input -> (1) -> (2) -> (3) -> (4) -> (5) -> (6) -> (7) -> (8) -> (9) -> (10) -> (11) -> (12) -> (13) -> output]
  (1): nn.SpatialConvolution(1 -> 6, 5x5)
  (2): nn.ReLU
  (3): nn.SpatialMaxPooling(2x2, 2,2)
  (4): nn.SpatialConvolution(6 -> 16, 5x5)
  (5): nn.ReLU
  (6): nn.SpatialMaxPooling(2x2, 2,2)
  (7): nn.View(400)
  (8): nn.Linear(400 -> 120)
  (9): nn.ReLU
  (10): nn.Linear(120 -> 84)
  (11): nn.ReLU
  (12): nn.Linear(84 -> 10)
  (13): nn.LogSoftMax
}

To access

net:get(6).output (see get and output).

Ref: http://stackoverflow.com/questions/36364190/obtaining-intermediate-layers-activation-values-given-input-examples

 

http://stackoverflow.com/questions/33545325/torch-backward-through-gmodule

https://groups.google.com/forum/#!topic/torch7/4f_wMQ6G2io

https://groups.google.com/forum/#!topic/torch7/6elQuBmTKLo

how-to-access-intermediate-layers-outputs-using-nngraph

http://stackoverflow.com/questions/38616179/how-to-access-intermediate-layers-outputs-using-nngraph

 

enter image description here

require 'torch'
require 'nn'
require 'nngraph'

function CreateModule(input_size)
    local input = nn.Identity()()   -- network input

    local nn_module_1 = nn.Linear(input_size, 100)(input)
    local nn_module_2 = nn.Linear(100, input_size)(nn_module_1)

    local output = nn.CMulTable()({input, nn_module_2})

    -- pack a graph into a convenient module with standard API (:forward(), :backward())
    return nn.gModule({input}, {output})
end


input = torch.rand(30)

my_module = CreateModule(input:size(1))

output = my_module:forward(input)
criterion_err = torch.rand(output:size())

gradInput = my_module:backward(input, criterion_err)
print(gradInput)


ref
https://groups.google.com/forum/#!topic/torch7/7uoKNv_InVA
https://groups.google.com/forum/#!topic/torch7/ItkpcJicczY
https://groups.google.com/forum/#!topic/torch7/KheG-Rlfa9k

require 'nngraph'

h1 = nn.Linear(20, 20)()
h2 = nn.Linear(10, 10)()
hh1 = nn.Linear(20, 1)(nn.Tanh()(h1))
hh2 = nn.Linear(10, 1)(nn.Tanh()(h2))
madd = nn.CAddTable()({hh1, hh2})
oA = nn.Sigmoid()(madd)
oB = nn.Tanh()(madd)
gmod = nn.gModule({h1, h2}, {oA, oB})

for indexNode, node in ipairs(gmod.forwardnodes) do
  if node.data.module then
    print(node.data.module)
  end
end

how to share the parameters in nngraph #114


https://github.com/torch/nngraph/issues/114
http://kbullaughey.github.io/lstm-play/2015/09/18/introduction-to-nngraph.html


deep_learning_with_torch_step_4_nngraph/
http://rnduja.github.io/2015/10/07/deep_learning_with_torch_step_4_nngraph/
https://github.com/torch/nngraph

Ref:https://github.com/Element-Research/rnn/issues/89

local net = nn.gModule({input}, {output})

for _,node in ipairs(net.forwardnodes) do
        print(node.data)
end
for _,node in ipairs(net.backwardnodes) do
        print(node.data)
end


Ref: https://github.com/torch/nngraph/issues/102
m = nn.gModule()
m.forwardnodes[i].data.module

m = nn.Sequential()
m:add(nn.Linear(5,5))
m:get(1)
 
Ref:
https://github.com/bshillingford/nnquery
https://groups.google.com/forum/#!topic/torch7/TFECV5ihRSw
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s