-
Notifications
You must be signed in to change notification settings - Fork 18
two changes about joint training #296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
danyaljj
wants to merge
10
commits into
CogComp:master
Choose a base branch
from
danyaljj:mergeJointTrainingFunctions
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
902d9fd
merging joint training
614048a
joint training in ER test.
f13284e
get rid of the old joint training.
924d2ac
small fix to the ER test.
1d21eaa
adding a comment for the case when joint training is not available fo…
b246622
making the joint training a little efficient by creating partial calls.
65ed4a0
Merge branch 'master' of github.com:IllinoisCogComp/saul into latestA…
ae4ddc2
joint training: fixed type issues in partial calls.
3ab1513
fix ER unit test.
627f4ca
version minor bump.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
145 changes: 74 additions & 71 deletions
145
saul-core/src/main/scala/edu/illinois/cs/cogcomp/saul/classifier/JointTrain.scala
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,91 +1,94 @@ | ||
| package edu.illinois.cs.cogcomp.saul.classifier | ||
|
|
||
| import edu.illinois.cs.cogcomp.lbjava.classify.Classifier | ||
| import edu.illinois.cs.cogcomp.lbjava.learn.LinearThresholdUnit | ||
| import edu.illinois.cs.cogcomp.lbjava.learn.{ Learner, LinearThresholdUnit } | ||
| import edu.illinois.cs.cogcomp.saul.datamodel.node.Node | ||
|
|
||
| import scala.reflect.ClassTag | ||
|
|
||
| /** Created by parisakordjamshidi on 29/01/15. | ||
| */ | ||
| object JointTrain { | ||
| def testClassifiers(cls: Classifier, oracle: Classifier, ds: List[AnyRef]): Unit = { | ||
|
|
||
| val results = ds.map({ | ||
| x => | ||
| val pri = cls.discreteValue(x) | ||
| val truth = oracle.discreteValue(x) | ||
| (pri, truth) | ||
| }) | ||
|
|
||
| val tp = results.count({ case (x, y) => x == y && (x == "true") }) * 1.0 | ||
| val fp = results.count({ case (x, y) => x != y && (x == "true") }) * 1.0 | ||
|
|
||
| val tn = results.count({ case (x, y) => x == y && (x == "false") }) * 1.0 | ||
| val fn = results.count({ case (x, y) => x != y && (x == "false") }) * 1.0 | ||
|
|
||
| println(s"tp: $tp fp: $fp tn: $tn fn: $fn ") | ||
| println(s" accuracy ${(tp + tn) / results.size} ") | ||
| println(s" precision ${tp / (tp + fp)} ") | ||
| println(s" recall ${tp / (tp + fn)} ") | ||
| println(s" f1 ${(2.0 * tp) / (2 * tp + fp + fn)} ") | ||
|
|
||
| def apply[HEAD <: AnyRef](node: Node[HEAD], classifiers: List[ConstrainedClassifier[_, HEAD]], iter: Int = 1)(implicit headTag: ClassTag[HEAD]): Unit = { | ||
| train(node, classifiers, iter) | ||
| } | ||
|
|
||
| def apply[HEAD <: AnyRef](node: Node[HEAD], cls: List[ConstrainedClassifier[_, HEAD]])(implicit headTag: ClassTag[HEAD]) = { | ||
| train[HEAD](node, cls, 1) | ||
| @scala.annotation.tailrec | ||
| def train[HEAD <: AnyRef](node: Node[HEAD], classifiers: List[ConstrainedClassifier[_, HEAD]], iter: Int)(implicit headTag: ClassTag[HEAD]): Unit = { | ||
| println("Joint training iterations: " + iter) | ||
| if (iter > 0) { | ||
| val allHeads = node.getTrainingInstances | ||
| allHeads foreach { head => | ||
| classifiers.foreach { | ||
| case typedClassifier: ConstrainedClassifier[_, HEAD] => | ||
| val oracle = typedClassifier.onClassifier.getLabeler | ||
| typedClassifier.getCandidates(head) foreach { candidate => | ||
| typedClassifier.onClassifier.classifier match { | ||
| case _: LinearThresholdUnit => trainLinearThresholdUnitOnce[HEAD](typedClassifier, oracle, candidate) | ||
| case _: SparseNetworkLBP => trainSparseNetworkLearnerOnce[HEAD](typedClassifier, oracle, candidate) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| train(node, classifiers, iter - 1) | ||
| } | ||
| } | ||
|
|
||
| def apply[HEAD <: AnyRef](node: Node[HEAD], cls: List[ConstrainedClassifier[_, HEAD]], it: Int)(implicit headTag: ClassTag[HEAD]) = { | ||
| train[HEAD](node, cls, it) | ||
| def trainLinearThresholdUnitOnce[HEAD <: AnyRef](typedClassifier: ConstrainedClassifier[_, HEAD], oracle: Classifier, candidate: Any): Unit = { | ||
| val result = typedClassifier.classifier.discreteValue(candidate) | ||
| val trueLabel = oracle.discreteValue(candidate) | ||
| if (result.equals("true") && trueLabel.equals("false")) { | ||
| val a = typedClassifier.onClassifier.getExampleArray(candidate) | ||
| val a0 = a(0).asInstanceOf[Array[Int]] | ||
| val a1 = a(1).asInstanceOf[Array[Double]] | ||
| typedClassifier.onClassifier.classifier.asInstanceOf[LinearThresholdUnit].promote(a0, a1, 0.1) | ||
| } else if (result.equals("false") && trueLabel.equals("true")) { | ||
| val a = typedClassifier.onClassifier.getExampleArray(candidate) | ||
| val a0 = a(0).asInstanceOf[Array[Int]] | ||
| val a1 = a(1).asInstanceOf[Array[Double]] | ||
| typedClassifier.onClassifier.classifier.asInstanceOf[LinearThresholdUnit].demote(a0, a1, 0.1) | ||
| } | ||
| } | ||
|
|
||
| @scala.annotation.tailrec | ||
| def train[HEAD <: AnyRef](node: Node[HEAD], cls: List[ConstrainedClassifier[_, HEAD]], it: Int)(implicit headTag: ClassTag[HEAD]): Unit = { | ||
| // forall members in collection of the head (dm.t) do | ||
|
|
||
| println("Training iteration: " + it) | ||
| if (it == 0) { | ||
| // Done | ||
| } else { | ||
| val allHeads = node.getTrainingInstances | ||
| def trainSparseNetworkLearnerOnce[HEAD <: AnyRef](typedClassifier: ConstrainedClassifier[_, HEAD], oracle: Classifier, candidate: Any): Unit = { | ||
| val result = typedClassifier.classifier.discreteValue(candidate) | ||
| val trueLabel = oracle.discreteValue(candidate) | ||
| val ilearner = typedClassifier.onClassifier.classifier.asInstanceOf[SparseNetworkLBP] | ||
| val lLexicon = typedClassifier.onClassifier.getLabelLexicon | ||
| var LTU_actual = 0 | ||
| var LTU_predicted = 0 | ||
| for (i <- 0 until lLexicon.size()) { | ||
| if (lLexicon.lookupKey(i).valueEquals(result)) | ||
| LTU_predicted = i | ||
| if (lLexicon.lookupKey(i).valueEquals(trueLabel)) | ||
| LTU_actual = i | ||
| } | ||
|
|
||
| allHeads foreach { | ||
| h => | ||
| { | ||
| cls.foreach { | ||
| case classifier: ConstrainedClassifier[_, HEAD] => | ||
| val typedC = classifier.asInstanceOf[ConstrainedClassifier[_, HEAD]] | ||
| val oracle = typedC.onClassifier.getLabeler | ||
| // The idea is that when the prediction is wrong the LTU of the actual class should be promoted | ||
| // and the LTU of the predicted class should be demoted. | ||
| if (!result.equals(trueLabel)) { | ||
| val a = typedClassifier.onClassifier.getExampleArray(candidate) | ||
| val a0 = a(0).asInstanceOf[Array[Int]] //exampleFeatures | ||
| val a1 = a(1).asInstanceOf[Array[Double]] // exampleValues | ||
| val exampleLabels = a(2).asInstanceOf[Array[Int]] | ||
| val label = exampleLabels(0) | ||
| var N = ilearner.net.size() | ||
|
|
||
| if (label >= N || ilearner.net.get(label) == null) { | ||
| ilearner.iConjuctiveLables = ilearner.iConjuctiveLables | ilearner.getLabelLexicon.lookupKey(label).isConjunctive | ||
|
|
||
| val ltu: LinearThresholdUnit = ilearner.getbaseLTU | ||
| ltu.initialize(ilearner.getnumExamples, ilearner.getnumFeatures) | ||
| ilearner.net.set(label, ltu) | ||
| N = label + 1 | ||
| } | ||
|
|
||
| typedC.getCandidates(h) foreach { | ||
| x => | ||
| { | ||
| def trainOnce() = { | ||
| val result = typedC.classifier.discreteValue(x) | ||
| val trueLabel = oracle.discreteValue(x) | ||
| // test push | ||
| val ltu_actual: LinearThresholdUnit = ilearner.getLTU(LTU_actual) | ||
| val ltu_predicted: LinearThresholdUnit = ilearner.getLTU(LTU_predicted) | ||
|
|
||
| if (result.equals("true") && trueLabel.equals("false")) { | ||
| val a = typedC.onClassifier.getExampleArray(x) | ||
| val a0 = a(0).asInstanceOf[Array[Int]] | ||
| val a1 = a(1).asInstanceOf[Array[Double]] | ||
| typedC.onClassifier.classifier.asInstanceOf[LinearThresholdUnit].promote(a0, a1, 0.1) | ||
| } else if (result.equals("false") && trueLabel.equals("true")) { | ||
| val a = typedC.onClassifier.getExampleArray(x) | ||
| val a0 = a(0).asInstanceOf[Array[Int]] | ||
| val a1 = a(1).asInstanceOf[Array[Double]] | ||
| typedC.onClassifier.classifier.asInstanceOf[LinearThresholdUnit].demote(a0, a1, 0.1) | ||
| } | ||
| } | ||
| trainOnce() | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| train(node, cls, it - 1) | ||
| if (ltu_actual != null) | ||
| ltu_actual.promote(a0, a1, 0.1) | ||
| if (ltu_predicted != null) | ||
| ltu_predicted.demote(a0, a1, 0.1) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } | ||
94 changes: 0 additions & 94 deletions
94
...core/src/main/scala/edu/illinois/cs/cogcomp/saul/classifier/JointTrainSparseNetwork.scala
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should add the default case and log a warning here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. Added it.