MLOnnx

pipeline pipeline

Exports a traditional machine learning model (i.e. scikit-learn) to ONNX.

Example

See the link below for a detailed example.

NotebookDescription
Export and run other machine learning modelsExport and run models from scikit-learn, PyTorch and moreOpen In Colab

Methods

Python documentation for the pipeline.

__call__(model, task='default', output=None, opset=12)

Exports a machine learning model to ONNX using ONNXMLTools.

Parameters:

NameTypeDescriptionDefault
model

model to export

required
task

optional model task or category

‘default’
output

optional output model path, defaults to return byte array if None

None
opset

onnx opset, defaults to 12

12

Returns:

TypeDescription

path to model output or model as bytes depending on output parameter

Source code in txtai/pipeline/train/mlonnx.py

  1. 31
  2. 32
  3. 33
  4. 34
  5. 35
  6. 36
  7. 37
  8. 38
  9. 39
  10. 40
  11. 41
  12. 42
  13. 43
  14. 44
  15. 45
  16. 46
  17. 47
  18. 48
  19. 49
  20. 50
  21. 51
  22. 52
  23. 53
  24. 54
  25. 55
  26. 56
  27. 57
  28. 58
  29. 59
  30. 60
  31. 61
  32. 62
  33. 63
  1. def call(self, model, task=”default”, output=None, opset=12):
  2. “””
  3. Exports a machine learning model to ONNX using ONNXMLTools.
  4. Args:
  5. model: model to export
  6. task: optional model task or category
  7. output: optional output model path, defaults to return byte array if None
  8. opset: onnx opset, defaults to 12
  9. Returns:
  10. path to model output or model as bytes depending on output parameter
  11. “””
  12. # Convert scikit-learn model to ONNX
  13. model = convertsklearn(model, task, initial_types=[(“input_ids”, StringTensorType([None, None]))], target_opset=opset)
  14. # Prune model graph down to only output probabilities
  15. model = select_model_inputs_outputs(model, outputs=”probabilities”)
  16. # pylint: disable=E1101
  17. # Rename output to logits for consistency with other models
  18. model.graph.output[0].name = logits
  19. # Find probabilities output node and rename to logits
  20. for node in model.graph.node:
  21. for x, in enumerate(node.output):
  22. if node.output[x] == probabilities”:
  23. node.output[x] = logits
  24. # Save model to specified output path or return bytes
  25. model = save_onnx_model(model, output)
  26. return output if output else model