forked from Atcold/torch-TripletEmbedding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.lua
68 lines (59 loc) · 1.9 KB
/
test.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
--------------------------------------------------------------------------------
-- Test function for TripletEmbeddingCriterion
--------------------------------------------------------------------------------
-- Alfredo Canziani, Apr/May 15
--------------------------------------------------------------------------------
cuda = false
require 'nn'
require 'TripletEmbedding'
if cuda then
require 'cutorch'
torch.setdefaulttensortype('torch.CudaTensor')
cutorch.manualSeedAll(0)
end
colour = require 'trepl.colorize'
local b = colour.blue
torch.manualSeed(0)
batch = 3
embeddingSize = 5
-- Ancore embedding batch
a = torch.rand(batch, embeddingSize)
print(b('ancore embedding batch:')); print(a)
-- Positive embedding batch
p = torch.rand(batch, embeddingSize)
print(b('positive embedding batch:')); print(p)
-- Negativep embedding batch
n = torch.rand(batch, embeddingSize)
print(b('negative embedding batch:')); print(n)
-- Testing the loss function forward and backward
loss = nn.TripletEmbeddingCriterion(.2)
if cuda then loss = loss:cuda() end
print(colour.red('loss: '), loss:forward({a, p, n}), '\n')
gradInput = loss:backward({a, p, n})
print(b('gradInput[1]:')); print(gradInput[1])
print(b('gradInput[2]:')); print(gradInput[2])
print(b('gradInput[3]:')); print(gradInput[3])
-- Jacobian test
d = 1e-6
jacobian = {}
zz = torch.Tensor{
{1, 0, 0},
{0, 1, 0},
{0, 0, 1},
}
for k = 1, 3 do
jacobian[k] = torch.zeros(a:size())
z = zz[k]
for i = 1, a:size(1) do
for j = 1, a:size(2) do
pert = torch.zeros(a:size())
pert[i][j] = d
outA = loss:forward({a - pert*z[1], p - pert*z[2], n - pert*z[3]})
outB = loss:forward({a + pert*z[1], p + pert*z[2], n + pert*z[3]})
jacobian[k][i][j] = (outB - outA)/(2*d)
end
end
end
print(b('jacobian[1]:')); print(jacobian[1])
print(b('jacobian[2]:')); print(jacobian[2])
print(b('jacobian[3]:')); print(jacobian[3])