Modified:
/branches/thefuture-modules/thebeast-apps/src/main/scala/org/riedelcastro/thebeast/apps/DependencyParsing.scala
/branches/thefuture-modules/thebeast-core/src/main/scala/org/riedelcastro/thebeast/env/Env.scala
/branches/thefuture-modules/thebeast-core/src/main/scala/org/riedelcastro/thebeast/env/combinatorics/SpanningTreeConstraint.scala
/branches/thefuture-modules/thebeast-core/src/main/scala/org/riedelcastro/thebeast/solve/SumProductBeliefPropagation.scala
/branches/thefuture-modules/thebeast-core/src/test/scala/org/riedelcastro/thebeast/solve/SumProductBeliefPropagationSpecification.scala
=======================================
---
/branches/thefuture-modules/thebeast-apps/src/main/scala/org/riedelcastro/thebeast/apps/DependencyParsing.scala
Fri Mar 26 20:22:47 2010
+++
/branches/thefuture-modules/thebeast-apps/src/main/scala/org/riedelcastro/thebeast/apps/DependencyParsing.scala
Fri Mar 26 20:58:54 2010
@@ -14,7 +14,7 @@
*/
object DependencyParsing extends TheBeastEnv {
- val maxLength = 5
+ val maxLength = 50
val Tokens = Ints(0 until maxLength)
val Words = new MutableValues[String]()
@@ -46,6 +46,7 @@
env.atoms(word) ++= asTokenProperties(ROOT,rows.map(row => row(1)))
env.atoms(pos) ++= asTokenProperties(ROOT,rows.map(row => row(3)))
env.atoms(link) ++= rows.map(row => row(6).toInt->row(0).toInt)
+ env.close(Set(token,word,pos,link),true)
result += env
if (result.size == to) return result.drop(from)
} else {
@@ -148,7 +149,10 @@
import DependencyParsing._
def main(args:Array[String]) = {
- val trainData = loadCoNLLFile(args(0),0,20)
+
+ Logging.level = Logging.DEBUG
+
+ val trainData = loadCoNLLFile(args(0),0,10)
println(trainData(0)(word))
println(trainData(0)(pos))
println(trainData(0)(link))
@@ -171,8 +175,17 @@
val weights = new Vector
for (pair <- posPairProbs) weights("Pos",pair._1._1,pair._1._2) =
pair._2
+ val global = new MutableEnv
+ global(theta) = weights
+
println(weights)
+ val bp = new SumProductBeliefPropagation
+ val marginals =
bp.infer(probModel.ground(trainData(0).overlay(global).mask(Set(link))))
+
+ println(marginals)
+
+
}
}
=======================================
---
/branches/thefuture-modules/thebeast-core/src/main/scala/org/riedelcastro/thebeast/env/Env.scala
Wed Mar 24 22:06:14 2010
+++
/branches/thefuture-modules/thebeast-core/src/main/scala/org/riedelcastro/thebeast/env/Env.scala
Fri Mar 26 20:58:54 2010
@@ -138,9 +138,11 @@
result
}
- def close(variable: EnvVar[_], closed: Boolean) {
+ def close(variable: EnvVar[_], closed: Boolean) : Unit = {
if (closed) this.closed += variable else
this.closed.removeEntry(variable)
}
+
+ def close(variables:Iterable[EnvVar[_]],closed:Boolean): Unit =
variables.foreach(close(_,closed))
private def cloneMutableMap(map: MapType): MapType = {
=======================================
---
/branches/thefuture-modules/thebeast-core/src/main/scala/org/riedelcastro/thebeast/env/combinatorics/SpanningTreeConstraint.scala
Sun Mar 21 14:23:37 2010
+++
/branches/thefuture-modules/thebeast-core/src/main/scala/org/riedelcastro/thebeast/env/combinatorics/SpanningTreeConstraint.scala
Fri Mar 26 20:58:54 2010
@@ -28,21 +28,27 @@
def asLogic: DoubleTerm = {
import GenericImplicits._
- val domain =
vertices.values.asInstanceOf[FunctionValues[V,Boolean]].domain
- val uniqueHead = forall(domain,domain,domain) {
- (h,i,o)=> vertices(i) && vertices(h) && vertices(o) && edges(h,i)
~> !edges(o,i)}
+ val domain = vertices.values.asInstanceOf[FunctionValues[V,
Boolean]].domain
+ val uniqueHead = forall(domain, domain, domain) {
+ (h, i, o) => vertices(i) && vertices(h) && vertices(o) && edges(h,
i) ~> !edges(o, i)
+ }
val dominates = Predicate("dominates", TupleValues2(domain, domain))
- val linkAndDominates = forall(domain,domain) {
- (h,i) => vertices(i) && vertices(h) && edges(h,i) ~> dominates(h,i)}
- val transitive = forall(domain,domain,domain){
- (h,m,g)=> vertices(h) && vertices(m) && vertices(g) &&
dominates(h,m) && edges(m,g) ~> dominates(h,g)}
+ val linkAndDominates = forall(domain, domain) {
+ (h, i) => vertices(i) && vertices(h) && edges(h, i) ~> dominates(h,
i)
+ }
+ val transitive = forall(domain, domain, domain) {
+ (h, m, g) => vertices(h) && vertices(m) && vertices(g) &&
dominates(h, m) && edges(m, g) ~> dominates(h, g)
+ }
val acyclic = forall(domain) {
- i => vertices(i) ~> !dominates(i,i)}
- val projective1 = forall(domain,domain,domain) {
- (h,m,i)=> vertices(h) && vertices(m) && vertices(i) && edges(h,m) &&
order(h,i) && order(i,m) ~> dominates(h,i)}
- val projective2 = forall(domain,domain,domain) {
- (h,m,i)=> vertices(h) && vertices(m) && vertices(i) && edges(h,m) &&
order(m,i) && order(i,h) ~> dominates(h,i)}
- ${uniqueHead && linkAndDominates && transitive && acyclic &&
projective1 && projective2}
+ i => vertices(i) ~> !dominates(i, i)
+ }
+ val projective1 = forall(domain, domain, domain) {
+ (h, m, i) => vertices(h) && vertices(m) && vertices(i) && edges(h,
m) && order(h, i) && order(i, m) ~> dominates(h, i)
+ }
+ val projective2 = forall(domain, domain, domain) {
+ (h, m, i) => vertices(h) && vertices(m) && vertices(i) && edges(h,
m) && order(m, i) && order(i, h) ~> dominates(h, i)
+ }
+ $ {uniqueHead && linkAndDominates && transitive && acyclic &&
projective1 && projective2}
this
}
@@ -175,6 +181,17 @@
beliefs.increaseBelief(atom, true, trueBelief)
beliefs.increaseBelief(atom, false, b - trueBelief)
}
+ //set self loops and links to root to be impossible
+ for (i <- 0 until sorted.size) {
+ val self = FunAppVar(pred, (sorted(i), sorted(i)))
+ beliefs.increaseBelief(self, true, 0.0)
+ beliefs.increaseBelief(self, false, 1.0)
+ if (i > 0) {
+ val toRoot = FunAppVar(pred, (sorted(i), sorted(0)))
+ beliefs.increaseBelief(toRoot, true, 0.0)
+ beliefs.increaseBelief(toRoot, false, 1.0)
+ }
+ }
beliefs
} else
super.marginalize(incoming)
@@ -206,7 +223,7 @@
- case class Signature(from:Int,to:Int,rightWard:Boolean,link:Boolean)
+ case class Signature(from: Int, to: Int, rightWard: Boolean, link:
Boolean)
class InsideOutsideResult {
@@ -215,21 +232,22 @@
val total = new HashMap[(Int, Int), Double]
var Z = 0.0
- def in(from:Int,to:Int,rightWard:Boolean,link:Boolean):Double = {
- inside.getOrElse(Signature(from,to,rightWard,link),0.0)
+ def in(from: Int, to: Int, rightWard: Boolean, link: Boolean):
Double = {
+ inside.getOrElse(Signature(from, to, rightWard, link), 0.0)
}
- def incrIn(from:Int,to:Int,rightWard:Boolean,link:Boolean,
value:Double) = {
- val sig = Signature(from,to,rightWard,link)
- inside(sig) = inside.getOrElse(sig,0.0) + value
- }
- def out(from:Int,to:Int,rightWard:Boolean,link:Boolean):Double = {
- outside.getOrElse(Signature(from,to,rightWard,link),0.0)
+ def incrIn(from: Int, to: Int, rightWard: Boolean, link: Boolean,
value: Double) = {
+ val sig = Signature(from, to, rightWard, link)
+ inside(sig) = inside.getOrElse(sig, 0.0) + value
+ }
+
+ def out(from: Int, to: Int, rightWard: Boolean, link: Boolean):
Double = {
+ outside.getOrElse(Signature(from, to, rightWard, link), 0.0)
}
- def incrOut(from:Int,to:Int,rightWard:Boolean,link:Boolean,
value:Double) = {
- val sig = Signature(from,to,rightWard,link)
- outside(sig) = outside.getOrElse(sig,0.0) + value
+ def incrOut(from: Int, to: Int, rightWard: Boolean, link: Boolean,
value: Double) = {
+ val sig = Signature(from, to, rightWard, link)
+ outside(sig) = outside.getOrElse(sig, 0.0) + value
}
}
@@ -248,62 +266,62 @@
val n = sorted.size
//init unit values
- for (i <- 0 until n-1){
- incrIn(i,i+1,true,true,weights(i,i+1))
- incrIn(i,i+1,true,false,weights(i,i+1))
- incrIn(i,i+1,false,true,weights(i+1,i))
- incrIn(i,i+1,false,false,weights(i+1,i))
- }
- for (i <- 0 until n){
- incrIn(i,i,true,false,1.0)
- incrIn(i,i,false,false,1.0)
+ for (i <- 0 until n - 1) {
+ incrIn(i, i + 1, true, true, weights(i, i + 1))
+ incrIn(i, i + 1, true, false, weights(i, i + 1))
+ incrIn(i, i + 1, false, true, weights(i + 1, i))
+ incrIn(i, i + 1, false, false, weights(i + 1, i))
+ }
+ for (i <- 0 until n) {
+ incrIn(i, i, true, false, 1.0)
+ incrIn(i, i, false, false, 1.0)
}
- for (width <- 2 until n){
- for (i <- 0 until n - width){
+ for (width <- 2 until n) {
+ for (i <- 0 until n - width) {
val j = i + width
//complete link inside
- for (m <- i until j){
- incrIn(i,j,true,true, in(i,m,true,false) *
in(m+1,j,false,false) * weights(i,j))
- incrIn(i,j,false,true, in(i,m,true,false) *
in(m+1,j,false,false) * weights(j,i))
+ for (m <- i until j) {
+ incrIn(i, j, true, true, in(i, m, true, false) * in(m + 1, j,
false, false) * weights(i, j))
+ incrIn(i, j, false, true, in(i, m, true, false) * in(m + 1, j,
false, false) * weights(j, i))
}
//complete sequence inside
- for (m <- i until j){
- incrIn(i,j,true,false, in(i,m,true,false) * in(m,j,true,true))
- }
- for (m <- i + 1 until j+1){
- incrIn(i,j,false,false, in(i,m,false,true) *
in(m,j,false,false))
+ for (m <- i until j) {
+ incrIn(i, j, true, false, in(i, m, true, false) * in(m, j,
true, true))
+ }
+ for (m <- i + 1 until j + 1) {
+ incrIn(i, j, false, false, in(i, m, false, true) * in(m, j,
false, false))
}
}
}
- Z = in(0,n-1,true,false)
+ Z = in(0, n - 1, true, false)
//outside probabilities
- incrOut(0, n-1, true, true, 1.0)
- incrOut(0, n-1, true, false, 1.0)
-
- for (width <- (1 until n-1).reverse){
- for (i <- 0 until n - width){
+ incrOut(0, n - 1, true, true, 1.0)
+ incrOut(0, n - 1, true, false, 1.0)
+
+ for (width <- (1 until n - 1).reverse) {
+ for (i <- 0 until n - width) {
val j = i + width
//complete sequence outside
- for (h <- j + 1 until n){
- incrOut(i,j,true,false, out(i,h,true,false) *
in(j,h,true,true) +
- out(i,h,true,true) * in(j+1,h,false,false) * weights(i,h) +
- out(i,h,false,true) * in(j+1,h,false,false) * weights(h,i))
- }
- for (v <- 0 until i){
- incrOut(i,j,false,false, out(v,j,false,false) *
in(v,i,false,true) +
- out(v,j,true,true) * in(v,i-1,true,false) * weights(v,j) +
- out(v,j,false,true) * in(v,i-1,true,false) * weights(j,v))
+ for (h <- j + 1 until n) {
+ incrOut(i, j, true, false, out(i, h, true, false) * in(j, h,
true, true) +
+ out(i, h, true, true) * in(j + 1, h, false, false) *
weights(i, h) +
+ out(i, h, false, true) * in(j + 1, h, false, false) *
weights(h, i))
+ }
+ for (v <- 0 until i) {
+ incrOut(i, j, false, false, out(v, j, false, false) * in(v, i,
false, true) +
+ out(v, j, true, true) * in(v, i - 1, true, false) *
weights(v, j) +
+ out(v, j, false, true) * in(v, i - 1, true, false) *
weights(j, v))
}
//complete link outside
- for (v <- 0 until i + 1){
- incrOut(i,j,true,true, out(v,j,true,false) *
in(v,i,true,false))
- }
- for (h <- j until n){
- incrOut(i,j,false,true, out(i,h,false,false) *
in(j,h,false,false))
+ for (v <- 0 until i + 1) {
+ incrOut(i, j, true, true, out(v, j, true, false) * in(v, i,
true, false))
+ }
+ for (h <- j until n) {
+ incrOut(i, j, false, true, out(i, h, false, false) * in(j, h,
false, false))
}
@@ -312,8 +330,8 @@
for (i <- 0 until n; j <- i + 1 until n) {
- total(i -> j) = in(i,j,true,true) * out(i,j,true,true)
- total(j -> i) = in(i,j,false,true) * out(i,j,false,true)
+ total(i -> j) = in(i, j, true, true) * out(i, j, true, true)
+ total(j -> i) = in(i, j, false, true) * out(i, j, false, true)
}
result
=======================================
---
/branches/thefuture-modules/thebeast-core/src/main/scala/org/riedelcastro/thebeast/solve/SumProductBeliefPropagation.scala
Mon Mar 15 10:46:14 2010
+++
/branches/thefuture-modules/thebeast-core/src/main/scala/org/riedelcastro/thebeast/solve/SumProductBeliefPropagation.scala
Fri Mar 26 20:58:54 2010
@@ -5,8 +5,6 @@
import env._
import doubles._
import util.{Logging, Trackable}
-import vectors.{VectorSum, VectorTerm, QuantifiedVectorSum, VectorDotApp}
-
/**
* @author Sebastian Riedel
*/
@@ -106,7 +104,7 @@
graph.addTerms(terms.map(DoubleTermOptimizer.optimize(_)))
**|
- debug("BP Nodes: %s".format(graph.nodes.mkString(",")))
+ debug("%d BP Nodes: %s".format(graph.nodes.size,
graph.nodes.mkString(",")))
_iterations = 0
|**("Message passing")
=======================================
---
/branches/thefuture-modules/thebeast-core/src/test/scala/org/riedelcastro/thebeast/solve/SumProductBeliefPropagationSpecification.scala
Fri Sep 25 23:42:36 2009
+++
/branches/thefuture-modules/thebeast-core/src/test/scala/org/riedelcastro/thebeast/solve/SumProductBeliefPropagationSpecification.scala
Fri Mar 26 20:58:54 2010
@@ -1,9 +1,9 @@
package org.riedelcastro.thebeast.solve
-import env.doubles._
-import env.{TheBeastEnv}
-import specs.Specification
import org.specs.runner.JUnit4
+import org.specs.Specification
+import org.riedelcastro.thebeast.env.TheBeastEnv
+import org.riedelcastro.thebeast.AppleTreeFixtures
/**