Skip to content

Instantly share code, notes, and snippets.

@reisepass
Created June 30, 2015 13:31
Show Gist options
  • Save reisepass/66e1cb60114bb62c0fac to your computer and use it in GitHub Desktop.
Save reisepass/66e1cb60114bb62c0fac to your computer and use it in GitHub Desktop.
val Q = DenseMatrix.ones[Double](numRegions, numClasses) //TODO maybe initialize to 1/num neighbours or 1/numneighbours*numClass
//TODO This Q could be stored in a spark RDD if it gets too large for memory;; Prob not necesary because thetaUnayr is the same size
//TODO consider storing the probabilities in a byte
if (debug) {
//val header = (0 until numClasses).toSeq.toList.flatMap { curclass => (0 until numRegions).toSeq.toList.map { curregion => ",Qr" + curregion + " Qc" + curclass } }
// println("#MF_Q_LOG#,timeID,elapsedTime" + header)
// println("#MF_E_LOG#,timeID,logTag,iter,maxE,minE,maxE_change,minE_change")
}
var lastMaxE = 0.0
var lastMinE = 0.0
var numNoChange = 0
for (iter <- 0 until maxIterations) {
var numUnchangedQs = 0
val lastQ = Q;
val xiLab = (0 until numClasses).par
//val xis = (0 until graph.size).par
val allXiperLabel = xiLab.map(curLab => ((curLab,
for (xi <- 0 until graph.size) yield {
val neigh = graph.getC(xi).toArray
val allClasses = (0 until numClasses).toList
val newQest = neigh.toList.map { neighIdx =>
allClasses.foldLeft(0.0) { (running, curClass) =>
{
running + Math.exp(lastQ(neighIdx, curClass) * (if (DISABLE_PAIRWISE) 0 else thetaPairwise(curClass, curLab))) * Math.exp((1 / temp) * thetaUnary(xi, curLab))
}
}
}.sum
(1 / temp) * newQest
})))
for (labAgain <- 0 until numClasses) {
val allXi = allXiperLabel(labAgain)._2.toArray
Q(::, labAgain) := DenseVector(allXi)
}
//allXiperLabel.foreach((label:Int,listofQs:IndexedSeq[Double])=>Q(::,label):=DenseVector(listofQs.toArray))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment