Guest User

Untitled

a guest
Aug 28th, 2014
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. import optiml.compiler._
  2. import optiml.library._
  3. import optiml.shared._
  4.  
  5. object kMeansCompilerExt extends OptiMLApplicationCompiler with kMeansExt{
  6. registerFunction(kMeansExtRun _);
  7. //override def functionName = "kMeansExtRun";
  8. }
  9. object kMeansInterpreterExt extends OptiMLApplicationInterpreter with kMeansExt
  10.  
  11. trait kMeansExt extends OptiMLApplication {
  12. def printUsage = {
  13. println("Usage: kmeans <input data file> [initmu data file]")
  14. exit(-1)
  15. }
  16.  
  17. private val tol = 0.001 // tolerance (for convergence)
  18. private val k = 16 // num clusters
  19.  
  20. private def findNearestCluster(x_i: Rep[DenseVectorView[Double]], mu: Rep[DenseMatrix[Double]]): Rep[Int] = {
  21. (mu mapRowsToVector { row => dist(x_i, row, SQUARE) }).minIndex
  22. }
  23.  
  24. def main() = {
  25. if (args.length < 1) printUsage
  26. val x = TrainingSet(readMatrix(args(0)), DenseVector[Double]()) // no labels
  27. val m = x.numSamples
  28. val n = x.numFeatures
  29. val mu = if (args.length > 1) readMatrix(args(1)) else ((0::k, *) { i => x(randomInt(m)) })
  30.  
  31. println("m:"+m+",n:"+n+",numClusters:"+k+",mu.numRows:"+mu.numRows)
  32.  
  33. tic(mu)
  34.  
  35. var iter = 0
  36.  
  37. val newMu = untilconverged_withdiff(mu, tol){ mu =>
  38. iter += 1
  39.  
  40. val c = (0::m) { e => findNearestCluster(x(e), mu) }
  41.  
  42. val allWP = (0::m).groupByReduce(i => c(i), i => x(i).Clone, (a: Rep[DenseVector[Double]], b: Rep[DenseVector[Double]]) => a + b)
  43. val allP = (0::m).groupByReduce(i => c(i), i => 1, (a: Rep[Int], b: Rep[Int]) => a+b)
  44.  
  45. (0::k, *) { j =>
  46. val weightedpoints = allWP(j)
  47. val points = allP(j)
  48. val d = if (points == 0) 1 else points
  49. weightedpoints / d
  50. }
  51. }((x, y) => dist(x, y, SQUARE)) // use SQUARE instead of the default EUC distance
  52.  
  53. toc(newMu)
  54. println("finished in " + iter + " iterations")
  55. newMu.pprint
  56. }
  57.  
  58.  
  59. // *if I don't specify an argument for kMeansExtRun, I get *
  60. //(in method registerFunction)(in method registerFunction)(in method registerFunction)(in method registerFunction)])Unit
  61. //[error] cannot be applied to (() => kMeansCompilerExt.Rep[Unit])
  62. //[error] registerFunction(kMeansExtRun _);
  63. def kMeansExtRun(dummyValue:Rep[Int]) = {
  64. val x = TrainingSet(readMatrix("hard-coded-file-path-here"), DenseVector[Double]()) // no labels
  65. val m = x.numSamples
  66. val n = x.numFeatures
  67. val mu = ((0::k, *) { i => x(randomInt(m)) })
  68.  
  69. println("m:"+m+",n:"+n+",numClusters:"+k+",mu.numRows:"+mu.numRows)
  70.  
  71. tic(mu)
  72.  
  73. var iter = 0
  74.  
  75. val newMu = untilconverged_withdiff(mu, tol){ mu =>
  76. iter += 1
  77.  
  78. val c = (0::m) { e => findNearestCluster(x(e), mu) }
  79.  
  80. val allWP = (0::m).groupByReduce(i => c(i), i => x(i).Clone, (a: Rep[DenseVector[Double]], b: Rep[DenseVector[Double]]) => a + b)
  81. val allP = (0::m).groupByReduce(i => c(i), i => 1, (a: Rep[Int], b: Rep[Int]) => a+b)
  82.  
  83. (0::k, *) { j =>
  84. val weightedpoints = allWP(j)
  85. val points = allP(j)
  86. val d = if (points == 0) 1 else points
  87. weightedpoints / d
  88. }
  89. }((x, y) => dist(x, y, SQUARE)) // use SQUARE instead of the default EUC distance
  90.  
  91. toc(newMu)
  92. println("finished in " + iter + " iterations")
  93. newMu.pprint
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment