|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +from pyspark.ml.classification import MultilayerPerceptronClassificationModel |
| 4 | + |
| 5 | +from ...common._registration import register_converter, register_shape_calculator |
| 6 | +from ...common.data_types import Int64TensorType, FloatTensorType |
| 7 | +from ...common.utils import check_input_and_output_numbers, check_input_and_output_types |
| 8 | +from ...common._topology import Operator, Scope, ModelComponentContainer |
| 9 | +from ....proto import onnx_proto |
| 10 | +from typing import List |
| 11 | +import numpy as np |
| 12 | + |
| 13 | + |
| 14 | +def convert_sparkml_mlp_classifier(scope: Scope, operator: Operator, container: ModelComponentContainer): |
| 15 | + op: MultilayerPerceptronClassificationModel = operator.raw_operator |
| 16 | + layers: List[int] = op.getLayers() |
| 17 | + weights: np.ndarray = op.weights.toArray() |
| 18 | + |
| 19 | + offset = 0 |
| 20 | + |
| 21 | + input: str |
| 22 | + for i in range(len(layers) - 1): |
| 23 | + weight_matrix = weights[offset : offset + layers[i] * layers[i + 1]].reshape([layers[i], layers[i + 1]]) |
| 24 | + offset += layers[i] * layers[i + 1] |
| 25 | + bias_vector = weights[offset : offset + layers[i + 1]] |
| 26 | + offset += layers[i + 1] |
| 27 | + |
| 28 | + if i == 0: |
| 29 | + input = operator.inputs[0].full_name |
| 30 | + |
| 31 | + weight_variable = scope.get_unique_variable_name("w") |
| 32 | + container.add_initializer( |
| 33 | + weight_variable, |
| 34 | + onnx_proto.TensorProto.FLOAT, |
| 35 | + weight_matrix.shape, |
| 36 | + weight_matrix.flatten().astype(np.float32), |
| 37 | + ) |
| 38 | + |
| 39 | + bias_variable = scope.get_unique_variable_name("b") |
| 40 | + container.add_initializer( |
| 41 | + bias_variable, onnx_proto.TensorProto.FLOAT, bias_vector.shape, bias_vector.astype(np.float32), |
| 42 | + ) |
| 43 | + |
| 44 | + gemm_output_variable = scope.get_unique_variable_name("gemm_output") |
| 45 | + container.add_node( |
| 46 | + op_type="Gemm", |
| 47 | + inputs=[input, weight_variable, bias_variable], |
| 48 | + outputs=[gemm_output_variable], |
| 49 | + op_version=7, |
| 50 | + name=scope.get_unique_operator_name("Gemm"), |
| 51 | + ) |
| 52 | + |
| 53 | + if i == len(layers) - 2: |
| 54 | + container.add_node( |
| 55 | + op_type="Softmax", |
| 56 | + inputs=[gemm_output_variable], |
| 57 | + outputs=[operator.outputs[1].full_name], |
| 58 | + op_version=1, |
| 59 | + name=scope.get_unique_operator_name("Softmax"), |
| 60 | + ) |
| 61 | + else: |
| 62 | + input = scope.get_unique_variable_name("activation_output") |
| 63 | + container.add_node( |
| 64 | + op_type="Sigmoid", |
| 65 | + inputs=[gemm_output_variable], |
| 66 | + outputs=[input], |
| 67 | + op_version=1, |
| 68 | + name=scope.get_unique_operator_name("Sigmoid"), |
| 69 | + ) |
| 70 | + |
| 71 | + container.add_node( |
| 72 | + "ArgMax", |
| 73 | + [operator.outputs[1].full_name], |
| 74 | + [operator.outputs[0].full_name], |
| 75 | + name=scope.get_unique_operator_name("ArgMax"), |
| 76 | + axis=1, |
| 77 | + keepdims = 0, |
| 78 | + ) |
| 79 | + |
| 80 | + |
| 81 | +register_converter("pyspark.ml.classification.MultilayerPerceptronClassificationModel", convert_sparkml_mlp_classifier) |
| 82 | + |
| 83 | + |
| 84 | +def calculate_mlp_classifier_output_shapes(operator: Operator): |
| 85 | + op: MultilayerPerceptronClassificationModel = operator.raw_operator |
| 86 | + |
| 87 | + check_input_and_output_numbers(operator, input_count_range=1, output_count_range=[1, 2]) |
| 88 | + check_input_and_output_types(operator, good_input_types=[FloatTensorType, Int64TensorType]) |
| 89 | + |
| 90 | + if len(operator.inputs[0].type.shape) != 2: |
| 91 | + raise RuntimeError("Input must be a [N, C]-tensor") |
| 92 | + |
| 93 | + N = operator.inputs[0].type.shape[0] |
| 94 | + operator.outputs[0].type = Int64TensorType(shape=[N]) |
| 95 | + class_count = op.numClasses |
| 96 | + operator.outputs[1].type = FloatTensorType([N, class_count]) |
| 97 | + |
| 98 | + |
| 99 | +register_shape_calculator( |
| 100 | + "pyspark.ml.classification.MultilayerPerceptronClassificationModel", calculate_mlp_classifier_output_shapes |
| 101 | +) |
0 commit comments