Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

Add example for OnnxRunner. #422

Merged
merged 1 commit into from
Jan 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/python/nimbusml.pyproj
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
<Compile Include="nimbusml\examples\BootStrapSample.py" />
<Compile Include="nimbusml\examples\CharTokenizer.py" />
<Compile Include="nimbusml\examples\ColumnConcatenator.py" />
<Compile Include="nimbusml\examples\examples_from_dataframe\OnnxRunner_df.py" />
<Compile Include="nimbusml\examples\examples_from_dataframe\RobustScaler_df.py" />
<Compile Include="nimbusml\examples\examples_from_dataframe\TimeSeriesImputer_df.py" />
<Compile Include="nimbusml\examples\examples_from_dataframe\VotingRegressor.py" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import os
import tempfile
import numpy as np
import pandas as pd
from nimbusml import Pipeline
from nimbusml.preprocessing import OnnxRunner
from nimbusml.preprocessing.normalization import MinMaxScaler


def get_tmp_file(suffix=None):
fd, file_name = tempfile.mkstemp(suffix=suffix)
fl = os.fdopen(fd, 'w')
fl.close()
return file_name

# Generate the train and test data
np.random.seed(0)
x = np.arange(100, step=0.1)
y = x * 10 + (np.random.standard_normal(len(x)) * 10)
train_data = {'c1': x, 'c2': y}
train_df = pd.DataFrame(train_data).astype({'c1': np.float32, 'c2': np.float32})

test_data = {'c1': [2.5, 30.5], 'c2': [1, 1]}
test_df = pd.DataFrame(test_data).astype({'c1': np.float32, 'c2': np.float32})

# Fit a MinMaxScaler Pipeline
r1 = Pipeline([MinMaxScaler()])
r1.fit(train_df)

# Export the pipeline to ONNX
onnx_path = get_tmp_file('.onnx')
r1.export_to_onnx(onnx_path, 'com.microsoft.ml', onnx_version='Stable')

# Perform the transform using the standard ML.Net backend
result_standard = r1.transform(test_df)
print(result_standard)
# c1 c2
# 0 0.025025 0.000998
# 1 0.305305 0.000998

# Perform the transform using the ONNX backend.
# Note, the extra columns and column name differences
# is a known issue with the ML.Net backend.
onnxrunner = OnnxRunner(model_file=onnx_path)
result_onnx = onnxrunner.fit_transform(test_df)
print(result_onnx)
# c1 c2 c12.0 c22.0
# 0 2.5 1.0 0.025025 0.000998
# 1 30.5 1.0 0.305305 0.000998