|
| 1 | +package edu.stanford.nlp.optimization; |
| 2 | + |
| 3 | +import edu.stanford.nlp.util.Pair; |
| 4 | + |
| 5 | +/** |
| 6 | + * Stochastic Gradient Descent Minimizer. |
| 7 | + * |
| 8 | + * |
| 9 | + * The basic way to use the minimizer is with a null constructor, then |
| 10 | + * the simple minimize method: |
| 11 | + * <p/> |
| 12 | + * <p><code>Minimizer smd = new InefficientSGDMinimizer();</code> |
| 13 | + * <br><code>DiffFunction df = new SomeDiffFunction(); //Note that it must be a incidence of AbstractStochasticCachingDiffFunction</code> |
| 14 | + * <br><code>double tol = 1e-4;</code> |
| 15 | + * <br><code>double[] initial = getInitialGuess();</code> |
| 16 | + * <br><code>int maxIterations = someSafeNumber; |
| 17 | + * <br><code>double[] minimum = qnm.minimize(df,tol,initial,maxIterations);</code> |
| 18 | + * <p/> |
| 19 | + * Constructing with a null constructor will use the default values of |
| 20 | + * <p> |
| 21 | + * <br><code>batchSize = 15;</code> |
| 22 | + * <br><code>initialGain = 0.1;</code> |
| 23 | + * <p/> |
| 24 | + * <br> NOTE: This class was previously called SGDMinimizer. SGDMinimizer is now what was StochasticInPlaceMinimizer. New projects should use that class. |
| 25 | + * <p/> |
| 26 | + * |
| 27 | + * @author <a href="mailto:[email protected]">Alex Kleeman</a> |
| 28 | + * @version 1.0 |
| 29 | + * @since 1.0 |
| 30 | + */ |
| 31 | +public class InefficientSGDMinimizer<T extends Function> extends StochasticMinimizer<T> { |
| 32 | + |
| 33 | + |
| 34 | + @Override |
| 35 | + public void shutUp() { |
| 36 | + this.quiet = true; |
| 37 | + } |
| 38 | + |
| 39 | + public void setBatchSize(int batchSize) { |
| 40 | + bSize = batchSize; |
| 41 | + } |
| 42 | + |
| 43 | + public InefficientSGDMinimizer() { |
| 44 | + } |
| 45 | + |
| 46 | + public InefficientSGDMinimizer(double SGDGain, int batchSize){ |
| 47 | + this(SGDGain,batchSize,50); |
| 48 | + } |
| 49 | + |
| 50 | + public InefficientSGDMinimizer(double SGDGain, int batchSize, int passes){ |
| 51 | + this(SGDGain,batchSize,passes,Long.MAX_VALUE,false); |
| 52 | + } |
| 53 | + |
| 54 | + public InefficientSGDMinimizer(double SGDGain, int batchSize, int passes, boolean outputToFile){ |
| 55 | + this(SGDGain, batchSize, passes, Long.MAX_VALUE ,outputToFile ); |
| 56 | + } |
| 57 | + |
| 58 | + public InefficientSGDMinimizer(double SGDGain, int batchSize, int passes, long maxTime){ |
| 59 | + this(SGDGain,batchSize,passes,maxTime,false); |
| 60 | + } |
| 61 | + |
| 62 | + public InefficientSGDMinimizer(double SGDGain, int batchSize, int passes, long maxTime, boolean outputToFile){ |
| 63 | + bSize = batchSize; |
| 64 | + gain = SGDGain; |
| 65 | + this.numPasses = passes; |
| 66 | + this.outputIterationsToFile = outputToFile; |
| 67 | + this.maxTime = maxTime; |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + @Override |
| 72 | + protected String getName(){ |
| 73 | + int g = (int) gain*1000; |
| 74 | + return "SGD" + bSize + "_g" + g; |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | + public Pair <Integer,Double> tune(Function function, double[] initial,long msPerTest,double gainLow,double gainHigh){ |
| 79 | + this.quiet = true; |
| 80 | + gain = tuneGain(function, initial, msPerTest, gainLow,gainHigh); |
| 81 | + bSize = tuneBatch(function,initial,msPerTest,1); |
| 82 | + |
| 83 | + return new Pair<Integer,Double>(bSize, gain); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public Pair<Integer,Double> tune(Function function,double[] initial, long msPerTest){ |
| 88 | + return this.tune(function, initial, msPerTest, 1e-7,1.0); |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + protected void takeStep(AbstractStochasticCachingDiffFunction dfunction){ |
| 94 | + for(int i = 0; i < x.length; i++){ |
| 95 | + newX[i] = x[i] - gain*gainSchedule(k,5*numBatches)*grad[i]; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | + public static void main(String[] args) { |
| 105 | + // optimizes test function using doubles and floats |
| 106 | + // test function is (0.5 sum(x_i^2 * var_i)) ^ PI |
| 107 | + // where var is a vector of random nonnegative numbers |
| 108 | + // dimensionality is variable. |
| 109 | + final int dim = 500000; |
| 110 | + final double maxVar = 5; |
| 111 | + final double[] var = new double[dim]; |
| 112 | + double[] init = new double[dim]; |
| 113 | + |
| 114 | + for (int i = 0; i < dim; i++) { |
| 115 | + init[i] = ((i + 1) / (double) dim - 0.5);//init[i] = (Math.random() - 0.5); |
| 116 | + var[i] = maxVar * (i + 1) / dim; |
| 117 | + } |
| 118 | + |
| 119 | + final double[] grads = new double[dim]; |
| 120 | + |
| 121 | + final DiffFunction f = new DiffFunction() { |
| 122 | + @Override |
| 123 | + public double[] derivativeAt(double[] x) { |
| 124 | + double val = Math.PI * valuePow(x, Math.PI - 1); |
| 125 | + for (int i = 0; i < dim; i++) { |
| 126 | + grads[i] = x[i] * var[i] * val; |
| 127 | + } |
| 128 | + return grads; |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public double valueAt(double[] x) { |
| 133 | + return 1.0 + valuePow(x, Math.PI); |
| 134 | + } |
| 135 | + |
| 136 | + private double valuePow(double[] x, double pow) { |
| 137 | + double val = 0.0; |
| 138 | + for (int i = 0; i < dim; i++) { |
| 139 | + val += x[i] * x[i] * var[i]; |
| 140 | + } |
| 141 | + return Math.pow(val * 0.5, pow); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public int domainDimension() { |
| 146 | + return dim; |
| 147 | + } |
| 148 | + }; |
| 149 | + |
| 150 | + InefficientSGDMinimizer<DiffFunction> min = new InefficientSGDMinimizer<DiffFunction>(); |
| 151 | + min.minimize(f, 1.0E-4, init); |
| 152 | + } |
| 153 | + |
| 154 | +} |
0 commit comments