/// Performs one pattern-presentation of stochastic gradient descent, and dynamically tunes the // learning rate void doSomeTraining() { // Present a sample of beliefs and corresponding contentment for training int index = rand.nextInt(trainSize); model.regularize(learningRate, 0.000001); model.trainIncremental(samples.row(index), contentment.row(index), learningRate); }
/// Marshals this model to a JSON DOM. JSONObject marshal() { JSONObject obj = new JSONObject(); obj.put("model", model.marshal()); obj.put("samples", samples.marshal()); obj.put("contentment", contentment.marshal()); obj.put("trainPos", trainPos); obj.put("trainSize", trainSize); obj.put("trainIters", trainIters); obj.put("learningRate", learningRate); return obj; }
/// Refines this model based on feedback from the teacher void trainIncremental(double[] sample_beliefs, double sample_contentment) { // Buffer the samples double[] dest = samples.row(trainPos); if (sample_beliefs.length != dest.length) throw new IllegalArgumentException("size mismatch"); for (int i = 0; i < dest.length; i++) dest[i] = sample_beliefs[i]; contentment.row(trainPos)[0] = sample_contentment; trainPos++; trainSize = Math.max(trainSize, trainPos); if (trainPos >= samples.rows()) trainPos = 0; // Do a few iterations of stochastic gradient descent int iters = Math.min(trainIters, trainSize); for (int i = 0; i < iters; i++) doSomeTraining(); }