Last active
March 2, 2017 09:17
-
-
Save ixaxaar/ef793e4c3d25f2b5edee5fac4bdd843b to your computer and use it in GitHub Desktop.
Beam over permutations, find the best possible sequence given a perplexity function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def beamPermute[T]( | |
data: List[T], | |
perplexityFn: List[T] => Double, | |
startNode: T, | |
beamSize: Int = 10, | |
beamWindow: Int = 3 | |
): List[(List[T], Double)] = { | |
var candidates = List((List(startNode), 0d)) | |
// var leftOver = data.to[ListBuffer] | |
data.foreach{ _ => | |
candidates = candidates.map{ | |
case (c:List[T], p:Double) => | |
var now = data.to[ListBuffer] | |
c.foreach{ x => now -= x } | |
now.map{ d => | |
val x = (c :+ d).takeRight(beamWindow) | |
((c :+ d), perplexityFn(x)) | |
} | |
}.toList.flatten | |
.sortBy{ x => -x._2 } | |
.take(beamSize) | |
} | |
candidates | |
} | |
def beamTest:Unit = { | |
val testData = List("999999999", "22", "55555", "333", "666666", "4444", "7777777", "88888888") | |
val res = beamPermute[String]( | |
testData, | |
(x:List[String]) => x.mkString(" ").length.toDouble, | |
"1", | |
beamSize=testData.length, | |
beamWindow=testData.length | |
) | |
res | |
res.map{ r => println(r._1.mkString(" ") +"\t\t->\t"+r._2) } | |
} | |
Author
ixaxaar
commented
Mar 2, 2017
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment