SLIM  1.0
Sparse Linear Methods (SLIM) for top-n recommender systems
 All Data Structures Files Functions Variables Typedefs Macros Pages
slim_fs_learn.c
Go to the documentation of this file.
1 /**************************************************************/
2 /*! \file
3 
4  \brief This file contains all the routines for SLIM learning
5  with feature selection
6 
7 */
8 /**************************************************************/
9 
10 
11 #include<slim.h>
12 
13 
14 /**************************************************************/
15 /*! \brief SLIM learning with feature selction
16 
17  \param[in] ctrl A ctrl structure which contains all the
18  parameters for SLIM Learning with feature
19  selection
20  \param[in] A The A matrix
21  \param[in] b The RHS vector
22  \param[in,out] w The solution vector
23  \param[in] A_colval A temporary place for a column
24  \param[in] Wrk A workspace for BCLS
25  \param[in] bl The lower bound for BCLS
26  \param[in] bu The upper bound for BCLS
27  \param[in] beta The regularization parameter for L-2 norm
28  \param[in] c The vector for L-1 norm
29  */
30 /**************************************************************/
31 void slim_fs_learn(ctrl_t * ctrl, gk_csr_t * A, double * b, double * w, float ** A_colval,
32  worksp * Wrk,
33  double * bl, double * bu,
34  double beta, double * c){
35 
36  int nnz = *(A->colptr + A->ncols);
37  int * acol = Wrk->acol;
38 
39  /* count nnz */
40  int kk = count_nnz(w, A->ncols);
41 
42  /* find topk nnz */
43  int topk = 0;
44  /* find the indices of topk entries, meanwhile w is over-written as the topk locations */
45  find_topk(w, A->ncols, gk_min(ctrl->k, kk), w, &topk);
46 
47  /* back up original values, this is done only once */
48  if (*A_colval == NULL){
49  *A_colval = gk_malloc(sizeof(float)*nnz, "malloc *A_colval");
50  memcpy((void *)*A_colval, (void *)A->colval, sizeof(float)*nnz);
51  }
52 
53  /* remove all A nnz values, this will not affect the column under consideration */
54  gk_fset(nnz, 0, A->colval);
55  /* set all columns as inactive */
56  gk_iset(A->ncols, 0, acol);
57  /* recover all topk columns in A */
58  for (int i = 0; i < topk; i ++){
59  int j = (int)w[i];
60  /* activate this column */
61  acol[j] = 1;
62  int nj = A->colptr[j+1] - A->colptr[j];
63  for (int k = 0; k < nj; k ++){
64  /* get the orignal values back */
65  *(A->colptr[j] + k + A->colval) = *(A->colptr[j] + k + *A_colval);
66  }
67  }
68 
69  /* BCLS */
70  gk_dset(A->ncols, 0, w);
71  bcsol(ctrl, A, b, w, Wrk, bl, bu, beta, c);
72 
73  /* recover full A, specific to binary A, this will over-write the column of b,
74  but will not matter */
75  memcpy((void *)A->colval, (void *)*A_colval, sizeof(float)*nnz);
76  /* activate all columns */
77  gk_iset(A->ncols, 1, acol);
78 
79 }
80 
81 
82 
83