use of com.alibaba.alink.pipeline.classification.MultilayerPerceptronClassifier in project Alink by alibaba.
the class Chap12 method c_6.
static void c_6() throws Exception {
AkSourceBatchOp train_data = new AkSourceBatchOp().setFilePath(DATA_DIR + TRAIN_FILE);
AkSourceBatchOp test_data = new AkSourceBatchOp().setFilePath(DATA_DIR + TEST_FILE);
new MultilayerPerceptronClassifier().setLayers(new int[] { 4, 12, 3 }).setFeatureCols(FEATURE_COL_NAMES).setLabelCol(LABEL_COL_NAME).setPredictionCol(PREDICTION_COL_NAME).fit(train_data).transform(test_data).link(new EvalMultiClassBatchOp().setLabelCol(LABEL_COL_NAME).setPredictionCol(PREDICTION_COL_NAME).lazyPrintMetrics("MultilayerPerceptronClassifier [4, 12, 3]"));
new MultilayerPerceptronClassifier().setLayers(new int[] { 4, 3 }).setFeatureCols(FEATURE_COL_NAMES).setLabelCol(LABEL_COL_NAME).setPredictionCol(PREDICTION_COL_NAME).fit(train_data).transform(test_data).link(new EvalMultiClassBatchOp().setLabelCol(LABEL_COL_NAME).setPredictionCol(PREDICTION_COL_NAME).lazyPrintMetrics("MultilayerPerceptronClassifier [4, 3]"));
BatchOperator.execute();
}
use of com.alibaba.alink.pipeline.classification.MultilayerPerceptronClassifier in project Alink by alibaba.
the class Chap13 method c_4.
static void c_4() throws Exception {
BatchOperator.setParallelism(4);
AkSourceBatchOp train_data = new AkSourceBatchOp().setFilePath(DATA_DIR + SPARSE_TRAIN_FILE);
AkSourceBatchOp test_data = new AkSourceBatchOp().setFilePath(DATA_DIR + SPARSE_TEST_FILE);
new MultilayerPerceptronClassifier().setLayers(new int[] { 784, 10 }).setVectorCol(VECTOR_COL_NAME).setLabelCol(LABEL_COL_NAME).setPredictionCol(PREDICTION_COL_NAME).fit(train_data).transform(test_data).link(new EvalMultiClassBatchOp().setLabelCol(LABEL_COL_NAME).setPredictionCol(PREDICTION_COL_NAME).lazyPrintMetrics("MultilayerPerceptronClassifier {784, 10}"));
BatchOperator.execute();
new MultilayerPerceptronClassifier().setLayers(new int[] { 784, 256, 128, 10 }).setVectorCol(VECTOR_COL_NAME).setLabelCol(LABEL_COL_NAME).setPredictionCol(PREDICTION_COL_NAME).fit(train_data).transform(test_data).link(new EvalMultiClassBatchOp().setLabelCol(LABEL_COL_NAME).setPredictionCol(PREDICTION_COL_NAME).lazyPrintMetrics("MultilayerPerceptronClassifier {784, 256, 128, 10}"));
BatchOperator.execute();
}
use of com.alibaba.alink.pipeline.classification.MultilayerPerceptronClassifier in project Alink by alibaba.
the class ModelSaveAndLoadTest method testSaveLoadSave.
@Test
public void testSaveLoadSave() throws Exception {
VectorAssembler va = new VectorAssembler().setSelectedCols(Iris.getFeatureColNames()).setOutputCol("features");
MultilayerPerceptronClassifier classifier = new MultilayerPerceptronClassifier().setVectorCol("features").setLabelCol(Iris.getLabelColName()).setLayers(new int[] { 4, 5, 3 }).setMaxIter(30).setPredictionCol("pred_label").setPredictionDetailCol("pred_detail").setReservedCols(Iris.getLabelColName());
Pipeline pipeline = new Pipeline().add(va).add(classifier);
PipelineModel model = PipelineModel.collectLoad(pipeline.fit(data).save());
LocalPredictor localPredictor = model.collectLocalPredictor(data.getSchema());
Row pred = localPredictor.map(Row.of(4.8, 3.4, 1.9, 0.2, "Iris-setosa"));
Assert.assertEquals(pred.getArity(), 3);
}
use of com.alibaba.alink.pipeline.classification.MultilayerPerceptronClassifier in project Alink by alibaba.
the class PipelineSaveAndLoadTest method testSaveLoadSave.
@Test
public void testSaveLoadSave() throws Exception {
VectorAssembler va = new VectorAssembler().setSelectedCols(Iris.getFeatureColNames()).setOutputCol("features");
MultilayerPerceptronClassifier classifier = new MultilayerPerceptronClassifier().setVectorCol("features").setLabelCol(Iris.getLabelColName()).setLayers(new int[] { 4, 5, 3 }).setMaxIter(30).setPredictionCol("pred_label").setPredictionDetailCol("pred_detail").setReservedCols(Iris.getLabelColName());
Pipeline pipeline = new Pipeline().add(va).add(classifier);
Pipeline pipeline1 = Pipeline.collectLoad(pipeline.save());
PipelineModel model = PipelineModel.collectLoad(pipeline1.fit(data).save());
LocalPredictor localPredictor = model.collectLocalPredictor(data.getSchema());
Row pred = localPredictor.map(Row.of(4.8, 3.4, 1.9, 0.2, "Iris-setosa"));
Assert.assertEquals(pred.getArity(), 3);
}
use of com.alibaba.alink.pipeline.classification.MultilayerPerceptronClassifier in project Alink by alibaba.
the class PipelineSaveAndLoadTest method testNewSave.
@Test
public void testNewSave() throws Exception {
VectorAssembler va = new VectorAssembler().setSelectedCols(Iris.getFeatureColNames()).setOutputCol("features");
MultilayerPerceptronClassifier classifier = new MultilayerPerceptronClassifier().setVectorCol("features").setLabelCol(Iris.getLabelColName()).setLayers(new int[] { 4, 5, 3 }).setMaxIter(30).setPredictionCol("pred_label").setPredictionDetailCol("pred_detail").setReservedCols(Iris.getLabelColName());
Pipeline pipeline = new Pipeline().add(va).add(classifier);
PipelineModel model = pipeline.fit(data);
BatchOperator<?> saved = model.save();
PipelineModel modelLoaded = PipelineModel.collectLoad(saved);
Assert.assertEquals(modelLoaded.transform(Iris.getBatchData()).count(), 150);
}
Aggregations