ONNXPredict
Description
The td_onnx_predict_sqle()
function is used to score data in Vantage with a model that has
been created outside Vantage and exported to Vantage using ONNX format.
For classical machine learning models on structured data, Vantage has a large set
of transformation functions in both the Vantage Analytics Library and Analytics Database
Analytic functions. User can use these functions to prepare the input data that the
classical machine learning models expect. However, there are no transformation or
conversion functions in Vantage to prepare tensors for unstructured data (text,
images, video and audio) for ONNX models. The data must be preprocessed before
loading to Vantage to conform the tensors into a shape that the ONNX models expect.
As long as the data is in the form expected by your ONNX model, it can be scored by
td_onnx_predict_sqle()
.
td_onnx_predict_sqle()
supports models in ONNX format. Several training frameworks support
native export functionality to ONNX, such as Chainer, Caffee2, and PyTorch.
User can also convert models from several toolkits like scikit-learn, TensorFlow,
Keras, XGBoost, H2O, and Spark ML to ONNX.
Usage
td_onnx_predict_sqle (
newdata = NULL,
modeldata = NULL,
accumulate = NULL,
model.output.fields = NULL,
overwrite.cached.models = "false",
show.model.input.fields.map = FALSE,
model.input.fields.map = NULL,
is.debug = FALSE,
...
)
Arguments
newdata |
Required Argument. |
modeldata |
Required Argument. |
accumulate |
Required Argument. |
model.output.fields |
Optional Argument. |
overwrite.cached.models |
Optional Argument. |
show.model.input.fields.map |
Optional Argument. |
model.input.fields.map |
Optional Argument. |
is.debug |
Optional Argument.
Default Value: FALSE |
... |
Specifies the generic keyword arguments SQLE functions accept. Below
are the generic keyword arguments: volatile: Function allows the user to partition, hash, order or local order the input data. These generic arguments are available for each argument that accepts tbl_teradata as input and can be accessed as:
Note: |
Value
Function returns an object of class "td_onnx_predict_sqle"
which is a named list containing object of class "tbl_teradata".
Named list member(s) can be referenced directly with the "$" operator
using the name(s):result
Examples
# Get the current context/connection..
con <- td_get_context()$connection
# Load the example data.
loadExampleData("pmmlpredict_example", "iris_test")
# Create tbl_teradata object.
iris_test <- tbl(con, "iris_test")
# Set install location of BYOM functions.
options(byom.install.location = "mldb")
# Check the list of available analytic functions.
display_analytic_functions(type="BYOM")
# Create following table on Vantage if it does not exist.
crt_tbl <- "CREATE SET TABLE byom_models(model_id VARCHAR(40), model BLOB)
PRIMARY INDEX (model_id);"
DBI::dbExecute(con, sql(crt_tbl))
# Run the following query through BTEQ or Teradata Studio to load the
# models. 'load_byom_model.txt' and byom files can be found under
# 'inst/scripts' in tdplyr installation directory. This file and the byom
# models to be loaded should be in the same directory.
# .import vartext file load_byom_model.txt
# .repeat *
# USING (c1 VARCHAR(40), c2 BLOB AS DEFERRED BY NAME) INSERT INTO byom_models(:c1, :c2);
# Retrieve ONNX model.
# The 'iris_db_dt_model_sklearn' created with each input variable mapped
# to a single input tensor, then converted this model into ONNX format
# with scikit-learn-onnx, and then used to predict the flower species.
# This model trained using iris_test dataset with scikit-learn.
skl_model <- tbl(con, "byom_models")
filter(model_id=='iris_db_dt_model_sklearn')
# The 'iris_db_dt_model_sklearn_floattensor' created by using an input array of
# four float32 values and named float_input, then converted this model into ONNX
# format with scikit-learn-onnx, and then used to predict the flower species.
# This model trained using iris_test dataset with scikit-learn.
skl_floattensor_model <- tbl(con, "byom_models")
filter(model_id=='iris_db_dt_model_sklearn_floattensor')
# Example 1: Example performs prediction with td_onnx_predict_sqle function
# using trained 'skl_model' model in onnx format generated
# outside of Vantage.
ONNXPredict_out <- td_onnx_predict_sqle(accumulate="id",
newdata=iris_test,
modeldata=skl_model)
# Print the results.
print(ONNXPredict_out$result)
# Example 2: Example performs prediction with td_onnx_predict_sqle function using trained
# 'skl_floattensor_model' model in onnx format generated
# outside of Vantage, where input tbl_teradata columns match the order
# used when generating the model, by specifying "model.input.fields.map"
# to define the columns.
ONNXPredict_out1 <- td_onnx_predict_sqle(
accumulate="id",
model.output.fields="output_probability",
overwrite.cached.models="*",
model.input.fields.map='float_input=sepal_length,
sepal_width, petal_length,
petal_width',
newdata=iris_test,
modeldata=skl_floattensor_model)
# Print the result.
print(ONNXPredict_out1$result)
# Example 3: Example to show case the trace table usage using
# is.debug=TRUE.
# Create the trace table.
crt_tbl_query <- 'CREATE GLOBAL TEMPORARY TRACE TABLE BYOM_Trace \
(vproc_ID BYTE(2) \
,Sequence INTEGER \
,Trace_Output VARCHAR(31000)
CHARACTER SET LATIN NOT CASESPECIFIC) \
ON COMMIT PRESERVE ROWS;'
DBI::dbExecute(con, sql(crt_tbl_query))
# Turn on tracing for the session.
dbExecute(con, "SET SESSION FUNCTION TRACE USING '' FOR TABLE BYOM_Trace;")
# Execute the td_onnx_predict_sqle() function using is.debug=TRUE.
ONNXPredict_out2 <- td_onnx_predict_sqle(
accumulate="id",
newdata=iris_test,
modeldata=skl_model,
is.debug=TRUE)
# Print the results.
print(ONNXPredict_out2$result)
# View the trace table information.
trace_df <- dbGetQuery(con, "select * from BYOM_Trace")
print(trace_df)
# Turn off tracing for the session.
dbExecute(con, "SET SESSION FUNCTION TRACE OFF;")