In [ ]:
require 'nn'
In [ ]:
model = nn.Sequential()
cat = nn.ConcatTable()
inner1 = nn.Sequential()
inner1:add(nn.Linear(1,2))
inner2 = nn.Sequential()
inner2:add(nn.Linear(1,5))
inner2:add(nn.ReLU(true))
inner2:add(nn.Linear(5,1))
inner2:add(nn.SoftMax())
cat:add(inner1)
cat:add(inner2)
model:add(cat)
In [ ]:
model = nn.Sequential()
cat = nn.ConcatTable()
cat:add(nn.Linear(1,3))
cat:add(nn.Linear(1,4))
model:add(cat)
In [ ]:
k = model:forward(torch.rand(10,1))
In [ ]:
k[2]
In [ ]:
x = torch.rand(10,1)
v = torch.rand(10,3)
In [ ]:
x:cmul(v[{{},1}])
In [ ]:
torch.zeros(3,1)
In [ ]:
require 'criteria/GMMKLDCriterion'
gkld = nn.GMMKLDCriterion
pmu = torch.zeros(3)
plogv = torch.zeros(3)
mu = torch.randn(3)
logv = torch.randn(3):pow(2):log()
pi = torch.rand(3)
input = {pmu, plogv, pi}
target = {mu, logv}
gkld:forward({pmu, plogv, pi}, {mu, logv})
In [ ]:
require 'criteria/GMMKLDCriterion'
require 'criteria/KLDCriterion'
gkld = nn.GMMKLDCriterion()
kld = nn.KLDCriterion()
pmu = torch.zeros(3,2)
plogv = torch.zeros(3,2)
pi = torch.ones(3,1)
mu = torch.randn(3,2)
logv = torch.randn(3,2):pow(2):log()
input = {pmu, plogv, pi}
target = {mu, logv}
print(gkld:forward({pmu, plogv, pi}, {mu, logv}))
print(kld:forward({pmu, plogv}, {mu, logv}))
In [26]:
require 'criteria/GMMKLDCriterion'
require 'criteria/KLDCriterion'
gkld = nn.GMMKLDCriterion()
kld = nn.KLDCriterion()
N = 1
D = 3
K = 2
pmu1 = torch.zeros(N,D)
pmu2 = torch.ones(N,D)*40
plogv1 = torch.zeros(N,D)
plogv2 = torch.ones(N,D)
pi = torch.ones(N,K):div(K)
mu = torch.ones(N,D)*.1
logv = torch.ones(N,D)
input = {pmu, plogv, pi}
target = {mu, logv}
print(gkld:forward({pmu1, pmu2, plogv1, plogv2, pi}, {mu, logv}))
print(kld:forward({pmu1, plogv1},{mu, logv}))
Out[26]:
In [ ]:
-- compare with direct KLD:
print(kld:forward({pmu1, plogv1}, {mu, logv}))
print(kld:forward({pmu2, plogv2}, {mu, logv}))
print(kld:forward({pmu3, plogv3}, {mu, logv}))
In [12]:
require 'criteria/GMMKLDCriterion'
require 'criteria/KLDCriterion'
gkld = nn.GMMKLDCriterion()
kld = nn.KLDCriterion()
N = 5
D = 3
K = 1
pmu1 = torch.randn(N,D)
pi = torch.Tensor({{1}}):expand(N,1)
plogv1 = torch.randn(N,D)
mu = torch.randn(N,D)
logv = torch.randn(N,D)
h = 1e-4
In [13]:
gkld:forward({pmu1, plogv1, pi}, {mu, logv})
dpmu1, dplogv1, dpi, dmu, dlogv = unpack(gkld:backward({pmu1, plogv1, pi}, {mu, logv}))
In [21]:
b
Out[21]:
In [14]:
a, b, c, d = unpack(kld:backward({pmu1, plogv1}, {mu, logv}))
In [1]:
require 'criteria/GMMKLDCriterion'
require 'criteria/KLDCriterion'
gkld = nn.GMMKLDCriterion()
kld = nn.KLDCriterion()
N = 5
D = 3
K = 3
pmu1 = torch.zeros(N,D)
pmu2 = torch.randn(N,D)
pmu3 = torch.randn(N,D)
pi = torch.Tensor({{1,2,3}}):expand(5,3)/6
plogv1 = torch.zeros(N,D)
plogv2 = torch.randn(N,D):pow(2):log()
plogv3 = torch.randn(N,D):pow(2):log()
mu = torch.zeros(N,D)
logv = torch.zeros(N,D)
h = 1e-4
In [ ]:
gkld:forward({pmu1, pmu2, pmu3, plogv1, plogv2, plogv3, pi}, {mu, logv})
dpmu1, dpmu2, dpmu3, dplogv1, dplogv2, dplogv3, dpi, dmu, dlogv = unpack(gkld:backward({pmu1, pmu2, pmu3, plogv1, plogv2, plogv3, pi}, {mu, logv}))
In [ ]:
dmu
In [ ]:
x = mu
y = dmu
print("Error:")
for i=1,x:size(1) do
for j=1,x:size(2) do
x[{i,j}] = x[{i,j}] + h
fph = gkld:forward({pmu1, pmu2, pmu3, plogv1, plogv2, plogv3, pi}, {mu, logv})
x[{i,j}] = x[{i,j}] - h - h
fmh = gkld:forward({pmu1, pmu2, pmu3, plogv1, plogv2, plogv3, pi}, {mu, logv})
x[{i,j}] = x[{i,j}] + h
print((fph - fmh)/2/h - y[{i,j}])
end
end
In [ ]:
dpi = unpack(gkld:backward({pmu1, pmu2, pmu3, plogv1, plogv2, plogv3, pi}, {mu, logv}))
print(dpi)
In [22]:
require 'nn'
Out[22]:
Out[22]:
In [23]:
model = nn.Sequential()
model:add(nn.Linear(2,3))
model:add(nn.ReLU(true))
model:add(nn.Linear(3,4))
model:add(nn.SoftMax())
model:add(nn.Linear(4,1))
In [28]:
x = torch.randn(10,2)
out = model:forward(x)
In [30]:
dout = torch.randn(10, 1)
In [31]:
model:backward(x, dout)
Out[31]:
In [31]:
pi
Out[31]:
In [49]:
a = torch.multinomial(pi, 1)
print(a)
Out[49]:
In [3]:
a
Out[3]: