RDFTensor (version 1.3)

rescal: RESCAL: Tensor Factorization.

Description

RESCAL-ALS algorithm to compute the RESCAL tensor factorization. The solution is a matrix and a core tensor. RESCAL factors a (usually sparse) three-way tensor X such that each frontal slice X_k is factored into X_k = A * R_k * A^T

Usage

rescal(X, rnk,ainit = 'nvecs',verbose=2,Ainit=NULL,Rinit=NULL,lambdaA=0,lambdaR=0, 
      lambdaV=0,epsilon=1e-3,maxIter=100, minIter=1, P = list(),orthogonalize=FALSE,
      func_compute_fval='compute_fit')

Arguments

X

is a sparse tensor as set of sparse matrices, one for every relation (predicate). (a LIST of SparseMatrix )

rnk

The rank of the factorization

ainit

the method used to initialize matrix A

verbose

the level of messages to be displayed, 0 is minimal.

Ainit

the initial value of matrix A.

Rinit

the initial value of R (the core tensor, as LIST of frontal slices)

lambdaA

Regularization parameter for A factor matrix. 0 by default

lambdaR

Regularization parameter for R_k factor matrices. 0 by default

lambdaV

Regularization parameter for R_k factor matrices. 0 by default

epsilon

error threshold

maxIter

Maximum number of iterations

minIter

Minimum number of iterations

P

Not implemented

orthogonalize

Not implemented

func_compute_fval

function used to compute fit.

Value

list(A=A, R=R, all_err, nitr=itr + 1, times=as.vector(exectimes) Returns a LIST of the following:

A

The matrix A of the factorization ( n by r)

R

The core tensor R the factorization as r (rank) matrices of ( r by r)

nitr

number of iterations

times

list of running times of each step.

References

-Maximilian Nickel, Volker Tresp, Hans-Peter-Kriegel, "A Three-Way Model for Collective Learning on Multi-Relational Data", ICML 2011, Bellevue, WA, USA

-Maximilian Nickel, Volker Tresp, Hans-Peter-Kriegel, "Factorizing YAGO: Scalable Machine Learning for Linked Data" WWW 2012, Lyon, France

See Also

cp_apr serial_parCube cp_nmu cp_als

Examples

Run this code
# NOT RUN {
    X1=matrix(c(1,0,0,0,0, 0,1,0,0,0, 0,0,1,1,0, 0,0,0,0,1, 1,0,0,0,0),byrow=TRUE,nrow=5,ncol=5)
    X2=matrix(c(0,1,0,1,1, 1,0,0,1,0, 0,1,0,1,1, 0,0,0,0,1, 0,0,1,0,0),byrow=TRUE,nrow=5,ncol=5)
    X2_=matrix(c(0,1,0,1,1, 1,0,0,1,0, 0,0,0,0,0, 0,0,0,0,1, 0,0,1,0,0),byrow=TRUE,nrow=5,ncol=5)
    X=list(t(X1),t(X2),t(X2_))

    N=nrow(X1)
    Xs=list()
    for(s in 1:length(X)){
      aa=which(X[[s]]==1,arr.ind=TRUE)
      Xs[[s]]=Matrix::sparseMatrix(x=rep(1,nrow(aa)),i=aa[,1],j=aa[,2],dims=c(N,N))
    }

    print(Xs)

    rf=rescal(Xs,2)
    A=rf$A
    R=rf$R
    Tensor_error(Xs,A,R)
    tmp=rescal_01(Xs,A,R,scale_fact=1.5)#generate 1.5*original number of triples
    print(sprintf('Precision:%.4f, Recall:%.4f',tmp$tp/(tmp$tp+tmp$fp),tmp$tp/(tmp$tp+tmp$fn)))

#using RESCAL for prediction missing relations.

aa=read.table(file = paste0(path.package("RDFTensor"), '/toy_vicePresident.nt'),
                                sep=' ',header=FALSE,stringsAsFactors=FALSE)
trp=aa[,1:3]

tnsr=getTensor(trp)
    r=4
    
    sr=NULL
    t0=proc.time()
    X_=list()
    library(Matrix)
    tt=rescal(tnsr$X,rnk=r,ainit='nvecs',verbose=1,lambdaA=0,epsilon=1e-4,lambdaR=0)
    R=tt$R
    A=tt$A
s1=A%*%R[[1]]%*%Matrix::t(A)
s2=A%*%R[[2]]%*%Matrix::t(A)
#predict the party of AlGore (no explicit info is given in the nt file)
print(s1[tnsr$SO=='<http://example.com/AlGore>',tnsr$SO=='<http://example.com/RepublicanParty>'])
print(s1[tnsr$SO=='<http://example.com/AlGore>',tnsr$SO=='<http://example.com/DemocraticParty>'])
partyOf=data.frame(tnsr$SO,Repub=s1[,tnsr$SO=='<http://example.com/RepublicanParty>'],
           Democ=s1[,tnsr$SO=='<http://example.com/DemocraticParty>'],
    GivenRepub=tnsr$X[[1]][,tnsr$SO=='<http://example.com/RepublicanParty>'],
          GivenDemoc=tnsr$X[[1]][,tnsr$SO=='<http://example.com/DemocraticParty>'],
    stringsAsFactors=FALSE)  
    
 print(partyOf)

# }

Run the code above in your browser using DataCamp Workspace