In [1]:
require 'nngraph'


Out[1]:
true	

In [4]:
input = nn.Identity()()

In [6]:
h1 = nn.Tanh()(nn.Linear(20,10)(input))
output = nn.Linear(10, 1)(h1)
mlp = nn.gModule({input},{output})

In [9]:
x = torch.rand(20)
dx = torch.rand(1)
mlp:updateOutput(x)
mlp:updateGradInput(x, dx)
mlp:accGradParameters(x, dx)

graph.dot(mlp.fg,'MLP','MLP')


/usr/local/share/lua/5.1/graph/graphviz.lua:135: graphviz layout failed
stack traceback:
	[C]: in function 'assert'
	/usr/local/share/lua/5.1/graph/graphviz.lua:135: in function 'graphvizFile'
	/usr/local/share/lua/5.1/graph/graphviz.lua:162: in function 'dot'
	[string "x = torch.rand(20)..."]:7: in main chunk
	[C]: in function 'xpcall'
	/usr/local/share/lua/5.1/itorch/main.lua:179: in function </usr/local/share/lua/5.1/itorch/main.lua:143>
	/usr/local/share/lua/5.1/lzmq/poller.lua:75: in function 'poll'
	/usr/local/share/lua/5.1/lzmq/impl/loop.lua:307: in function 'poll'
	/usr/local/share/lua/5.1/lzmq/impl/loop.lua:325: in function 'sleep_ex'
	/usr/local/share/lua/5.1/lzmq/impl/loop.lua:370: in function 'start'
	/usr/local/share/lua/5.1/itorch/main.lua:350: in main chunk
	[C]: in function 'require'
	[string "arg={'/Users/carpedm20/Library/Jupyter/runtim..."]:1: in main chunk

In [11]:
h1 = nn.Linear(20, 10)()
h2 = nn.Linear(10, 1)(nn.Tanh()(nn.Linear(10, 10)(nn.Tanh()(h1))))
mlp = nn.gModule({h1}, {h2})

x = torch.rand(20)
dx = torch.rand(1)
mlp:updateOutput(x)
mlp:updateGradInput(x, dx)
mlp:accGradParameters(x, dx)

-- draw graph (the forward graph, '.fg')
graph.dot(mlp.fg, 'MLP')


/usr/local/share/lua/5.1/graph/graphviz.lua:135: graphviz layout failed
stack traceback:
	[C]: in function 'assert'
	/usr/local/share/lua/5.1/graph/graphviz.lua:135: in function 'graphvizFile'
	/usr/local/share/lua/5.1/graph/graphviz.lua:162: in function 'dot'
	[string "h1 = nn.Linear(20, 10)()..."]:12: in main chunk
	[C]: in function 'xpcall'
	/usr/local/share/lua/5.1/itorch/main.lua:179: in function </usr/local/share/lua/5.1/itorch/main.lua:143>
	/usr/local/share/lua/5.1/lzmq/poller.lua:75: in function 'poll'
	/usr/local/share/lua/5.1/lzmq/impl/loop.lua:307: in function 'poll'
	/usr/local/share/lua/5.1/lzmq/impl/loop.lua:325: in function 'sleep_ex'
	/usr/local/share/lua/5.1/lzmq/impl/loop.lua:370: in function 'start'
	/usr/local/share/lua/5.1/itorch/main.lua:350: in main chunk
	[C]: in function 'require'
	[string "arg={'/Users/carpedm20/Library/Jupyter/runtim..."]:1: in main chunk

In [13]:
local function get_net()
    local input = nn.Identity()()
    
    local h1 = nn.Linear(20,10)(input)
    local h2 = nn.Sigmoid()(h1)
    local output = nn.Linear(10,1)(h2)
    
    -- Annotate nodes with local variable names
    -- The local variables at the given stack level are inspected.
    nngraph.annotateNodes()
    return nn.gModule({input},{output})
end

mlp = get_net(10,10)
x = torch.rand(20)
dx = torch.rand(1)

mlp:updateOutput(x)

-- Computing the gradient of the module with respect to its own input.
-- This is returned in gradInput.
-- Also, the gradInput state variable is updated accordingly.
mlp:updateGradInput(x, dx)

-- gradInput
-- This contains the gradients with respect to the inputs of the module,
-- computed with the last call of updateGradInput(input, gradOutput).

-- Computing the gradient of the module with respect to its ownparameters.
mlp:accGradParameters(x, dx)

print(mlp.fg)


Out[13]:
graph.Graph
{
  nodes : 
    {
      1 : 
        nngraph.Node
        {
          data : 
            {
              annotations : 
                {
                  _debugLabel : [[string "local function get_net()..."]]:6
                  name : output
                }
              input : 
                {
                  1 : DoubleTensor - size: 10
                }
              module : 
                nn.Linear(10 -> 1)
                {
                  gradBias : DoubleTensor - size: 1
                  weight : DoubleTensor - size: 1x10
                  bias : DoubleTensor - size: 1
                  gradInput : DoubleTensor - size: 10
                  gradWeight : DoubleTensor - size: 1x10
                  output : DoubleTensor - size: 1
                }
              mapindex : 
                {
                  1 : table: 0x0ac2c568
                  table: 0x0ac2c568 : 1
                }
              forwardNodeId : 1
              gradOutput : 
                {
                  1 : DoubleTensor - size: 1
                }
            }
          visited : false
          id : 1
          marked : false
          children : 
            {
              1 : 
                nngraph.Node
                {
                  data : table: 0x0ac2d448
                  visited : false
                  id : 2
                  marked : false
                  children : table: 0x0ac2e860
                }
              nngraph.Node : 1
            }
        }
      2 : 
        nngraph.Node
        {
          data : 
            {
              annotations : 
                {
                  _debugLabel : [[C]]:-1
                }
              input : 
                {
                  1 : DoubleTensor - size: 1
                }
              mapindex : 
                {
                  1 : table: 0x0ac2cc98
                  table: 0x0ac2cc98 : 1
                }
              forwardNodeId : 2
              gradOutput : 
                {
                  1 : DoubleTensor - size: 1
                }
            }
          visited : false
          id : 2
          marked : false
          children : table: 0x0ac2e860
        }
      3 : 
        nngraph.Node
        {
          data : 
            {
              annotations : 
                {
                  _debugLabel : [[string "local function get_net()..."]]:5
                  name : h2
                }
              input : 
                {
                  1 : DoubleTensor - size: 10
                }
              module : 
                nn.Sigmoid
                {
                  gradInput : DoubleTensor - size: 10
                  output : DoubleTensor - size: 10
                }
              mapindex : 
                {
                  1 : table: 0x0ac2bfb8
                  table: 0x0ac2bfb8 : 1
                }
              forwardNodeId : 3
              gradOutput : 
                {
                  1 : DoubleTensor - size: 10
                }
            }
          visited : false
          id : 3
          marked : false
          children : 
            {
              1 : 
                nngraph.Node
                {
                  data : table: 0x0ac2cc98
                  visited : false
                  id : 1
                  marked : false
                  children : table: 0x0ac2e9b8
                }
              nngraph.Node : 1
            }
        }
      4 : 
        nngraph.Node
        {
          data : 
            {
              annotations : 
                {
                  _debugLabel : [[string "local function get_net()..."]]:4
                  name : h1
                }
              input : 
                {
                  1 : DoubleTensor - size: 20
                }
              module : 
                nn.Linear(20 -> 10)
                {
                  gradBias : DoubleTensor - size: 10
                  weight : DoubleTensor - size: 10x20
                  bias : DoubleTensor - size: 10
                  gradInput : DoubleTensor
Out[13]:
 - size: 20
                  gradWeight : DoubleTensor - size: 10x20
                  output : DoubleTensor - size: 10
                }
              mapindex : 
                {
                  1 : table: 0x0ac2b560
                  table: 0x0ac2b560 : 1
                }
              forwardNodeId : 4
              gradOutput : 
                {
                  1 : DoubleTensor - size: 10
                }
            }
          visited : false
          id : 4
          marked : false
          children : 
            {
              1 : 
                nngraph.Node
                {
                  data : table: 0x0ac2c568
                  visited : false
                  id : 3
                  marked : false
                  children : table: 0x0ac2ebf0
                }
              nngraph.Node : 1
            }
        }
      5 : 
        nngraph.Node
        {
          data : 
            {
              annotations : 
                {
                  _debugLabel : [[string "local function get_net()..."]]:2
                  name : input
                }
              input : 
                {
                  1 : DoubleTensor - size: 20
                }
              module : 
                nn.Identity
                {
                  gradInput : DoubleTensor - size: 20
                  output : DoubleTensor - size: 20
                }
              mapindex : 
                {
                  1 : table: 0x0ac2d928
                  table: 0x0ac2d928 : 1
                }
              forwardNodeId : 5
              gradOutput : 
                {
                  1 : DoubleTensor - size: 20
                }
            }
          visited : false
          id : 5
          marked : false
          children : 
            {
              1 : 
                nngraph.Node
                {
                  data : table: 0x0ac2bfb8
                  visited : false
                  id : 4
                  marked : false
                  children : table: 0x0ac2ee50
                }
              nngraph.Node : 1
            }
        }
      6 : 
        nngraph.Node
        {
          data : 
            {
              annotations : 
                {
                  _debugLabel : [[C]]:-1
                }
              input : 
                {
                  1 : DoubleTensor - size: 20
                }
              mapindex : table: 0x0ac2da38
              forwardNodeId : 6
              gradOutput : 
                {
                  1 : DoubleTensor - size: 20
                }
            }
          visited : false
          id : 6
          marked : false
          children : 
            {
              1 : 
                nngraph.Node
                {
                  data : table: 0x0ac2b560
                  visited : false
                  id : 5
                  marked : false
                  children : table: 0x0ac2f080
                }
              nngraph.Node : 1
            }
        }
      nngraph.Node : 2
      nngraph.Node : 3
      nngraph.Node : 4
      nngraph.Node : 6
      nngraph.Node : 1
      nngraph.Node : 5
    }
  edges : 
    {
      1 : 
        graph.Edge
        {
          to : 
            nngraph.Node
            {
              data : 
                {
                  annotations : table: 0x0ac2d4f8
                  input : table: 0x0ac2d4a8
                  mapindex : table: 0x0ac2d558
                  forwardNodeId : 2
                  gradOutput : table: 0x0ac32c98
                }
              visited : false
              id : 2
              marked : false
              children : table: 0x0ac2e860
            }
          from : 
            nngraph.Node
            {
              data : 
                {
                  annotations : table: 0x0ac2cd20
                  input : table: 0x0ac2c7c0
                  module : nn.Linear(10 -> 1)
                  mapindex : table: 0x0ac2cd80
                  forwardNodeId : 1
                  gradOutput : table: 0x0ac2d6a8
             
Out[13]:
   }
              visited : false
              id : 1
              marked : false
              children : 
                {
                  1 : nngraph.Node
                  nngraph.Node : 1
                }
            }
        }
      2 : 
        graph.Edge
        {
          to : 
            nngraph.Node
            {
              data : 
                {
                  annotations : table: 0x0ac2cd20
                  input : table: 0x0ac2c7c0
                  module : nn.Linear(10 -> 1)
                  mapindex : table: 0x0ac2cd80
                  forwardNodeId : 1
                  gradOutput : table: 0x0ac2d6a8
                }
              visited : false
              id : 1
              marked : false
              children : 
                {
                  1 : nngraph.Node
                  nngraph.Node : 1
                }
            }
          from : 
            nngraph.Node
            {
              data : 
                {
                  annotations : table: 0x0ac2c5f0
                  input : table: 0x0ac2c210
                  module : nn.Sigmoid
                  mapindex : table: 0x0ac2c650
                  forwardNodeId : 3
                  gradOutput : table: 0x0ac32db0
                }
              visited : false
              id : 3
              marked : false
              children : 
                {
                  1 : nngraph.Node
                  nngraph.Node : 1
                }
            }
        }
      3 : 
        graph.Edge
        {
          to : 
            nngraph.Node
            {
              data : 
                {
                  annotations : table: 0x0ac2c5f0
                  input : table: 0x0ac2c210
                  module : nn.Sigmoid
                  mapindex : table: 0x0ac2c650
                  forwardNodeId : 3
                  gradOutput : table: 0x0ac32db0
                }
              visited : false
              id : 3
              marked : false
              children : 
                {
                  1 : nngraph.Node
                  nngraph.Node : 1
                }
            }
          from : 
            nngraph.Node
            {
              data : 
                {
                  annotations : table: 0x0ac2c040
                  input : table: 0x0ac2b8d0
                  module : nn.Linear(20 -> 10)
                  mapindex : table: 0x0ac2c0a0
                  forwardNodeId : 4
                  gradOutput : table: 0x0ac32df8
                }
              visited : false
              id : 4
              marked : false
              children : 
                {
                  1 : nngraph.Node
                  nngraph.Node : 1
                }
            }
        }
      4 : 
        graph.Edge
        {
          to : 
            nngraph.Node
            {
              data : 
                {
                  annotations : table: 0x0ac2c040
                  input : table: 0x0ac2b8d0
                  module : nn.Linear(20 -> 10)
                  mapindex : table: 0x0ac2c0a0
                  forwardNodeId : 4
                  gradOutput : table: 0x0ac32df8
                }
              visited : false
              id : 4
              marked : false
              children : 
                {
                  1 : nngraph.Node
                  nngraph.Node : 1
                }
            }
          from : 
            nngraph.Node
            {
              data : 
                {
                  annotations : table: 0x0ac2b840
                  input : table: 0x0ac328e8
                  module : nn.Identity
                  mapindex : table: 0x0ac2b868
                  forwardNodeId : 5
                  gradOutput : table: 0x0ac32e68
                }
              visited : false
              id : 5
              marked : false
              children : 
                {
                  1 : nngraph.Node
                  nngraph.Node : 1
                }
            }
        }
      5 : 
        graph.Edge
        {
          to :
Out[13]:
 
            nngraph.Node
            {
              data : 
                {
                  annotations : table: 0x0ac2b840
                  input : table: 0x0ac328e8
                  module : nn.Identity
                  mapindex : table: 0x0ac2b868
                  forwardNodeId : 5
                  gradOutput : table: 0x0ac32e68
                }
              visited : false
              id : 5
              marked : false
              children : 
                {
                  1 : nngraph.Node
                  nngraph.Node : 1
                }
            }
          from : 
            nngraph.Node
            {
              data : 
                {
                  annotations : table: 0x0ac2d9d8
                  input : table: 0x0ac2d988
                  mapindex : table: 0x0ac2da38
                  forwardNodeId : 6
                  gradOutput : table: 0x0ac32eb0
                }
              visited : false
              id : 6
              marked : false
              children : 
                {
                  1 : nngraph.Node
                  nngraph.Node : 1
                }
            }
        }
      graph.Edge : 3
      graph.Edge : 2
      graph.Edge : 4
      graph.Edge : 5
      graph.Edge : 1
    }
}

In [14]:
nngraph.setDebug(true)

local function get_network()
    local input = nn.Identity()()
    
    local h1 = nn.Linear(20, 10)(input)
    local h2 = nn.Sigmoid()(h1)
    local output = nn.Linear(10,1)(h2)
    
    nngraph.annotateNodes()
    return nn.gModule({input},{output})
end

mlp = get_network()
mlp.name = 'MYMLPError'
x=torch.rand(15)
local o, error = pcall(function() mlp:updateOutput(x) end)

In [16]:
print(o, error)


Out[16]:
nil	function: builtin#20	

In [18]:
function get_rnn(input_size, rnn_size)
    local input = nn.Identity()()
    local prev_h = nn.Identity()()
    
    local i2h = nn.Linear(input_size, rnn_size)(input)
    local h2h = nn.Linear(rnn_size, rnn_size)(prev_h)
    local added_h = nn.CAddTable()({i2h,h2h})
    local next_h = nn.Tanh()(added_h)
    
    nngraph.annotateNodes()
    return nn.gModule({input, prev_h}, {next_h})
end

local rnn_net = get_rnn(128,128)

In [19]:
-- rnn with one depth (layer) two input

local function get_rnn2(input_size, rnn_size)
    local input1 = nn.Identity()()
    local input2 = nn.Identity()()
    local prev_h = nn.Identity()()
    local rnn_net1 = get_rnn(128, 128)({input1, prev_h})
    local rnn_net2 = get_rnn(128, 128)({input2, rnn_net1})
    nngraph.annotateNodes()
    return nn.gModule({input1, input2, prev_h}, {rnn_net2})
end

local rnn_net2 = get_rnn2(128, 128)

In [20]:
local function get_rnn2(input_size, rnn_size)
    local input1 = nn.Identity()():annotate{graphAttributes = {style='filled', fillcolor='blue'}}
    local input2 = nn.Identity()():annotate{graphAttributes = {style='filled', fillcolor='blue'}}
    local prev_h = nn.Identity()():annotate{graphAttributes = {style='filled', fillcolor='blue'}}
    local rnn_net1 = get_rnn(128, 128)({input1, prev_h}):annotate{graphAttributes = {style='filled', fillcolor='yellow'}}
    local rnn_net2 = get_rnn(128, 128)({input2, rnn_net1}):annotate{graphAttributes = {style='filled', fillcolor='green'}}
    nngraph.annotateNodes()
    return nn.gModule({input1, input2, prev_h}, {rnn_net2})
end
local rnn_net3 = get_rnn2(128, 128)
graph.dot(rnn_net3.fg, 'rnn_net3', 'rnn_net3')


/usr/local/share/lua/5.1/graph/graphviz.lua:135: graphviz layout failed
stack traceback:
	[C]: in function 'assert'
	/usr/local/share/lua/5.1/graph/graphviz.lua:135: in function 'graphvizFile'
	/usr/local/share/lua/5.1/graph/graphviz.lua:162: in function 'dot'
	[string "local function get_rnn2(input_size, rnn_size)..."]:11: in main chunk
	[C]: in function 'xpcall'
	/usr/local/share/lua/5.1/itorch/main.lua:179: in function </usr/local/share/lua/5.1/itorch/main.lua:143>
	/usr/local/share/lua/5.1/lzmq/poller.lua:75: in function 'poll'
	/usr/local/share/lua/5.1/lzmq/impl/loop.lua:307: in function 'poll'
	/usr/local/share/lua/5.1/lzmq/impl/loop.lua:325: in function 'sleep_ex'
	/usr/local/share/lua/5.1/lzmq/impl/loop.lua:370: in function 'start'
	/usr/local/share/lua/5.1/itorch/main.lua:350: in main chunk
	[C]: in function 'require'
	[string "arg={'/Users/carpedm20/Library/Jupyter/runtim..."]:1: in main chunk

In [ ]: