-
Notifications
You must be signed in to change notification settings - Fork 0
/
MKL.lua
51 lines (41 loc) · 1.02 KB
/
MKL.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
require 'cutorch'
function MKL(A,B)
local timer = torch.Timer()
local N = A:size(2)
local Da2, Ua = torch.eig(A, 'V')
--Da2 = torch.diag(Da2)
Da2 = Da2:select(2,1)
for i = 1, Da2:size(1) do
if Da2[i] < 0 then
Da2[i] = 0
end
end
local Da = torch.diag(torch.sqrt(Da2))
-- C = Da * Ua:t() * B * Ua * Da
local C = torch.mm(Da, Ua:t())
C = torch.mm(C,B)
C = torch.mm(C,Ua)
C = torch.mm(C,Da)
local Dc2, Uc = torch.eig(C, 'V')
Dc2 = Dc2:select(2,1)
for i = 1, Dc2:size(1) do
if Dc2[i] < 0 then
Dc2[i] = 0
end
end
local Dc = torch.diag(torch.sqrt(Dc2))
local temp = torch.Tensor(N)
for i = 1, N do
temp[i] = 1/torch.diag(Da):select(2,i)
end
local Da_inv = torch.diag(temp)
local T = torch.mm(Ua, Da_inv)
--T = Ua * Da_inv * Uc * Dc * Uc:t() * Da_inv * Ua:t()
T = torch.mm(T, Uc)
T = torch.mm(T, Dc)
T = torch.mm(T, Uc:t())
T = torch.mm(T, Da_inv)
T = torch.mm(T, Ua:t())
print('Time for MKL: ' ..timer:time().real.. 'seconds')
return T
end