[Home] DMelt member access. Java source code: com.datumbox.framework.core.machinelearning.classification.MaximumEntropy
/**
* Copyright (C) 2013-2017 Vasilis Vryniotis
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.datumbox.framework.core.machinelearning.classification;
import com.datumbox.framework.common.Configuration;
import com.datumbox.framework.common.concurrency.ForkJoinStream;
import com.datumbox.framework.common.concurrency.StreamMethods;
import com.datumbox.framework.common.dataobjects.AssociativeArray;
import com.datumbox.framework.core.common.dataobjects.Dataframe;
import com.datumbox.framework.core.common.dataobjects.Record;
import com.datumbox.framework.common.dataobjects.TypeInference;
import com.datumbox.framework.common.storage.interfaces.BigMap;
import com.datumbox.framework.common.storage.interfaces.StorageEngine;
import com.datumbox.framework.common.storage.interfaces.StorageEngine.MapType;
import com.datumbox.framework.common.storage.interfaces.StorageEngine.StorageHint;
import com.datumbox.framework.core.machinelearning.common.abstracts.AbstractTrainer;
import com.datumbox.framework.core.machinelearning.common.abstracts.modelers.AbstractClassifier;
import com.datumbox.framework.core.machinelearning.common.interfaces.PredictParallelizable;
import com.datumbox.framework.core.machinelearning.common.interfaces.TrainParallelizable;
import com.datumbox.framework.core.statistics.descriptivestatistics.Descriptives;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* The MaximumEntropy implements the Max Ent classifier which is an alternative
* implementation of Multinomial Logistic Regression model.
*
* References:
* http://blog.datumbox.com/machine-learning-tutorial-the-max-entropy-text-classifier/
* http://www.cs.cmu.edu/afs/cs/user/aberger/www/html/tutorial/node3.html
* https://web.archive.org/web/20121021173335/http://acl.ldc.upenn.edu/P/P02/P02-1002.pdf
*
* @author Vasilis Vryniotis
*/
public class MaximumEntropy extends AbstractClassifier implements PredictParallelizable, TrainParallelizable {
/** {@inheritDoc} */
public static class ModelParameters extends AbstractClassifier.AbstractModelParameters {
private static final long serialVersionUID = 1L;
@BigMap(keyClass=List.class, valueClass=Double.class, mapType=MapType.HASHMAP, storageHint=StorageHint.IN_MEMORY, concurrent=true)
private Map, Double> lambdas; //the lambda parameters of the model
/**
* @param storageEngine
* @see AbstractTrainer.AbstractModelParameters#AbstractModelParameters(StorageEngine)
*/
protected ModelParameters(StorageEngine storageEngine) {
super(storageEngine);
}
/**
* Getter for the Lambda coefficients.
*
* @return
*/
public Map, Double> getLambdas() {
return lambdas;
}
/**
* Setter for the Lambda coefficients.
*
* @param lambdas
*/
protected void setLambdas(Map, Double> lambdas) {
this.lambdas = lambdas;
}
}
/** {@inheritDoc} */
public static class TrainingParameters extends AbstractClassifier.AbstractTrainingParameters {
private static final long serialVersionUID = 1L;
private int totalIterations=100;
/**
* Getter for the total iterations of the training process.
*
* @return
*/
public int getTotalIterations() {
return totalIterations;
}
/**
* Setter for the total iterations of the training process.
*
* @param totalIterations
*/
public void setTotalIterations(int totalIterations) {
this.totalIterations = totalIterations;
}
}
/**
* @param trainingParameters
* @param configuration
* @see AbstractTrainer#AbstractTrainer(AbstractTrainingParameters, Configuration)
*/
protected MaximumEntropy(TrainingParameters trainingParameters, Configuration configuration) {
super(trainingParameters, configuration);
streamExecutor = new ForkJoinStream(knowledgeBase.getConfiguration().getConcurrencyConfiguration());
}
/**
* @param storageName
* @param configuration
* @see AbstractTrainer#AbstractTrainer(String, Configuration)
*/
protected MaximumEntropy(String storageName, Configuration configuration) {
super(storageName, configuration);
streamExecutor = new ForkJoinStream(knowledgeBase.getConfiguration().getConcurrencyConfiguration());
}
private boolean parallelized = true;
/**
* This executor is used for the parallel processing of streams with custom
* Thread pool.
*/
protected final ForkJoinStream streamExecutor;
/** {@inheritDoc} */
@Override
public boolean isParallelized() {
return parallelized;
}
/** {@inheritDoc} */
@Override
public void setParallelized(boolean parallelized) {
this.parallelized = parallelized;
}
/** {@inheritDoc} */
@Override
protected void _predict(Dataframe newData) {
_predictDatasetParallel(newData, knowledgeBase.getStorageEngine(), knowledgeBase.getConfiguration().getConcurrencyConfiguration());
}
/** {@inheritDoc} */
@Override
public Prediction _predictRecord(Record r) {
Set