-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
193 additions
and
16 deletions.
There are no files selected for viewing
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
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
66 changes: 66 additions & 0 deletions
66
...nqs/psl/application/learning/weight/gradient/policygradient/PolicyGradientBinaryStep.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* This file is part of the PSL software. | ||
* Copyright 2011-2015 University of Maryland | ||
* Copyright 2013-2023 The Regents of the University of California | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.linqs.psl.application.learning.weight.gradient.policygradient; | ||
|
||
import org.linqs.psl.database.AtomStore; | ||
import org.linqs.psl.database.Database; | ||
import org.linqs.psl.model.atom.ObservedAtom; | ||
import org.linqs.psl.model.atom.RandomVariableAtom; | ||
import org.linqs.psl.model.rule.Rule; | ||
import org.linqs.psl.util.Logger; | ||
import org.linqs.psl.util.MathUtils; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* Learns parameters for a model by minimizing the squared error loss function | ||
* using the policy gradient learning framework. | ||
*/ | ||
public class PolicyGradientBinaryStep extends PolicyGradient { | ||
private static final Logger log = Logger.getLogger(PolicyGradientBinaryStep.class); | ||
|
||
public PolicyGradientBinaryStep(List<Rule> rules, Database trainTargetDatabase, Database trainTruthDatabase, | ||
Database validationTargetDatabase, Database validationTruthDatabase, boolean runValidation) { | ||
super(rules, trainTargetDatabase, trainTruthDatabase, validationTargetDatabase, validationTruthDatabase, runValidation); | ||
} | ||
|
||
@Override | ||
protected float computeSupervisedLoss() { | ||
AtomStore atomStore = trainInferenceApplication.getTermStore().getAtomStore(); | ||
|
||
float supervisedLoss = 0.0f; | ||
for (Map.Entry<RandomVariableAtom, ObservedAtom> entry : trainingMap.getLabelMap().entrySet()) { | ||
RandomVariableAtom randomVariableAtom = entry.getKey(); | ||
ObservedAtom observedAtom = entry.getValue(); | ||
|
||
int atomIndex = atomStore.getAtomIndex(randomVariableAtom); | ||
if (atomIndex == -1) { | ||
// This atom is not in the current batch. | ||
continue; | ||
} | ||
|
||
if (!MathUtils.equals(observedAtom.getValue(), atomStore.getAtom(atomIndex).getValue())) { | ||
supervisedLoss = 1.0f; | ||
break; | ||
} | ||
} | ||
|
||
return supervisedLoss; | ||
} | ||
} |
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
55 changes: 55 additions & 0 deletions
55
...psl/application/learning/weight/gradient/policygradient/PolicyGradientBinaryStepTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* This file is part of the PSL software. | ||
* Copyright 2011-2015 University of Maryland | ||
* Copyright 2013-2023 The Regents of the University of California | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.linqs.psl.application.learning.weight.gradient.policygradient; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.linqs.psl.application.inference.mpe.DualBCDInference; | ||
import org.linqs.psl.application.learning.weight.WeightLearningApplication; | ||
import org.linqs.psl.application.learning.weight.WeightLearningTest; | ||
import org.linqs.psl.config.Options; | ||
|
||
public class PolicyGradientBinaryStepTest extends WeightLearningTest { | ||
@Before | ||
public void setup() { | ||
super.setup(); | ||
|
||
Options.WLA_INFERENCE.set(DualBCDInference.class.getName()); | ||
Options.WLA_GRADIENT_DESCENT_SYMBOLIC_LEARNING.set(false); | ||
} | ||
|
||
@Override | ||
protected WeightLearningApplication getBaseWLA() { | ||
return new PolicyGradientBinaryStep(info.model.getRules(), trainTargetDatabase, trainTruthDatabase, | ||
validationTargetDatabase, validationTruthDatabase, false); | ||
} | ||
|
||
@Test | ||
public void DualBCDFriendshipRankTest() { | ||
Options.WLA_INFERENCE.set(DualBCDInference.class.getName()); | ||
|
||
super.friendshipRankTest(); | ||
} | ||
|
||
@Test | ||
public void DistributedDualBCDFriendshipRankTest() { | ||
Options.WLA_INFERENCE.set(DualBCDInference.class.getName()); | ||
|
||
super.friendshipRankTest(); | ||
} | ||
} |