Troubles getting started with Factorie. Documentation example (factors section) won't compile.

36 views
Skip to first unread message

Ryad Zenine

unread,
Aug 1, 2016, 11:17:55 AM8/1/16
to Factorie
Hi guys, 

I am currently learning factorie and while browsing the documentation i ran across an example that won"t compile. Can you please help ? As i am also knew to scala i am not sure how to fix it. Thanks in advance. 

The code: 
object Hello extends App {
 
import org.junit.Assert._
 
import cc.factorie._
 
import cc.factorie.la._
 
import cc.factorie.variable._
 
import scala.Tuple2
 
import cc.factorie.model._


 
object LabelDomain extends CategoricalDomain(List("politics", "sports", "arts"))
 
class Label(initialValue: String) extends CategoricalVariable(initialValue) { def domain = LabelDomain }


 
object WordDomain extends CategoricalDomain(List("beat", "beautiful", "election"))
 
// TODO Consider interface improvements to CategoricalVectorDomain initialization.
 
object ArticleDomain extends CategoricalVectorDomain[String] { override def dimensionDomain = WordDomain }
 
class Article(ws: Iterable[String]) extends BinaryFeatureVectorVariable[String](ws) {
   
def domain = ArticleDomain
 
}


 
class MyClassifier(label: Label, article: Article) extends DotFactorWithStatistics2(label, article) {
    val weights
= new DenseTensor2(LabelDomain.size, WordDomain.size)
    weights
(LabelDomain.index("politics"), WordDomain.index("beat")) = 3.0
    weights
(LabelDomain.index("politics"), WordDomain.index("beautiful")) = 2.0
    weights
(LabelDomain.index("politics"), WordDomain.index("election")) = 5.0
    weights
(LabelDomain.index("sports"), WordDomain.index("beat")) = 4.0
    weights
(LabelDomain.index("sports"), WordDomain.index("beautiful")) = 1.0
    weights
(LabelDomain.index("sports"), WordDomain.index("election")) = -1.0
    weights
(LabelDomain.index("arts"), WordDomain.index("beat")) = -2.0
    weights
(LabelDomain.index("arts"), WordDomain.index("beautiful")) = 5.0
    weights
(LabelDomain.index("arts"), WordDomain.index("election")) = 1.0
 
}


  val a1
= new Article("beat election".split(" "))
  val l1
= new Label("politics")
  val cf
= new MyClassifier(l1, a1)


 
// Any now we can do simple exhaustive inference
 
var maxScore = Double.NegativeInfinity
 
var maxLabeling = LabelDomain.head
 
for (labeling <- LabelDomain) {
    l1
.set(labeling)(null)
    val score
= cf.currentScore
   
if (score > maxScore) {
      maxScore
= score
      maxLabeling
= labeling
   
}
 
}
  println
("When scoring " + a1 + " the highest scoring label value is " + maxLabeling)
}


And the compiler error: 
[error] Hello.scala:22: overriding method weights in class DotFactor2 of type => cc.factorie.model.Weights;
[error]  value weights has incompatible type
[error]     val weights = new DenseTensor2(LabelDomain.size, WordDomain.size)
[error]         ^
[error] one error found
[error] (compile:compile) Compilation failed


Here is also the relevant section of my build.sbt
version := "1.0.0-SNAPSHOT"

scalaVersion
:= "2.11.7"

scalacOptions
+= "-deprecation"

libraryDependencies
++= Seq(
 
"org.specs2" %% "specs2-core" % "3.8.4" % "test",
 
"junit" % "junit" % "4.10",
 
"cc.factorie" % "factorie_2.11" % "1.2"
)


Thanks in advance. 





Ryad Zenine

unread,
Aug 1, 2016, 11:58:50 AM8/1/16
to Factorie
Here is a version that compiles. hoppefully it will help somebody else:
class MyClassifier(label: Label, article: Article) extends TensorFactorWithStatistics2(label, article) {

    val weights
= new DenseTensor2(LabelDomain.size, WordDomain.size)
    weights
(LabelDomain.index("politics"), WordDomain.index("beat")) = 3.0
    weights
(LabelDomain.index("politics"), WordDomain.index("beautiful")) = 2.0
    weights
(LabelDomain.index("politics"), WordDomain.index("election")) = 5.0
    weights
(LabelDomain.index("sports"), WordDomain.index("beat")) = 4.0
    weights
(LabelDomain.index("sports"), WordDomain.index("beautiful")) = 1.0
    weights
(LabelDomain.index("sports"), WordDomain.index("election")) = -1.0
    weights
(LabelDomain.index("arts"), WordDomain.index("beat")) = -2.0
    weights
(LabelDomain.index("arts"), WordDomain.index("beautiful")) = 5.0
    weights
(LabelDomain.index("arts"), WordDomain.index("election")) = 1.0
    def statisticsScore(tensor: Tensor): Double = weights dot tensor

 
}

Thomas Luechtefeld

unread,
Apr 5, 2017, 12:00:36 AM4/5/17
to Factorie
In the below line:

val weights = new DenseTensor2(LabelDomain.size, WordDomain.size)

you should have 

val weights = new ConstantWeights(new DenseTensor2(LabelDomain.size, WordDomain.size))

and you have to change to weights.set(tensor) for the assignment of weights.

Thomas Luechtefeld

unread,
Apr 5, 2017, 12:03:34 AM4/5/17
to Factorie
Slight mistake here, you can't edit ConstantWeights, but you could just set the weights in the construction.
Reply all
Reply to author
Forward
0 new messages