In [1]:
require 'criteria/KLDCriterion'
require 'criteria/GaussianCriterion'
require 'utils/Sampler'
In [2]:
kld = nn.KLDCriterion
gauss = nn.GaussianCriterion
sampler = nn.Sampler()
In [3]:
mu = torch.randn(3,4)
lv = torch.randn(3,4):pow(2):log()
pmu = torch.zeros(3,4)
plv = torch.zeros(3,4)
In [4]:
kld:forward({pmu, plv}, {mu, lv})
Out[4]:
In [35]:
-- D(posterior || prior) = E[log posterior - log prior]
log_posterior = 0
log_prior = 0
N = 10
for i = 1,N do
code = sampler({mu, lv})
log_posterior = log_posterior + gauss:forward({mu, lv}, code)/N
log_prior = log_prior + gauss:forward({pmu, plv}, code)/N
end
print(log_posterior - log_prior)
Out[35]:
In [6]:
zero_mu = torch.zeros(1)
zero_logv = torch.zeros(1)
code = torch.zeros(1)
print(gauss:forward({zero_mu, zero_logv}, code))
Out[6]:
In [7]:
mu = torch.randn(3,4)
lv = torch.randn(3,4):pow(2):log()
pmu = torch.zeros(3,4)
plv = torch.zeros(3,4)
code = sampler({mu, lv})
mu = torch.randn(3,4)
lv = torch.randn(3,4):pow(2):log()
h = 1e-5
dmu, dlv = unpack(gauss:backward({mu, lv}, code))
In [8]:
for i = 1,mu:size(1) do
for j = 1,mu:size(2) do
mu[{i, j}] = mu[{i, j}] + h
fph = gauss:forward({mu, lv}, code)
mu[{i, j}] = mu[{i, j}] - h - h
fmp = gauss:forward({mu, lv}, code)
mu[{i, j}] = mu[{i, j}] + h
print((fph - fmp)/2/h - dmu[{i, j}])
end
end
Out[8]:
In [9]:
for i = 1,lv:size(1) do
for j = 1,lv:size(2) do
lv[{i, j}] = lv[{i, j}] + h
fph = gauss:forward({mu, lv}, code)
lv[{i, j}] = lv[{i, j}] - h - h
fmp = gauss:forward({mu, lv}, code)
lv[{i, j}] = lv[{i, j}] + h
print((fph - fmp)/2/h - dlv[{i, j}])
end
end
Out[9]: