Java 类名:com.alibaba.alink.operator.batch.classification.MultilayerPerceptronPredictBatchOp
Python 类名:MultilayerPerceptronPredictBatchOp

功能介绍

多层感知机(MLP,Multilayer Perceptron)也被称作人工神经网络(ANN,Artificial Neural Network),经常用来进行多分类问题的训练预测。

算法原理

多层感知机算法除了输入输出层外,它中间可以有多个隐层,最简单的MLP只含一个隐层,即三层的结构,如下图:
多层感知机分类预测 (MultilayerPerceptronPredictBatchOp) - 图1
从上图可以看到,多层感知机层与层之间是全连接的。多层感知机最左边是输入层,中间是隐藏层,最后是输出层。 其中输出层对应的是各个分类标签,输出层
的每一个节点对应每一个标签的出现的概率。

算法使用

多层感知机主要用于多分类问题,类似文字识别,语音识别,文本分析等问题。

文献

[1]Artificial neural networks (the multilayer perceptron)—a review of applications in the atmospheric sciences
MW Gardner, SR Dorling - Atmospheric environment, 1998 - Elsevier.

参数说明

| 名称 | 中文名称 | 描述 | 类型 | 是否必须? | 取值范围 | 默认值 | | —- | —- | —- | —- | —- | —- | —- |

| predictionCol | 预测结果列名 | 预测结果列名 | String | ✓ | | |

| modelFilePath | 模型的文件路径 | 模型的文件路径 | String | | | null |

| predictionDetailCol | 预测详细信息列名 | 预测详细信息列名 | String | | | |

| reservedCols | 算法保留列名 | 算法保留列 | String[] | | | null |

| vectorCol | 向量列名 | 向量列对应的列名,默认值是null | String | | 所选列类型为 [DENSE_VECTOR, SPARSE_VECTOR, STRING, VECTOR] | null |

| numThreads | 组件多线程线程个数 | 组件多线程线程个数 | Integer | | | 1 |

代码示例

Python 代码

  1. from pyalink.alink import *
  2. import pandas as pd
  3. useLocalEnv(1)
  4. df = pd.DataFrame([
  5. [5,2,3.5,1,'Iris-versicolor'],
  6. [5.1,3.7,1.5,0.4,'Iris-setosa'],
  7. [6.4,2.8,5.6,2.2,'Iris-virginica'],
  8. [6,2.9,4.5,1.5,'Iris-versicolor'],
  9. [4.9,3,1.4,0.2,'Iris-setosa'],
  10. [5.7,2.6,3.5,1,'Iris-versicolor'],
  11. [4.6,3.6,1,0.2,'Iris-setosa'],
  12. [5.9,3,4.2,1.5,'Iris-versicolor'],
  13. [6.3,2.8,5.1,1.5,'Iris-virginica'],
  14. [4.7,3.2,1.3,0.2,'Iris-setosa'],
  15. [5.1,3.3,1.7,0.5,'Iris-setosa'],
  16. [5.5,2.4,3.8,1.1,'Iris-versicolor'],
  17. ])
  18. data = BatchOperator.fromDataframe(df, schemaStr='sepal_length double, sepal_width double, petal_length double, petal_width double, category string')
  19. mlpc = MultilayerPerceptronTrainBatchOp() \
  20. .setFeatureCols(["sepal_length", "sepal_width", "petal_length", "petal_width"]) \
  21. .setLabelCol("category") \
  22. .setLayers([4, 8, 3]) \
  23. .setMaxIter(10)
  24. model = mlpc.linkFrom(data)
  25. predictor = MultilayerPerceptronPredictBatchOp()\
  26. .setPredictionCol('p')
  27. predictor.linkFrom(model, data).print()

Java 代码

  1. import org.apache.flink.types.Row;
  2. import com.alibaba.alink.operator.batch.BatchOperator;
  3. import com.alibaba.alink.operator.batch.classification.MultilayerPerceptronPredictBatchOp;
  4. import com.alibaba.alink.operator.batch.classification.MultilayerPerceptronTrainBatchOp;
  5. import com.alibaba.alink.operator.batch.source.MemSourceBatchOp;
  6. import org.junit.Test;
  7. import java.util.Arrays;
  8. import java.util.List;
  9. public class MultilayerPerceptronPredictBatchOpTest {
  10. @Test
  11. public void testMultilayerPerceptronPredictBatchOp() throws Exception {
  12. List <Row> df = Arrays.asList(
  13. Row.of(5.0, 2.0, 3.5, 1.0, "Iris-versicolor"),
  14. Row.of(5.1, 3.7, 1.5, 0.4, "Iris-setosa"),
  15. Row.of(6.4, 2.8, 5.6, 2.2, "Iris-virginica"),
  16. Row.of(6.0, 2.9, 4.5, 1.5, "Iris-versicolor"),
  17. Row.of(4.9, 3.0, 1.4, 0.2, "Iris-setosa"),
  18. Row.of(5.7, 2.6, 3.5, 1.0, "Iris-versicolor"),
  19. Row.of(4.6, 3.6, 1.0, 0.2, "Iris-setosa"),
  20. Row.of(5.9, 3.0, 4.2, 1.5, "Iris-versicolor"),
  21. Row.of(6.3, 2.8, 5.1, 1.5, "Iris-virginica"),
  22. Row.of(4.7, 3.2, 1.3, 0.2, "Iris-setosa"),
  23. Row.of(5.1, 3.3, 1.7, 0.5, "Iris-setosa"),
  24. Row.of(5.5, 2.4, 3.8, 1.1, "Iris-versicolor")
  25. );
  26. BatchOperator <?> data = new MemSourceBatchOp(df,
  27. "sepal_length double, sepal_width double, petal_length double, petal_width double, category string");
  28. BatchOperator <?> mlpc = new MultilayerPerceptronTrainBatchOp()
  29. .setFeatureCols("sepal_length", "sepal_width", "petal_length", "petal_width")
  30. .setLabelCol("category")
  31. .setLayers(new int[] {4, 8, 3})
  32. .setMaxIter(10);
  33. BatchOperator model = mlpc.linkFrom(data);
  34. BatchOperator <?> predictor = new MultilayerPerceptronPredictBatchOp()
  35. .setPredictionCol("p");
  36. predictor.linkFrom(model, data).print();
  37. }
  38. }

运行结果

| sepal_length | sepal_width | petal_length | petal_width | category | p | | —- | —- | —- | —- | —- | —- |

| 5.0000 | 2.0000 | 3.5000 | 1.0000 | Iris-versicolor | Iris-versicolor |

| 5.1000 | 3.7000 | 1.5000 | 0.4000 | Iris-setosa | Iris-versicolor |

| 6.4000 | 2.8000 | 5.6000 | 2.2000 | Iris-virginica | Iris-versicolor |

| 6.0000 | 2.9000 | 4.5000 | 1.5000 | Iris-versicolor | Iris-versicolor |

| 4.9000 | 3.0000 | 1.4000 | 0.2000 | Iris-setosa | Iris-versicolor |

| 5.7000 | 2.6000 | 3.5000 | 1.0000 | Iris-versicolor | Iris-versicolor |

| 4.6000 | 3.6000 | 1.0000 | 0.2000 | Iris-setosa | Iris-setosa |

| 5.9000 | 3.0000 | 4.2000 | 1.5000 | Iris-versicolor | Iris-versicolor |

| 6.3000 | 2.8000 | 5.1000 | 1.5000 | Iris-virginica | Iris-versicolor |

| 4.7000 | 3.2000 | 1.3000 | 0.2000 | Iris-setosa | Iris-versicolor |

| 5.1000 | 3.3000 | 1.7000 | 0.5000 | Iris-setosa | Iris-versicolor |

| 5.5000 | 2.4000 | 3.8000 | 1.1000 | Iris-versicolor | Iris-versicolor |