summaryrefslogtreecommitdiff
path: root/gi/posterior-regularisation/prjava/src/optimization/gradientBasedMethods/DebugHelpers.java
diff options
context:
space:
mode:
authordesaicwtf <desaicwtf@ec762483-ff6d-05da-a07a-a48fb63a330f>2010-07-09 16:59:55 +0000
committerdesaicwtf <desaicwtf@ec762483-ff6d-05da-a07a-a48fb63a330f>2010-07-09 16:59:55 +0000
commit7f69c868c41e4b36eecf9d3b1dc22f3f3aa1540c (patch)
treed22aa7b6f47248ed6da02b77a0680b6b83e67b63 /gi/posterior-regularisation/prjava/src/optimization/gradientBasedMethods/DebugHelpers.java
parent4e37402323c3227e90a89345387834e149732b5c (diff)
add optimization library source code
git-svn-id: https://ws10smt.googlecode.com/svn/trunk@204 ec762483-ff6d-05da-a07a-a48fb63a330f
Diffstat (limited to 'gi/posterior-regularisation/prjava/src/optimization/gradientBasedMethods/DebugHelpers.java')
-rw-r--r--gi/posterior-regularisation/prjava/src/optimization/gradientBasedMethods/DebugHelpers.java65
1 files changed, 65 insertions, 0 deletions
diff --git a/gi/posterior-regularisation/prjava/src/optimization/gradientBasedMethods/DebugHelpers.java b/gi/posterior-regularisation/prjava/src/optimization/gradientBasedMethods/DebugHelpers.java
new file mode 100644
index 00000000..6dc4ef6c
--- /dev/null
+++ b/gi/posterior-regularisation/prjava/src/optimization/gradientBasedMethods/DebugHelpers.java
@@ -0,0 +1,65 @@
+package optimization.gradientBasedMethods;
+
+import java.util.ArrayList;
+
+import optimization.util.MathUtils;
+
+
+
+public class DebugHelpers {
+ public static void getLineSearchGraph(Objective o, double[] direction,
+ double[] parameters, double originalObj,
+ double originalDot, double c1, double c2){
+ ArrayList<Double> stepS = new ArrayList<Double>();
+ ArrayList<Double> obj = new ArrayList<Double>();
+ ArrayList<Double> norm = new ArrayList<Double>();
+ double[] gradient = new double[o.getNumParameters()];
+ double[] newParameters = parameters.clone();
+ MathUtils.plusEquals(newParameters,direction,0);
+ o.setParameters(newParameters);
+ double minValue = o.getValue();
+ int valuesBiggerThanMax = 0;
+ for(double step = 0; step < 2; step +=0.01 ){
+ newParameters = parameters.clone();
+ MathUtils.plusEquals(newParameters,direction,step);
+ o.setParameters(newParameters);
+ double newValue = o.getValue();
+ gradient = o.getGradient();
+ double newgradDirectionDot = MathUtils.dotProduct(gradient,direction);
+ stepS.add(step);
+ obj.add(newValue);
+ norm.add(newgradDirectionDot);
+ if(newValue <= minValue){
+ minValue = newValue;
+ }else{
+ valuesBiggerThanMax++;
+ }
+
+ if(valuesBiggerThanMax > 10){
+ break;
+ }
+
+ }
+ System.out.println("step\torigObj\tobj\tsuffdec\tnorm\tcurvature1");
+ for(int i = 0; i < stepS.size(); i++){
+ double cnorm= norm.get(i);
+ System.out.println(stepS.get(i)+"\t"+originalObj +"\t"+obj.get(i) + "\t" +
+ (originalObj + originalDot*((Double)stepS.get(i))*c1) +"\t"+Math.abs(cnorm) +"\t"+c2*Math.abs(originalDot));
+ }
+ }
+
+ public static double[] getNumericalGradient(Objective o, double[] parameters, double epsilon){
+ int nrParameters = o.getNumParameters();
+ double[] gradient = new double[nrParameters];
+ double[] newParameters;
+ double originalValue = o.getValue();
+ for(int parameter = 0; parameter < nrParameters; parameter++){
+ newParameters = parameters.clone();
+ newParameters[parameter]+=epsilon;
+ o.setParameters(newParameters);
+ double newValue = o.getValue();
+ gradient[parameter]=(newValue-originalValue)/epsilon;
+ }
+ return gradient;
+ }
+}