Last active
August 29, 2015 14:16
-
-
Save nagadomi/1fb0abe4ad40abc6942a to your computer and use it in GitHub Desktop.
PageRank
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local function pagerank(mat, alpha, epsilon) | |
alpha = alpha or 0.85 | |
epsilon = epsilon or 1.0e-5 | |
local state = torch.Tensor(mat:size(1)):fill(1.0 / mat:size(1)) | |
local p = mat:clone() | |
local alpha_not = ((1.0 - alpha) / state:size(1)) | |
for i = 1, mat:size(1) do | |
p[i]:div(mat[i]:sum() + 1.0e-6) | |
end | |
p = p:t():clone() | |
while true do | |
local prev = state:clone() | |
local ip = torch.mv(p, prev) | |
state:copy(ip:mul(alpha):add(alpha_not)) | |
local err = prev:dist(state, 1) | |
if err < epsilon then | |
return state | |
end | |
end | |
end | |
local function pagerank_test() | |
local sys = require 'sys' | |
local mat = torch.Tensor( | |
{ | |
{0,0,1,1}, | |
{0,0,1,1}, | |
{1,1,0,0}, | |
{0,1,1,0} | |
}) | |
local alpha = 0.85 | |
local t = sys.clock() | |
local rank = pagerank(mat, alpha) | |
print(rank) | |
print(sys.clock() - t) | |
end | |
--pagerank_test() | |
return pagerank |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment