Github user myui commented on a diff in the pull request:
https://github.com/apache/incubator-hivemall/pull/87#discussion_r124519932
--- Diff: core/src/main/java/hivemall/GeneralLearnerBaseUDTF.java ---
@@ -318,66 +412,240 @@ protected void onlineUpdate(@Nonnull final
FeatureValue[] features, final float
@Override
public final void close() throws HiveException {
super.close();
- if (model != null) {
- if (accumulated != null) { // Update model with accumulated
delta
- batchUpdate();
- this.accumulated = null;
- }
- int numForwarded = 0;
- if (useCovariance()) {
- final WeightValueWithCovar probe = new
WeightValueWithCovar();
- final Object[] forwardMapObj = new Object[3];
- final FloatWritable fv = new FloatWritable();
- final FloatWritable cov = new FloatWritable();
- final IMapIterator<Object, IWeightValue> itor =
model.entries();
- while (itor.next() != -1) {
- itor.getValue(probe);
- if (!probe.isTouched()) {
- continue; // skip outputting untouched weights
+ finalizeTraining();
+ forwardModel();
+ this.accumulated = null;
+ this.model = null;
+ }
+
+ @VisibleForTesting
+ public void finalizeTraining() throws HiveException {
+ if (count == 0L) {
+ this.model = null;
+ return;
+ }
+ if (is_mini_batch) { // Update model with accumulated delta
+ batchUpdate();
+ }
+ if (iterations > 1) {
+ runIterativeTraining(iterations);
+ }
+ }
+
+ protected final void runIterativeTraining(@Nonnegative final int
iterations)
+ throws HiveException {
+ final ByteBuffer buf = this.inputBuf;
+ final NioStatefullSegment dst = this.fileIO;
+ assert (buf != null);
+ assert (dst != null);
+ final long numTrainingExamples = count;
+
+ final Reporter reporter = getReporter();
+ final Counters.Counter iterCounter = (reporter == null) ? null :
reporter.getCounter(
+ "hivemall.GeneralLearnerBase$Counter", "iteration");
+
+ try {
+ if (dst.getPosition() == 0L) {// run iterations w/o temporary
file
+ if (buf.position() == 0) {
+ return; // no training example
+ }
+ buf.flip();
+
+ int iter = 2;
+ double cumLossPrev;
+ for (; iter <= iterations; iter++) {
+ cumLossPrev = cumLoss;
+ this.cumLoss = 0.d;
+
+ reportProgress(reporter);
+ setCounterValue(iterCounter, iter);
+
+ while (buf.remaining() > 0) {
+ int recordBytes = buf.getInt();
+ assert (recordBytes > 0) : recordBytes;
+ int featureVectorLength = buf.getInt();
+ final FeatureValue[] featureVector = new
FeatureValue[featureVectorLength];
+ for (int j = 0; j < featureVectorLength; j++) {
+ featureVector[j] = new FeatureValue(buf);
+ }
+ float target = buf.getFloat();
+ train(featureVector, target);
+ }
+ buf.rewind();
+
+ if (is_mini_batch) { // Update model with accumulated
delta
+ batchUpdate();
+ }
+
+ logger.info("[iter " + iter + "] cumulative loss: " +
cumLoss);
+
+ if (Math.abs(cumLossPrev - cumLoss) < tol) {
--- End diff --
`cumLoss` is always 0 due to ` this.cumLoss = 0.d;`. So, this conversion
check is not working properly.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---