|
|||||||||||||||||||
| 30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| RankProp.java | 0% | 0% | 0% | 0% |
|
||||||||||||||
| 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; // alpha parameter, controls amount of "clustering" |
|
| 23 |
int maxIter = 20;// number of iterations of algorithm |
|
| 24 |
|
|
| 25 |
/**
|
|
| 26 |
* @param matrix
|
|
| 27 |
* @param query
|
|
| 28 |
* @param k
|
|
| 29 |
* @return
|
|
| 30 |
*/
|
|
| 31 | 0 |
public DoubleMatrix1D computeRanking( AbstractNamedDoubleMatrix matrix,
|
| 32 |
AbstractNamedDoubleMatrix query, int k ) {
|
|
| 33 |
|
|
| 34 | 0 |
DoubleMatrix1D yorig = new DenseDoubleMatrix1D( query.viewRow( 0 )
|
| 35 |
.toArray() ); |
|
| 36 |
|
|
| 37 | 0 |
return this.computeRanking(matrix, yorig, k); |
| 38 |
|
|
| 39 |
} |
|
| 40 |
|
|
| 41 |
|
|
| 42 |
/**
|
|
| 43 |
* @param matrix
|
|
| 44 |
* @param matrix1D
|
|
| 45 |
* @param k
|
|
| 46 |
* @return
|
|
| 47 |
*/
|
|
| 48 | 0 |
public DoubleMatrix1D computeRanking( AbstractNamedDoubleMatrix matrix, DoubleMatrix1D query, int indexOfQuery ) { |
| 49 | 0 |
int dim = query.size();
|
| 50 | 0 |
DoubleMatrix1D y = new DenseDoubleMatrix1D( dim ); // we use own implementation for performance.s |
| 51 | 0 |
DoubleMatrix1D yold = new DenseDoubleMatrix1D( dim );
|
| 52 |
|
|
| 53 | 0 |
if (query.size() <= 1) {
|
| 54 | 0 |
return null; |
| 55 |
} |
|
| 56 |
|
|
| 57 | 0 |
y.assign( 0.0 ); // set all to zero.
|
| 58 | 0 |
y.setQuick( indexOfQuery, 1.0 ); |
| 59 |
|
|
| 60 | 0 |
if ( alpha == 0.0 ) {
|
| 61 | 0 |
return query;
|
| 62 |
} |
|
| 63 |
|
|
| 64 | 0 |
for ( int loops = 0; loops < maxIter; loops++ ) { // iterations of propagation |
| 65 |
|
|
| 66 | 0 |
yold.assign( y ); // initially all zero except for 1 at the query point.
|
| 67 |
|
|
| 68 | 0 |
int lim = Math.min( query.size(), matrix.rows() );
|
| 69 |
|
|
| 70 | 0 |
for ( int j = 0; j < lim; j++ ) { |
| 71 | 0 |
if ( j == indexOfQuery ) continue; // don't update query |
| 72 |
|
|
| 73 | 0 |
double dotProduct = matrix.viewRow( j ).zDotProduct( yold );
|
| 74 |
|
|
| 75 |
// new y is old y +
|
|
| 76 |
// new weighted linear combination of neighbors
|
|
| 77 | 0 |
y.set( j, ( alpha * dotProduct ) + query.getQuick( j ) ); |
| 78 |
} |
|
| 79 |
|
|
| 80 | 0 |
if ( loops % 5 == 0 ) {
|
| 81 | 0 |
log.info( " iteration " + loops + " y[0]=" |
| 82 |
+ Format.sprintf( "%g", new Parameters( y.getQuick( 0 ) ) ) ); |
|
| 83 |
} |
|
| 84 |
} |
|
| 85 | 0 |
return y;
|
| 86 |
} |
|
| 87 |
|
|
| 88 |
/**
|
|
| 89 |
* @return Returns the alpha.
|
|
| 90 |
*/
|
|
| 91 | 0 |
public double getAlpha() { |
| 92 | 0 |
return alpha;
|
| 93 |
} |
|
| 94 |
|
|
| 95 |
/**
|
|
| 96 |
* controls amount of "clustering"
|
|
| 97 |
* @param alpha The alpha to set.
|
|
| 98 |
*/
|
|
| 99 | 0 |
public void setAlpha( double alpha ) { |
| 100 | 0 |
this.alpha = alpha;
|
| 101 |
} |
|
| 102 |
|
|
| 103 |
/**
|
|
| 104 |
* Maximum iterations before stopping.
|
|
| 105 |
* @return Returns the max_loops.
|
|
| 106 |
*/
|
|
| 107 | 0 |
public int getMaxIter() { |
| 108 | 0 |
return maxIter;
|
| 109 |
} |
|
| 110 |
|
|
| 111 |
/**
|
|
| 112 |
* Maximum iterations before stopping.
|
|
| 113 |
* @param max_loops The max_loops to set.
|
|
| 114 |
*/
|
|
| 115 | 0 |
public void setMaxIter( int maxIter ) { |
| 116 | 0 |
this.maxIter = maxIter;
|
| 117 |
} |
|
| 118 |
|
|
| 119 |
} |
|
| 120 |
|
|
| 121 |
|
|
||||||||||