1 package baseCode.algorithm.learning.unsupervised;
2
3 import baseCode.algorithm.Algorithm;
4 import baseCode.dataStructure.matrix.AbstractNamedDoubleMatrix;
5 import baseCode.dataStructure.matrix.DenseDoubleMatrix1D;
6 import cern.colt.matrix.DoubleMatrix1D;
7
8 import com.braju.beta.format.Format;
9 import com.braju.beta.format.Parameters;
10
11 /***
12 * Implementation of RankProp, as described in Weston et al. PNAS
13 * <hr>
14 * <p>
15 * Copyright (c) 2004 Columbia University
16 *
17 * @author Paul Pavlidis (port from Jason's code)
18 * @version $Id: RankProp.java,v 1.10 2005/01/05 02:01:02 pavlidis Exp $
19 */
20 public class RankProp extends Algorithm {
21
22 double alpha = 0.95;
23 int maxIter = 20;
24
25 /***
26 * @param matrix
27 * @param query
28 * @param k
29 * @return
30 */
31 public DoubleMatrix1D computeRanking( AbstractNamedDoubleMatrix matrix,
32 AbstractNamedDoubleMatrix query, int k ) {
33
34 DoubleMatrix1D yorig = new DenseDoubleMatrix1D( query.viewRow( 0 )
35 .toArray() );
36
37 return this.computeRanking(matrix, yorig, k);
38
39 }
40
41
42 /***
43 * @param matrix
44 * @param matrix1D
45 * @param k
46 * @return
47 */
48 public DoubleMatrix1D computeRanking( AbstractNamedDoubleMatrix matrix, DoubleMatrix1D query, int indexOfQuery ) {
49 int dim = query.size();
50 DoubleMatrix1D y = new DenseDoubleMatrix1D( dim );
51 DoubleMatrix1D yold = new DenseDoubleMatrix1D( dim );
52
53 if (query.size() <= 1) {
54 return null;
55 }
56
57 y.assign( 0.0 );
58 y.setQuick( indexOfQuery, 1.0 );
59
60 if ( alpha == 0.0 ) {
61 return query;
62 }
63
64 for ( int loops = 0; loops < maxIter; loops++ ) {
65
66 yold.assign( y );
67
68 int lim = Math.min( query.size(), matrix.rows() );
69
70 for ( int j = 0; j < lim; j++ ) {
71 if ( j == indexOfQuery ) continue;
72
73 double dotProduct = matrix.viewRow( j ).zDotProduct( yold );
74
75
76
77 y.set( j, ( alpha * dotProduct ) + query.getQuick( j ) );
78 }
79
80 if ( loops % 5 == 0 ) {
81 log.info( " iteration " + loops + " y[0]="
82 + Format.sprintf( "%g", new Parameters( y.getQuick( 0 ) ) ) );
83 }
84 }
85 return y;
86 }
87
88 /***
89 * @return Returns the alpha.
90 */
91 public double getAlpha() {
92 return alpha;
93 }
94
95 /***
96 * controls amount of "clustering"
97 * @param alpha The alpha to set.
98 */
99 public void setAlpha( double alpha ) {
100 this.alpha = alpha;
101 }
102
103 /***
104 * Maximum iterations before stopping.
105 * @return Returns the max_loops.
106 */
107 public int getMaxIter() {
108 return maxIter;
109 }
110
111 /***
112 * Maximum iterations before stopping.
113 * @param max_loops The max_loops to set.
114 */
115 public void setMaxIter( int maxIter ) {
116 this.maxIter = maxIter;
117 }
118
119 }
120