renozao / NMF

NMF: A Flexible R package for Nonnegative Matrix Factorization
136 stars 40 forks source link

Two level matrix factorization #60

Open sureshgorakala opened 8 years ago

sureshgorakala commented 8 years ago

Hi Renozao,

I'm working on recommender systems using matrix factorization methods, recently I come across below paper: https://opus.lib.uts.edu.au/bitstream/10453/37442/4/Two%20level%20Matrix%20Factorization.pdf

My objective is to integrate item attributes to the rating matrix which is explained in the above paper. I need your guidance to modify the objective function in NMF package to address the below objective function:

tet

renozao commented 8 years ago

You can plug the complete algorithm (loop, stopping criteria) or use the built-in iterative framework.

# define the complete algorithm
my_objective_function <- function(x, R, alpha, beta, ...){
  # distance/objective function
  P <- basis(x); Q <- t(coef(x))
  sum((R - P %*% t(Q))^2) + alpha * sum(abs(P)) + beta * sum(Q^2)
}

algo <- function(R, x, alpha = 1, beta = 10){

  # iterative loop
  n <- 0
  while(TRUE){
    # update basis (P) and coefficient (t(Q)) matrix
    coef(x) <- coef(x) %*% (alpha * diag(runif(ncol(x))))
    basis(x) <- basis(x) %*% (beta * diag(runif(nbasis(x))))
    if( n>100 || my_objective_function(x, R, alpha, beta) < .5 ) break
    n <- n + 1
  }

  x
}

x <- rmatrix(20, 10)
nmf(x, 2, algo, alpha = 1, beta = 10, .opt = 'v', objective = my_objective_function)

# Or use the built-in iterative framework
algo_iter <- NMFStrategy('test', objective = my_objective_function, 
  Update = function(i, R, x, alpha, beta, ...){
    # update basis (P) and coefficient (t(Q)) matrix
    coef(x) <- coef(x) %*% (alpha * diag(runif(ncol(x))))
    basis(x) <- basis(x) %*% (beta * diag(runif(nbasis(x))))
    x
  }, Stop = function(algo, i, R, x, ...){
    # use step 0 to initialize local static variable if necessary
    if( i == 0L ) return(FALSE)
    # here random stopping criteria
    runif(1, max = 100) > 99
  }
)

nmf(x, 2, algo_iter, alpha = 1, beta = 10, .opt = 'v')
sureshgorakala commented 8 years ago

Thank you Renozao for the response., will try it and get back to you.

sureshgorakala commented 8 years ago

HI Renozao, just for confirmation, did the above equation doesn't contain the regularised part containing similarity calculation in the above equation?

renozao commented 8 years ago

The code I sent is a toy example, showing how you can can define your own algorithm, with a specific objective function. I only tried to use the notations from the paper you sent. You need to implement the right equations there.

sefat13756 commented 6 years ago

sureshgorakala have you finished work on 2 level matrix factorization? I have been working on it. But I have find it difficult in some places? will you please help me in this regard?