• Stars
    star
    299
  • Rank 139,269 (Top 3 %)
  • Language
    Lua
  • License
    Other
  • Created over 11 years ago
  • Updated over 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Graph Computation for nn

Neural Network Graph Package

Build Status

This package provides graphical computation for nn library in Torch.

Requirements

You do not need graphviz to be able to use this library, but if you have it you will be able to display the graphs that you have created. For installing the package run the appropriate command below:

# Mac users
brew install graphviz
# Debian/Ubuntu users
sudo apt-get install graphviz -y

Usage

Plug: A more explanatory nngraph tutorial by Nando De Freitas of Oxford

The aim of this library is to provide users of nn package with tools to easily create complicated architectures. Any given nn module is going to be bundled into a graph node. The __call__ operator of an instance of nn.Module is used to create architectures as if one is writing function calls.

Two hidden layers MLP

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')

Read this diagram from top to bottom, with the first and last nodes being dummy nodes that regroup all inputs and outputs of the graph. The module entry describes the function of the node, as applies to input, and producing a result of the shape gradOutput; mapindex contains pointers to the parent nodes.

To save the graph on file, specify the file name, and both a dot and svg files will be saved. For example, you can type:

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

You can also use the __unm__ and __sub__ operators to replace all __call__:

h1 = - nn.Linear(20,10)
h2 = h1
     - nn.Tanh()
     - nn.Linear(10,10)
     - nn.Tanh()
     - nn.Linear(10, 1)
mlp = nn.gModule({h1}, {h2})

A network with 2 inputs and 2 outputs

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})

x1 = torch.rand(20)
x2 = torch.rand(10)

gmod:updateOutput({x1, x2})
gmod:updateGradInput({x1, x2}, {torch.rand(1), torch.rand(1)})
graph.dot(gmod.fg, 'Big MLP')

Alternatively, you can use - to make your code looks like the data flow:

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

A network with containers

Another net that uses container modules (like ParallelTable) that output a table of outputs.

m = nn.Sequential()
m:add(nn.SplitTable(1))
m:add(nn.ParallelTable():add(nn.Linear(10, 20)):add(nn.Linear(10, 30)))
input = nn.Identity()()
input1, input2 = m(input):split(2)
m3 = nn.JoinTable(1)({input1, input2})

g = nn.gModule({input}, {m3})

indata = torch.rand(2, 10)
gdata = torch.rand(50)
g:forward(indata)
g:backward(indata, gdata)

graph.dot(g.fg, 'Forward Graph')
graph.dot(g.bg, 'Backward Graph')

More fun with graphs

A multi-layer network where each layer takes output of previous two layers as input.

input = nn.Identity()()
L1 = nn.Tanh()(nn.Linear(10, 20)(input))
L2 = nn.Tanh()(nn.Linear(30, 60)(nn.JoinTable(1)({input, L1})))
L3 = nn.Tanh()(nn.Linear(80, 160)(nn.JoinTable(1)({L1, L2})))

g = nn.gModule({input}, {L3})

indata = torch.rand(10)
gdata = torch.rand(160)
g:forward(indata)
g:backward(indata, gdata)

graph.dot(g.fg, 'Forward Graph')
graph.dot(g.bg, 'Backward Graph')

As your graph getting bigger and more complicated, the nested parentheses may become confusing. In this case, using - to chain the modules is a clearer and easier way:

input = - nn.Identity()
L1 =  input 
     - nn.Linear(10, 20) 
     - nn.Tanh()
L2 =  { input, L1 }
     -  nn.JoinTable(1)
     -  nn.Linear(30,60) 
     -  nn.Tanh()
L3 = { L1,L2 }
     - nn.JoinTable(1)
     - nn.Linear(80,160)
     - nn.Tanh()
g = nn.gModule({input},{L3})

Annotations

It is possible to add annotations to your network, such as labeling nodes with names or attributes which will show up when you graph the network. This can be helpful in large graphs.

For the full list of graph attributes see the graphviz documentation.

input = nn.Identity()()
L1 = nn.Tanh()(nn.Linear(10, 20)(input)):annotate{
   name = 'L1', description = 'Level 1 Node',
   graphAttributes = {color = 'red'}
}
L2 = nn.Tanh()(nn.Linear(30, 60)(nn.JoinTable(1)({input, L1}))):annotate{
   name = 'L2', description = 'Level 2 Node',
   graphAttributes = {color = 'blue', fontcolor = 'green'}
}
L3 = nn.Tanh()(nn.Linear(80, 160)(nn.JoinTable(1)({L1, L2}))):annotate{
   name = 'L3', description = 'Level 3 Node',
   graphAttributes = {color = 'green',
   style = 'filled', fillcolor = 'yellow'}
}

g = nn.gModule({input},{L3})

indata = torch.rand(10)
gdata = torch.rand(160)
g:forward(indata)
g:backward(indata, gdata)

graph.dot(g.fg, 'Forward Graph', '/tmp/fg')
graph.dot(g.bg, 'Backward Graph', '/tmp/bg')

In this case, the graphs are saved in the following 4 files: /tmp/{fg,bg}.{dot,svg}.

Debugging

With nngraph, one can create very complicated networks. In these cases, finding errors can be hard. For that purpose, nngraph provides several useful utilities. The following code snippet shows how to use local variable names for annotating the nodes in a graph and how to enable debugging mode that automatically creates an svg file with error node marked in case of a runtime error.

require 'nngraph'

-- generate SVG of the graph with the problem node highlighted
-- and hover over the nodes in svg to see the filename:line_number info
-- nodes will be annotated with local variable names even if debug mode is not enabled.
nngraph.setDebug(true)

local function get_net(from, to)
	local from = from or 10
	local to = to or 10
	local input_x = nn.Identity()()
	local linear_module = nn.Linear(from, to)(input_x)

	-- Annotate nodes with local variable names
	nngraph.annotateNodes()
	return nn.gModule({input_x},{linear_module})
end

local net = get_net(10,10)

-- if you give a name to the net, it will use that name to produce the
-- svg in case of error, if not, it will come up with a name
-- that is derived from number of inputs and outputs to the graph
net.name = 'my_bad_linear_net'

-- prepare an input that is of the wrong size to force an error
local input = torch.rand(11)
pcall(function() net:updateOutput(input) end)
-- it should have produced an error and spit out a graph
-- just run Safari to display the svg
os.execute('open -a  Safari my_bad_linear_net.svg')

More Repositories

1

torch7

http://torch.ch
C
8,966
star
2

nn

Lua
1,334
star
3

tutorials

A series of machine learning tutorials for Torch7
Jupyter Notebook
622
star
4

distro

Torch installation in a self-contained folder
CMake
554
star
5

demos

Demos and tutorials around Torch7.
Lua
355
star
6

cutorch

A CUDA backend for Torch7
Cuda
338
star
7

threads

Threads for Lua and LuaJIT. Transparent exchange of data between threads is allowed thanks to torch serialization.
Lua
250
star
8

DEPRECEATED-torch7-distro

Torch7: state-of-the-art machine learning algorithms
C
224
star
9

cunn

Cuda
215
star
10

image

An Image toolbox for Torch.
C
209
star
11

qtlua

Lua interface to QT library
C++
204
star
12

optim

A numeric optimization package for Torch.
Lua
196
star
13

luajit-rocks

LuaJIT and luarocks in one location
C
155
star
14

trepl

A pure Lua-based, lightweight REPL for Torch.
Lua
81
star
15

tds

Torch C data structures
C
80
star
16

xlua

A set of useful functions to extend Lua (string, table, ...).
Lua
77
star
17

torch.github.io

Torch's web page.
HTML
75
star
18

ezinstall

One-line install scripts for Torch.
Shell
75
star
19

rocks

Rocks for torch
HTML
72
star
20

class

Oriented Object Programming for Lua
Lua
71
star
21

rnn

Torch recurrent neural networks
Lua
64
star
22

gnuplot

Lua
59
star
23

TH

Standalone C TH library
C
58
star
24

argcheck

A powerful (and blazing fast) argument checker and function overloading system for Lua or LuaJIT
Lua
53
star
25

paths

C
51
star
26

senna

NLP SENNA (http://ml.nec-labs.com/senna) interface to LuaJIT
Lua
49
star
27

sdl2-ffi

A LuaJIT interface to SDL2
Lua
37
star
28

graph

Graph package for Torch
Lua
35
star
29

cwrap

Lua
29
star
30

xt

torch TH/THC c++11 wrapper
C
14
star
31

sys

A system utility package for Torch.
Lua
13
star
32

ffi

FFI bindings for Torch7. Allows LuaJIT-speed access to Tensors and Storages.
Lua
9
star
33

sundown-ffi

A LuaJIT interface to the Sundown library (a Markdown implementation)
C
9
star
34

qttorch

C++
8
star
35

hash

Hashing functions for Torch7
C
8
star
36

cairo-ffi

LuaJIT FFI interface to Cairo Graphics
Lua
7
star
37

luarocks-mirror

because luarocks.org is not completely reliable!
Shell
6
star
38

dok

Lua
6
star
39

rational

rational numbers for lua
Lua
5
star
40

socketfile

adds file-over-sockets support for torch
Lua
5
star
41

env

Sets up default torch environment
Lua
4
star
42

vector

Lua
4
star
43

testme

Unit Testing for Torch.
Lua
2
star