-
Notifications
You must be signed in to change notification settings - Fork 11
/
Prior.lua
45 lines (35 loc) · 849 Bytes
/
Prior.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
local model, parent = torch.class('nn.Prior', 'nn.Module')
function model:__init(nvars)
self.nvars = nvars
self.net = nn.Sequential()
self.net:add(nn.Linear(1, nvars))
self.net:add(nn.LogSoftMax())
self.proto = torch.ones(1)
end
function model:parameters()
return self.net:parameters()
end
function model:training()
self.net:training()
parent.training(self)
end
function model:evaluate()
self.net:evaluate()
parent.evaluate(self)
end
function model:reset()
self.net:reset()
end
function model:precompute()
self._cache = self.net:forward(self.proto)
end
function model:log_prob(input)
if self._cache then
return self._cache
else
return self.net:forward(self.proto)
end
end
function model:update(input, gradOutput)
self.net:backward(self.proto, gradOutput)
end