use of ml.shifu.shifu.core.alg.SVMTrainer in project shifu by ShifuML.
the class TrainModelProcessor method runAkkaTrain.
/**
* run training process with number of bags
*
* @param numBags
* number of bags, it decide how much trainer will start training
* @throws IOException
*/
private void runAkkaTrain(int numBags) throws IOException {
File models = new File("models");
FileUtils.deleteDirectory(models);
FileUtils.forceMkdir(models);
trainers.clear();
for (int i = 0; i < numBags; i++) {
AbstractTrainer trainer;
if (modelConfig.getAlgorithm().equalsIgnoreCase("NN")) {
trainer = new NNTrainer(modelConfig, i, isDryTrain);
} else if (modelConfig.getAlgorithm().equalsIgnoreCase("SVM")) {
trainer = new SVMTrainer(this.modelConfig, i, isDryTrain);
} else if (modelConfig.getAlgorithm().equalsIgnoreCase("LR")) {
trainer = new LogisticRegressionTrainer(this.modelConfig, i, isDryTrain);
} else {
throw new ShifuException(ShifuErrorCode.ERROR_UNSUPPORT_ALG);
}
trainers.add(trainer);
}
List<Scanner> scanners = null;
if (modelConfig.getAlgorithm().equalsIgnoreCase("DT")) {
LOG.info("Raw Data: " + pathFinder.getNormalizedDataPath());
try {
scanners = ShifuFileUtils.getDataScanners(modelConfig.getDataSetRawPath(), modelConfig.getDataSet().getSource());
} catch (IOException e) {
throw new ShifuException(ShifuErrorCode.ERROR_INPUT_NOT_FOUND, e, pathFinder.getNormalizedDataPath());
}
if (CollectionUtils.isNotEmpty(scanners)) {
AkkaSystemExecutor.getExecutor().submitDecisionTreeTrainJob(modelConfig, columnConfigList, scanners, trainers);
}
} else {
LOG.info("Normalized Data: " + pathFinder.getNormalizedDataPath());
try {
scanners = ShifuFileUtils.getDataScanners(pathFinder.getNormalizedDataPath(), modelConfig.getDataSet().getSource());
} catch (IOException e) {
throw new ShifuException(ShifuErrorCode.ERROR_INPUT_NOT_FOUND, e, pathFinder.getNormalizedDataPath());
}
if (CollectionUtils.isNotEmpty(scanners)) {
AkkaSystemExecutor.getExecutor().submitModelTrainJob(modelConfig, columnConfigList, scanners, trainers);
}
}
// release
closeScanners(scanners);
}
use of ml.shifu.shifu.core.alg.SVMTrainer in project shifu by ShifuML.
the class SVMTrainerTest method setUp.
// MLDataSet dataSet;
// MLDataSet trainSet;
// MLDataSet validSet, testSet;
// Random random;
@BeforeClass
public void setUp() throws IOException {
// .createInitModelConfig("./", "./");
config = new ModelConfig();
config.getTrain().setAlgorithm("SVM");
config.getDataSet().setSource(SourceType.LOCAL);
config.getVarSelect().setFilterNum(2);
config.getDataSet().setDataDelimiter(",");
config.getDataSet().setSource(SourceType.HDFS);
config.getTrain().setParams(new HashMap<String, Object>());
config.getTrain().getParams().put("Const", 1.1);
config.getTrain().getParams().put("Gamma", 0.95);
config.getTrain().getParams().put("Kernel", "rbf");
config.getTrain().setBaggingSampleRate(1.0);
config.getTrain().setBaggingWithReplacement(false);
trainer = new SVMTrainer(config, 0, false);
trainer.setTrainSet(xor_Trainset);
trainer.setValidSet(xor_Validset);
}
use of ml.shifu.shifu.core.alg.SVMTrainer in project shifu by ShifuML.
the class TrainModelActorTest method testActor.
@Test
public void testActor() throws IOException, InterruptedException {
File tmpDir = new File("./tmp");
FileUtils.forceMkdir(tmpDir);
// create normalize data
actorSystem = ActorSystem.create("shifuActorSystem");
ActorRef normalizeRef = actorSystem.actorOf(new Props(new UntypedActorFactory() {
private static final long serialVersionUID = 6777309320338075269L;
public UntypedActor create() throws IOException {
return new NormalizeDataActor(modelConfig, columnConfigList, new AkkaExecStatus(true));
}
}), "normalize-calculator");
List<Scanner> scanners = ShifuFileUtils.getDataScanners("src/test/resources/example/cancer-judgement/DataStore/DataSet1", SourceType.LOCAL);
normalizeRef.tell(new AkkaActorInputMessage(scanners), normalizeRef);
while (!normalizeRef.isTerminated()) {
Thread.sleep(5000);
}
File outputFile = new File("./tmp/NormalizedData");
Assert.assertTrue(outputFile.exists());
// start to run trainer
actorSystem = ActorSystem.create("shifuActorSystem");
File models = new File("./models");
FileUtils.forceMkdir(models);
final List<AbstractTrainer> trainers = new ArrayList<AbstractTrainer>();
for (int i = 0; i < 5; i++) {
AbstractTrainer trainer;
if (modelConfig.getAlgorithm().equalsIgnoreCase("NN")) {
trainer = new NNTrainer(this.modelConfig, i, false);
} else if (modelConfig.getAlgorithm().equalsIgnoreCase("SVM")) {
trainer = new SVMTrainer(this.modelConfig, i, false);
} else if (modelConfig.getAlgorithm().equalsIgnoreCase("LR")) {
trainer = new LogisticRegressionTrainer(this.modelConfig, i, false);
} else {
throw new RuntimeException("unsupport algorithm");
}
trainers.add(trainer);
}
// train model
ActorRef modelTrainRef = actorSystem.actorOf(new Props(new UntypedActorFactory() {
private static final long serialVersionUID = 6777309320338075269L;
public UntypedActor create() throws IOException {
return new TrainModelActor(modelConfig, columnConfigList, new AkkaExecStatus(true), trainers);
}
}), "trainer");
scanners = ShifuFileUtils.getDataScanners("./tmp/NormalizedData", SourceType.LOCAL);
modelTrainRef.tell(new AkkaActorInputMessage(scanners), modelTrainRef);
while (!modelTrainRef.isTerminated()) {
Thread.sleep(5000);
}
for (Scanner scanner : scanners) {
scanner.close();
}
File model0 = new File("./models/model0.nn");
File model1 = new File("./models/model0.nn");
File model2 = new File("./models/model0.nn");
File model3 = new File("./models/model0.nn");
File model4 = new File("./models/model0.nn");
Assert.assertTrue(model0.exists());
Assert.assertTrue(model1.exists());
Assert.assertTrue(model2.exists());
Assert.assertTrue(model3.exists());
Assert.assertTrue(model4.exists());
File modelsTemp = new File("./modelsTmp");
FileUtils.deleteDirectory(modelsTemp);
FileUtils.deleteDirectory(models);
FileUtils.deleteDirectory(tmpDir);
}
use of ml.shifu.shifu.core.alg.SVMTrainer in project shifu by ShifuML.
the class ScorerTest method setup.
@BeforeClass
public void setup() throws IOException {
modelConfig = ModelConfig.createInitModelConfig(".", ALGORITHM.NN, ".", false);
modelConfig.getTrain().getParams().put("Propagation", "B");
modelConfig.getTrain().getParams().put("NumHiddenLayers", 2);
modelConfig.getTrain().getParams().put("LearningRate", 0.5);
List<Integer> nodes = new ArrayList<Integer>();
nodes.add(3);
nodes.add(4);
List<String> func = new ArrayList<String>();
func.add("linear");
func.add("tanh");
modelConfig.getTrain().getParams().put("NumHiddenNodes", nodes);
modelConfig.getTrain().getParams().put("ActivationFunc", func);
NNTrainer trainer = new NNTrainer(modelConfig, 0, false);
double[] input = { 0., 0. };
double[] ideal = { 1. };
MLDataPair pair = new BasicMLDataPair(new BasicMLData(input), new BasicMLData(ideal));
set.add(pair);
input = new double[] { 0., 1. };
ideal = new double[] { 0. };
pair = new BasicMLDataPair(new BasicMLData(input), new BasicMLData(ideal));
set.add(pair);
input = new double[] { 1., 0. };
ideal = new double[] { 0. };
pair = new BasicMLDataPair(new BasicMLData(input), new BasicMLData(ideal));
set.add(pair);
input = new double[] { 1., 1. };
ideal = new double[] { 1. };
pair = new BasicMLDataPair(new BasicMLData(input), new BasicMLData(ideal));
set.add(pair);
trainer.setTrainSet(set);
trainer.setValidSet(set);
trainer.train();
modelConfig.getTrain().setAlgorithm("SVM");
modelConfig.getTrain().getParams().put("Kernel", "rbf");
modelConfig.getTrain().getParams().put("Const", 0.1);
modelConfig.getTrain().getParams().put("Gamma", 1.0);
modelConfig.getVarSelect().setFilterNum(2);
SVMTrainer svm = new SVMTrainer(modelConfig, 1, false);
svm.setTrainSet(set);
svm.setValidSet(set);
svm.train();
models.add(trainer.getNetwork());
models.add(svm.getSVM());
}
Aggregations