In [13]:
require 'nn'
require 'nngraph'
require 'graph'
In [ ]:
In [ ]:
In [ ]:
In [7]:
x=torch.randn(5)
y=torch.randn(5)
m=nn.CAddTable()
m:forward({x,y})
In [8]:
y
Out[8]:
In [15]:
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')
In [16]:
mse = nn.MSECriterion()
In [ ]:
-- Apprentissage par descente de gradient
function fit(mlp, criterion, data, labels, lr, nIter)
local lr = lr or 1e-4
local nIter = nIter or 1200
-- local choices = torch.LongTensor((#data)[1])
for i = 1,nIter do
mlp:zeroGradParameters()
--on shufflise les datas
-- choices:random((#data)[1])
-- local x = data:index(1,choices)
-- local y = labels:index(1,choices)
indiceRand=math.random((#data)[1])
local x = data[indiceRand]
local y = labels[indiceRand]
--calcul du y chapeau (prediction)
local pred = mlp:forward(x)
--calcul de l'erreur entre pred et y*
local loss = criterion:forward(pred,y)
--gradient de l'erreur par rapport à pred
local df_do = criterion:backward(pred,y)
--propagation du gradient de l'erreur sur la fonction
-- linear d'entrée
local df_di = mlp:backward(x, df_do)
-- on modifie les poids suivant le learning rate sur lensemble des couches
mlp:updateParameters(lr)
if i % 1000 == 0 then
print(i,loss)
end
end
end
In [ ]:
-- Apprentissage par descente de gradient
function fit(mlp, criterion, data, labels, lr, nIter)
local lr = lr or 1e-4
local nIter = nIter or 1000
-- local choices = torch.LongTensor((#data)[1])
for i = 1,nIter do
mlp:zeroGradParameters()
--on shufflise les datas
-- choices:random((#data)[1])
-- local x = data:index(1,choices)
-- local y = labels:index(1,choices)
indiceRand=math.random((#data)[1])
local x = data[indiceRand]
local y = labels[indiceRand]
--calcul du y chapeau (prediction)
local pred = mlp:forward(x)
--calcul de l'erreur entre pred et y*
local loss = criterion:forward(pred,y)
--gradient de l'erreur par rapport à pred
local df_do = criterion:backward(pred,y)
--propagation du gradient de l'erreur sur la fonction
-- linear d'entrée
local df_di = mlp:backward(x, df_do)
-- on modifie les poids suivant le learning rate sur lensemble des couches
mlp:updateParameters(lr)
if i % 1000 == 0 then
print(i,loss)
end
end
end
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]: