GradientMachine extends AbstractVectorClassifier implements OnlineLearner, Writable { public GradientMachine(int numFeatures, int numHidden, int numOutput) { this.numFeatures = numFeatures; this.numHidden = numHidden; this.numOutput = numOutput; hiddenWeights = new DenseVector[numHidden]; for (int i = 0; i < numHidden; i++) { hiddenWeights[i] = new DenseVector(numFeatures); hiddenWeights[i].assign(0); } hiddenBias = new DenseVector(numHidden); hiddenBias.assign(0); outputWeights = new DenseVector[numOutput]; for (int i = 0; i < numOutput; i++) { outputWeights[i] = new DenseVector(numHidden); outputWeights[i].assign(0); } outputBias = new DenseVector(numOutput); outputBias.assign(0); rnd = RandomUtils.getRandom(); } GradientMachine(int numFeatures, int numHidden, int numOutput); void initWeights(Random gen); GradientMachine learningRate(double learningRate); GradientMachine regularization(double regularization); GradientMachine sparsity(double sparsity); GradientMachine sparsityLearningRate(double sparsityLearningRate); void copyFrom(GradientMachine other); @Override int numCategories(); int numFeatures(); int numHidden(); DenseVector inputToHidden(Vector input); DenseVector hiddenToOutput(Vector hiddenActivation); void updateRanking(Vector hiddenActivation,
                            Collection<Integer> goodLabels,
                            int numTrials,
                            Random gen); @Override Vector classify(Vector instance); @Override Vector classifyNoLink(Vector instance); @Override double classifyScalar(Vector instance); GradientMachine copy(); @Override void write(DataOutput out); @Override void readFields(DataInput in); @Override void close(); @Override void train(long trackingKey, String groupKey, int actual, Vector instance); @Override void train(long trackingKey, int actual, Vector instance); @Override void train(int actual, Vector instance); static final int WRITABLE_VERSION; }
@Test public void testGradientmachine() throws IOException { Vector target = readStandardData(); GradientMachine grad = new GradientMachine(8,4,2).learningRate(0.1).regularization(0.01); Random gen = RandomUtils.getRandom(); grad.initWeights(gen); train(getInput(), target, grad); test(getInput(), target, grad, 1.0, 1); }