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

功能介绍

与 KerasSequential分类训练组件对应的预测组件。

参数说明

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

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

| inferBatchSize | 推理数据批大小 | 推理数据批大小 | Integer | | | 256 |

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

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

代码示例

以下代码仅用于示意,可能需要修改部分代码或者配置环境后才能正常运行!

Python 代码

  1. source = CsvSourceBatchOp() \
  2. .setFilePath("https://alink-release.oss-cn-beijing.aliyuncs.com/data-files/random_tensor.csv") \
  3. .setSchemaStr("tensor string, label int")
  4. source = ToTensorBatchOp() \
  5. .setSelectedCol("tensor") \
  6. .setTensorDataType("DOUBLE") \
  7. .setTensorShape([200, 3]) \
  8. .linkFrom(source)
  9. trainBatchOp = KerasSequentialClassifierTrainBatchOp() \
  10. .setTensorCol("tensor") \
  11. .setLabelCol("label") \
  12. .setLayers([
  13. "Conv1D(256, 5, padding='same', activation='relu')",
  14. "Conv1D(128, 5, padding='same', activation='relu')",
  15. "Dropout(0.1)",
  16. "MaxPooling1D(pool_size=8)",
  17. "Conv1D(128, 5, padding='same', activation='relu')",
  18. "Conv1D(128, 5, padding='same', activation='relu')",
  19. "Flatten()"
  20. ]) \
  21. .setOptimizer("Adam()") \
  22. .setNumEpochs(1) \
  23. .linkFrom(source)
  24. predictBatchOp = KerasSequentialClassifierPredictBatchOp() \
  25. .setPredictionCol("pred") \
  26. .setPredictionDetailCol("pred_detail") \
  27. .setReservedCols(["label"]) \
  28. .linkFrom(trainBatchOp, source)
  29. predictBatchOp.lazyPrint(10)
  30. BatchOperator.execute()

Java 代码

  1. import com.alibaba.alink.operator.batch.BatchOperator;
  2. import com.alibaba.alink.operator.batch.classification.KerasSequentialClassifierPredictBatchOp;
  3. import com.alibaba.alink.operator.batch.classification.KerasSequentialClassifierTrainBatchOp;
  4. import com.alibaba.alink.operator.batch.dataproc.ToTensorBatchOp;
  5. import com.alibaba.alink.operator.batch.source.CsvSourceBatchOp;
  6. import org.junit.Test;
  7. public class KerasSequentialClassifierPredictBatchOpTest {
  8. @Test
  9. public void testKerasSequentialClassifierPredictBatchOp() throws Exception {
  10. BatchOperator<?> source = new CsvSourceBatchOp()
  11. .setFilePath("https://alink-release.oss-cn-beijing.aliyuncs.com/data-files/random_tensor.csv")
  12. .setSchemaStr("tensor string, label int");
  13. source = new ToTensorBatchOp()
  14. .setSelectedCol("tensor")
  15. .setTensorDataType("DOUBLE")
  16. .setTensorShape(200, 3)
  17. .linkFrom(source);
  18. KerasSequentialClassifierTrainBatchOp trainBatchOp = new KerasSequentialClassifierTrainBatchOp()
  19. .setTensorCol("tensor")
  20. .setLabelCol("label")
  21. .setLayers(new String[] {
  22. "Conv1D(256, 5, padding='same', activation='relu')",
  23. "Conv1D(128, 5, padding='same', activation='relu')",
  24. "Dropout(0.1)",
  25. "MaxPooling1D(pool_size=8)",
  26. "Conv1D(128, 5, padding='same', activation='relu')",
  27. "Conv1D(128, 5, padding='same', activation='relu')",
  28. "Flatten()"
  29. })
  30. .setOptimizer("Adam()")
  31. .setNumEpochs(1)
  32. .linkFrom(source);
  33. KerasSequentialClassifierPredictBatchOp predictBatchOp = new KerasSequentialClassifierPredictBatchOp()
  34. .setPredictionCol("pred")
  35. .setPredictionDetailCol("pred_detail")
  36. .setReservedCols("label")
  37. .linkFrom(trainBatchOp, source);
  38. predictBatchOp.lazyPrint(10);
  39. BatchOperator.execute();
  40. }
  41. }

运行结果

| label | pred | pred_detail | | —- | —- | —- |

| 0 | 0 | {“0”:0.636155836712713,”1”:0.36384416328728697} |

| 1 | 0 | {“0”:0.6334926095655181,”1”:0.3665073904344819} |

| 1 | 0 | {“0”:0.6381823204965642,”1”:0.3618176795034358} |

| 1 | 0 | {“0”:0.6376416296248051,”1”:0.362358370375195} |

| 1 | 0 | {“0”:0.6345856985385896,”1”:0.36541430146141035} |

| 1 | 0 | {“0”:0.6357593109428179,”1”:0.364240689057182} |

| 0 | 0 | {“0”:0.6404387449594703,”1”:0.3595612550405296} |

| 1 | 0 | {“0”:0.6372702905549685,”1”:0.36272970944503136} |

| 0 | 0 | {“0”:0.635502012172225,”1”:0.36449798782777487} |

| 0 | 0 | {“0”:0.6262401788033837,”1”:0.37375982119661644} |