Fixed IllegalStateException during models training

This commit is contained in:
Vladislav Chumak
2019-02-21 22:25:18 +01:00
parent 6e2e3dd6f5
commit 4a6e992f03
7 changed files with 24 additions and 7 deletions
@@ -66,7 +66,6 @@ public class ManeuverClassifierTrainer {
LoggingUtil.logInfo("Training with " + trainManeuvers.size() + " maneuvers...");
double[][] x = modelContext.getXMatrix(maneuvers);
int[] y = labelExtraction.getYVector(maneuvers);
classifierModel.resetTrainingStats();
classifierModel.train(x, y);
LoggingUtil.logInfo("Training finished. Validating on train dataset...");
ManeuverClassifierScoring classifierScoring = new ManeuverClassifierScoring(classifierModel);
@@ -100,7 +100,6 @@ public class TwdTransitionClassifierTrainer {
i++;
}
LoggingUtil.logInfo("Training with " + numberOfTrainingInstances + " instances...");
classifierModel.resetTrainingStats();
classifierModel.train(x, y);
LoggingUtil.logInfo("Training finished. Validating on train dataset...");
TwdTransitionClassifierScoring classifierScoring = new TwdTransitionClassifierScoring(classifierModel);
@@ -41,6 +41,9 @@ public class IncrementalSingleDimensionPolynomialRegressorTrainerHelper {
}
public void incrementRmseCalculation(double[] x, double y) {
if (!model.isModelReady()) {
model.setModelAsReadyAfterSuccessfulTraining();
}
double predictedStd = model.getValue(x);
double diff = predictedStd - y;
squareErrorSum += diff * diff;
@@ -21,8 +21,7 @@ public class TwdTransitionManualTrainingDataInputRegressorTrainer {
throws Exception {
IncrementalSingleDimensionPolynomialRegressorTrainerHelper trainerHelper = new IncrementalSingleDimensionPolynomialRegressorTrainerHelper(
regressorModelStore, model);
LoggingUtil
.logInfo("########## Training of " + model.getModelContext().getId() + " started...");
LoggingUtil.logInfo("########## Training of " + model.getModelContext().getId() + " started...");
double[] modelInput = new double[1];
for (int i = 0; i < inputOutputPairs.length; i++) {
double xi = inputOutputPairs[i][0];
@@ -51,12 +51,16 @@ public abstract class AbstractTrainableModel<InstanceType, MC extends ModelConte
@Override
public void setStatsAfterSuccessfulTraining(double trainScore, double testScore, long numberOfTrainingInstances) {
trainingFinished = true;
this.trainScore = trainScore;
this.testScore = testScore;
this.numberOfTrainingInstances = numberOfTrainingInstances;
}
@Override
public void setModelAsReadyAfterSuccessfulTraining() {
trainingFinished = true;
}
@Override
public void resetTrainingStats() {
trainingFinished = false;
@@ -46,8 +46,9 @@ public interface TrainableModel<InstanceType, MC extends ModelContext<InstanceTy
long getNumberOfTrainingInstances();
/**
* Sets the training statistics for this model and makes this model ready (see {@link #isModelReady()}) for
* prediction tasks. This method must be called only after model training has been successfully completed.
* Sets the training statistics for this model after successful training which should have been previously signaled
* with {@link #setModelAsReadyAfterSuccessfulTraining()}. This method must be called only after model training has
* been successfully completed.
*
* @param trainScore
* Macro-averaged F2-Score with training data (see {@link #getTrainScore()})
@@ -58,6 +59,16 @@ public interface TrainableModel<InstanceType, MC extends ModelContext<InstanceTy
*/
void setStatsAfterSuccessfulTraining(double trainScore, double testScore, long numberOfTrainingInstances);
/**
* Marks this model as ready for prediction tasks. This method should be called after successful training of this
* model. Afterwards, model evaluation should be performed to determine the training and test score of the model and
* set it with {@link #setStatsAfterSuccessfulTraining(double, double, long)}.
*
* @see #isModelReady()
* @see #setStatsAfterSuccessfulTraining(double, double, long)
*/
void setModelAsReadyAfterSuccessfulTraining();
/**
* Resets the training stats and marks this model as not ready (see {@link #isModelReady()}). Should be called
* before model training starts.
@@ -38,6 +38,7 @@ public abstract class AbstractSmileClassificationModel<InstanceType, MC extends
@Override
public void train(double[][] x, int[] y) {
resetTrainingStats();
PreprocessingConfig preprocessingConfig = getPreprocessingConfig();
scaler = null;
if (preprocessingConfig.isScaling()) {
@@ -56,6 +57,7 @@ public abstract class AbstractSmileClassificationModel<InstanceType, MC extends
x = pca.project(x);
}
internalModel = trainInternalModel(x, y);
setModelAsReadyAfterSuccessfulTraining();
}
protected abstract SoftClassifier<double[]> trainInternalModel(double[][] x, int[] y);