-
Notifications
You must be signed in to change notification settings - Fork 3
/
layer.lua
31 lines (25 loc) · 897 Bytes
/
layer.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
-- Module for linear transformations using a weight tensor
function learn.layer.linear(p)
p.n_input = p.n_input or 1
p.n_output = p.n_output or 1
p.weight_init = p.weight_init or function()
return learn.gaussian(0.0, 1.0)
end
p.weights = p.weights or learn.tensor({size = {p.n_output, p.n_input}}).map(p.weight_init)
p.gradients = p.gradients or learn.tensor({size = {p.n_output, p.n_input}})
function p.forward(input)
-- print(table.concat(p.weights.data, ", "))
p.output = p.weights.dot(input)
-- print(table.concat(p.output.data, ", "))
return p.output
end
function p.backward(input, gradients)
p.delta = gradients.copy().mul(input)
return input, p.weights.transpose().dot(p.delta)
end
function p.update(input, learning_rate)
p.weights.sub(p.delta.dot(input.transpose()).scale(learning_rate))
return p.output
end
return p
end