Java 类名:com.alibaba.alink.operator.stream.classification.MultilayerPerceptronPredictStreamOp
Python 类名:MultilayerPerceptronPredictStreamOp

功能介绍

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

算法原理

多层感知机算法除了输入输出层外,它中间可以有多个隐层,最简单的MLP只含一个隐层,即三层的结构,如下图:
多层感知机分类预测 (MultilayerPerceptronPredictStreamOp) - 图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 |

| modelStreamFilePath | 模型流的文件路径 | 模型流的文件路径 | String | | | null |

| modelStreamScanInterval | 扫描模型路径的时间间隔 | 描模型路径的时间间隔,单位秒 | Integer | | | 10 |

| modelStreamStartTime | 模型流的起始时间 | 模型流的起始时间。默认从当前时刻开始读。使用yyyy-mm-dd hh:mm:ss.fffffffff格式,详见Timestamp.valueOf(String s) | String | | | null |

代码示例

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. train_data = BatchOperator.fromDataframe(df, schemaStr='sepal_length double, sepal_width double, petal_length double, petal_width double, category string')
  19. test_data = StreamOperator.fromDataframe(df, schemaStr='sepal_length double, sepal_width double, petal_length double, petal_width double, category string')
  20. mlpc = MultilayerPerceptronTrainBatchOp() \
  21. .setFeatureCols(["sepal_length", "sepal_width", "petal_length", "petal_width"]) \
  22. .setLabelCol("category") \
  23. .setLayers([4, 5, 3]) \
  24. .setMaxIter(10)
  25. model = mlpc.linkFrom(train_data)
  26. predictor = MultilayerPerceptronPredictStreamOp(model)\
  27. .setPredictionCol('p')
  28. predictor.linkFrom(test_data).print()
  29. StreamOperator.execute()

Java 代码

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

运行结果

| 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 |