In [2]:
require 'nngraph';

In [8]:
a = torch.Tensor{1,2,3}
print(a)


Out[8]:
 1
 2
 3
[torch.DoubleTensor of size 3]

For whatever reason, the identity module is the first step of every nngraph


In [6]:
module1 = nn.Identity()

In [9]:
module1:forward(a)


Out[9]:
 1
 2
 3
[torch.DoubleTensor of size 3]

Notice the extra parentheses. "The extra () contain properties of this module when embedded into a graph"


In [10]:
x1 = nn.Identity()()
m = nn.gModule({x1},{x1})

In [11]:
m:forward(a)


Out[11]:
 1
 2
 3
[torch.DoubleTensor of size 3]

Digression: the Oxford tutorial:


In [14]:
add = nn.CAddTable()
t1 = torch.Tensor{3,4,10}
x=add:forward({a,t1})

In [16]:
x


Out[16]:
  4
  6
 13
[torch.DoubleTensor of size 3]

Creating z = x1 + x1 * linear(x3)


In [10]:
-- Declare some tensors
t1 = torch.Tensor{1,2,3}
t2 = torch.Tensor{3,4,5}
x1 = nn.Identity()()
x2 = nn.Identity()()
a = nn.CAddTable()({x1,x2})
m = nn.gModule({x1,x2},{a})
print(m:forward({t1,t2}))


Out[10]:
 4
 6
 8
[torch.DoubleTensor of size 3]

Back to Lab


In [9]:
x1 = nn.Identity()()
x2 = nn.Identity()()
add = nn.CAddTable()({x1,x2})
mul = nn.CMulTable()({add,x1})
m = nn.gModule({x1,x2},{mul})
print(m:forward({t1,t2}))


...rlesguthrie/torch/install/share/lua/5.1/nngraph/init.lua:48: inputs[1] should be an nngraph.Node but is of type torch.DoubleTensor
stack traceback:
	[C]: in function 'error'
	...rlesguthrie/torch/install/share/lua/5.1/nngraph/init.lua:48: in function <...rlesguthrie/torch/install/share/lua/5.1/nngraph/init.lua:25>
	[C]: at 0x028340c0
	[string "x1 = nn.Identity()()..."]:3: in main chunk
	[C]: in function 'xpcall'
	...arlesguthrie/torch/install/share/lua/5.1/itorch/main.lua:179: in function <...arlesguthrie/torch/install/share/lua/5.1/itorch/main.lua:143>
	...arlesguthrie/torch/install/share/lua/5.1/lzmq/poller.lua:75: in function 'poll'
	...esguthrie/torch/install/share/lua/5.1/lzmq/impl/loop.lua:307: in function 'poll'
	...esguthrie/torch/install/share/lua/5.1/lzmq/impl/loop.lua:325: in function 'sleep_ex'
	...esguthrie/torch/install/share/lua/5.1/lzmq/impl/loop.lua:370: in function 'start'
	...arlesguthrie/torch/install/share/lua/5.1/itorch/main.lua:350: in main chunk
	[C]: in function 'require'
	(command line):1: in main chunk
	[C]: at 0x010234bbb0

In [36]:
a = torch.Tensor{1,2,3,1}
b = torch.Tensor{3,4,5,0}
print(m:forward({a,b}))


Out[36]:
  4
 12
 24
  1
[torch.DoubleTensor of size 4]

A4

nngraph


In [4]:
x = torch.ones(4)
y = torch.ones(5)
z = torch.ones(2)

In [7]:
--inputs
ix = nn.Identity()()
iy = nn.Identity()()
iz = nn.Identity()()
--Wx + b
h1 = nn.Linear(4,2)({ix})
h2 = nn.Linear(5,2)({iy})
-- tanh, sigmoid
tanh = nn.Tanh()({h1})
sigmoid = nn.Sigmoid()({h2})
-- square
tsq = nn.Square()({tanh})
ssq = nn.Square()({sigmoid})
-- cmul
cmul = nn.CMulTable()({tsq,ssq})
a = nn.CAddTable()({cmul,iz})
-- final graph
output = nn.gModule({ix,iy,iz},{a})

In [8]:
h1.data.module.weight = torch.ones(2,4)
h1.data.module.bias = torch.ones(2)
h2.data.module.weight = torch.ones(2,5)
h2.data.module.bias = torch.ones(2)

In [12]:
print(output:forward({x,y,z}))


Out[12]:
 1.9949
 1.9949
[torch.DoubleTensor of size 2]


In [ ]:
graph.dot(output.fg, 'output','outputBaseName')

nngraph b


In [111]:
gradOutput = torch.ones(2)

In [112]:
foo = {[0]=1,[1]=5}

In [113]:
foo


Out[113]:
{
Out[113]:
  0 : 1
  1 : 5
}

In [122]:
foo = torch.ones(4,4)

In [123]:
foo


Out[123]:
 1  1  1  1
 1  1  1  1
 1  1  1  1
 1  1  1  1
[torch.DoubleTensor of size 4x4]


In [125]:
print(nn.Reshape(2,2):forward(foo))


Out[125]:
(1,.,.) = 
  1  1
  1  1

(2,.,.) = 
  1  1
  1  1

(3,.,.) = 
  1  1
  1  1

(4,.,.) = 
  1  1
  1  1
[torch.DoubleTensor of size 4x2x2]


In [ ]:
vm = torch.load('vocab_map.tab')

In [ ]: