|
| 1 | +--- |
| 2 | +title: Native scoring with T-SQL PREDICT |
| 3 | +titleSuffix: SQL machine learning |
| 4 | +description: Learn how to use native scoring with the PREDICT T-SQL function to generate prediction values for new data inputs in near-real-time. |
| 5 | +ms.prod: sql |
| 6 | +ms.technology: machine-learning |
| 7 | +ms.date: 06/26/2020 |
| 8 | +ms.topic: how-to |
| 9 | +author: dphansen |
| 10 | +ms.author: davidph |
| 11 | +ms.custom: seo-lt-2019 |
| 12 | +monikerRange: ">=sql-server-2017||=azuresqldb-current||>=sql-server-linux-2017||=azuresqldb-mi-current||=azure-sqldw-latest||=sqlallproducts-allversions" |
| 13 | +--- |
| 14 | + |
| 15 | +# Native scoring using the PREDICT T-SQL function with SQL machine learning |
| 16 | +[!INCLUDE[tsql-appliesto-ss2017-asdb-asdbmi-asdw-xxx-md](../../includes/tsql-appliesto-ss2017-asdb-asdbmi-asdw-xxx-md.md)] |
| 17 | + |
| 18 | +Learn how to use native scoring with the [PREDICT T-SQL function](../../t-sql/queries/predict-transact-sql.md) to generate prediction values for new data inputs in near-real-time. Native scoring requires that you have an already-trained model. |
| 19 | + |
| 20 | +The `PREDICT` function uses the native C++ extension capabilities in [SQL machine learning](../index.yml). This methodology offers the fastest possible processing speed of forecasting and prediction workloads and support models in [Open Neural Network Exchange (ONNX)](https://onnx.ai/get-started.html) format or models trained using the [RevoScaleR](../r/ref-r-revoscaler.md) and [revoscalepy](../python/ref-py-revoscalepy.md) packages. |
| 21 | + |
| 22 | +## How native scoring works |
| 23 | + |
| 24 | +Native scoring uses libraries that can read models in ONNX or a predefined binary format, and generate scores for new data inputs that you provide. Because the model is trained, deployed, and stored, it can be used for scoring without having to call the R or Python interpreter. This means that the overhead of multiple process interactions is reduced, resulting in faster prediction performance. |
| 25 | + |
| 26 | +To use native scoring, call the `PREDICT` T-SQL function and pass the following required inputs: |
| 27 | + |
| 28 | ++ A compatible model based on a supported model and algorithm. |
| 29 | ++ Input data, typically defined as a T-SQL query. |
| 30 | + |
| 31 | +The function returns predictions for the input data, together with any columns of source data that you want to pass through. |
| 32 | + |
| 33 | +## Prerequisites |
| 34 | + |
| 35 | +`PREDICT` is available on: |
| 36 | + |
| 37 | ++ All editions of SQL Server 2017 and later on Windows and Linux |
| 38 | ++ Azure SQL Managed Instance |
| 39 | ++ Azure SQL Database |
| 40 | ++ Azure SQL Edge |
| 41 | ++ Azure Synapse Analytics |
| 42 | + |
| 43 | +The function is enabled by default. You do not need to install R or Python, or enable additional features. |
| 44 | + |
| 45 | +## Supported models |
| 46 | + |
| 47 | +The model formats supported by the `PREDICT` function depends on the SQL platform on which you perform native scoring. See the table below to see which model formats are supported on which platform. |
| 48 | + |
| 49 | +| Platform | ONNX model format | RevoScale model format | |
| 50 | +|-|-|-| |
| 51 | +| SQL Server | No | Yes | |
| 52 | +| Azure SQL Managed Instance | Yes | Yes | |
| 53 | +| Azure SQL Database | No | Yes | |
| 54 | +| Azure SQL Edge | Yes | No | |
| 55 | +| Azure Synapse Analytics | Yes | No | |
| 56 | + |
| 57 | +::: moniker range="=azuresqldb-mi-current||=azure-sqldw-latest||=sqlallproducts-allversions" |
| 58 | +### ONNX models |
| 59 | + |
| 60 | +The model must be in an [Open Neural Network Exchange (ONNX)](https://onnx.ai/get-started.html) model format. |
| 61 | +::: moniker-end |
| 62 | + |
| 63 | +::: moniker range=">=sql-server-2017||>=sql-server-linux-2017||=azuresqldb-mi-current||=azuresqldb-current||=sqlallproducts-allversions" |
| 64 | +### RevoScale models |
| 65 | + |
| 66 | +The model must be trained in advance using one of the supported **rx** algorithms listed below using the [RevoScaleR](../r/ref-r-revoscaler.md) or [revoscalepy](../python/ref-py-revoscalepy.md) package. |
| 67 | + |
| 68 | +Serialize the model using [rxSerialize](https://docs.microsoft.com/machine-learning-server/r-reference/revoscaler/rxserializemodel) for R, and [rx_serialize_model](https://docs.microsoft.com/machine-learning-server/python-reference/revoscalepy/rx-serialize-model) for Python. These serialization functions have been optimized to support fast scoring. |
| 69 | + |
| 70 | +<a name="bkmk_native_supported_algos"></a> |
| 71 | + |
| 72 | +#### Supported RevoScale algorithms |
| 73 | + |
| 74 | +The following algorithms are supported in revoscalepy and RevoScaleR. |
| 75 | + |
| 76 | ++ revoscalepy algorithms |
| 77 | + |
| 78 | + + [rx_lin_mod](https://docs.microsoft.com/machine-learning-server/python-reference/revoscalepy/rx-lin-mod) |
| 79 | + + [rx_logit](https://docs.microsoft.com/machine-learning-server/python-reference/revoscalepy/rx-logit) |
| 80 | + + [rx_btrees](https://docs.microsoft.com/machine-learning-server/python-reference/revoscalepy/rx-btrees) |
| 81 | + + [rx_dtree](https://docs.microsoft.com/machine-learning-server/python-reference/revoscalepy/rx-dtree) |
| 82 | + + [rx_dforest](https://docs.microsoft.com/machine-learning-server/python-reference/revoscalepy/rx-dforest) |
| 83 | + |
| 84 | ++ RevoScaleR algorithms |
| 85 | + |
| 86 | + + [rxLinMod](https://docs.microsoft.com/r-server/r-reference/revoscaler/rxlinmod) |
| 87 | + + [rxLogit](https://docs.microsoft.com/r-server/r-reference/revoscaler/rxlogit) |
| 88 | + + [rxBTrees](https://docs.microsoft.com/r-server/r-reference/revoscaler/rxbtrees) |
| 89 | + + [rxDtree](https://docs.microsoft.com/r-server/r-reference/revoscaler/rxdtree) |
| 90 | + + [rxDForest](https://docs.microsoft.com/r-server/r-reference/revoscaler/rxdforest) |
| 91 | + |
| 92 | +If you need to use an algorithms from MicrosoftML or microsoftml, use [real-time scoring with sp_rxPredict](../real-time-scoring.md). |
| 93 | + |
| 94 | +Unsupported model types include the following types: |
| 95 | + |
| 96 | ++ Models containing other transformations |
| 97 | ++ Models using the `rxGlm` or `rxNaiveBayes` algorithms in RevoScaleR or revoscalepy equivalents |
| 98 | ++ PMML models |
| 99 | ++ Models created using other open-source or third-party libraries |
| 100 | +::: moniker-end |
| 101 | + |
| 102 | +## Examples |
| 103 | +::: moniker range="=azuresqldb-mi-current||=azure-sqldw-latest||=sqlallproducts-allversions" |
| 104 | +### PREDICT with an ONNX model |
| 105 | + |
| 106 | +This example shows how to use an ONNX model stored in the `dbo.models` table for native scoring. |
| 107 | + |
| 108 | +```sql |
| 109 | +DECLARE @model VARBINARY(max) = ( |
| 110 | + SELECT DATA |
| 111 | + FROM dbo.models |
| 112 | + WHERE id = 1 |
| 113 | + ); |
| 114 | + |
| 115 | +WITH predict_input |
| 116 | +AS ( |
| 117 | + SELECT TOP (1000) [id] |
| 118 | + , CRIM |
| 119 | + , ZN |
| 120 | + , INDUS |
| 121 | + , CHAS |
| 122 | + , NOX |
| 123 | + , RM |
| 124 | + , AGE |
| 125 | + , DIS |
| 126 | + , RAD |
| 127 | + , TAX |
| 128 | + , PTRATIO |
| 129 | + , B |
| 130 | + , LSTAT |
| 131 | + FROM [dbo].[features] |
| 132 | + ) |
| 133 | +SELECT predict_input.id |
| 134 | + , p.variable1 AS MEDV |
| 135 | +FROM PREDICT(MODEL = @model, DATA = predict_input, RUNTIME=ONNX) WITH (variable1 FLOAT) AS p; |
| 136 | +``` |
| 137 | + |
| 138 | +> [!NOTE] |
| 139 | +> Because the columns and values returned by **PREDICT** can vary by model type, you must define the schema of the returned data by using a **WITH** clause. |
| 140 | +::: moniker-end |
| 141 | + |
| 142 | +::: moniker range=">=sql-server-2017||=azuresqldb-mi-current||>=sql-server-linux-2017||=sqlallproducts-allversions" |
| 143 | +### PREDICT with RevoScale model |
| 144 | + |
| 145 | +In this example, you create a model using **RevoScaleR** in R, and then call the real-time prediction function from T-SQL. |
| 146 | + |
| 147 | +#### Step 1. Prepare and save the model |
| 148 | + |
| 149 | +Run the following code to create the sample database and required tables. |
| 150 | + |
| 151 | +```sql |
| 152 | +CREATE DATABASE NativeScoringTest; |
| 153 | +GO |
| 154 | +USE NativeScoringTest; |
| 155 | +GO |
| 156 | +DROP TABLE IF EXISTS iris_rx_data; |
| 157 | +GO |
| 158 | +CREATE TABLE iris_rx_data ( |
| 159 | + "Sepal.Length" float not null, "Sepal.Width" float not null |
| 160 | + , "Petal.Length" float not null, "Petal.Width" float not null |
| 161 | + , "Species" varchar(100) null |
| 162 | +); |
| 163 | +GO |
| 164 | +``` |
| 165 | + |
| 166 | +Use the following statement to populate the data table with data from the **iris** dataset. |
| 167 | + |
| 168 | +```sql |
| 169 | +INSERT INTO iris_rx_data ("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width" , "Species") |
| 170 | +EXECUTE sp_execute_external_script |
| 171 | + @language = N'R' |
| 172 | + , @script = N'iris_data <- iris;' |
| 173 | + , @input_data_1 = N'' |
| 174 | + , @output_data_1_name = N'iris_data'; |
| 175 | +GO |
| 176 | +``` |
| 177 | + |
| 178 | +Now, create a table for storing models. |
| 179 | + |
| 180 | +```sql |
| 181 | +DROP TABLE IF EXISTS ml_models; |
| 182 | +GO |
| 183 | +CREATE TABLE ml_models ( model_name nvarchar(100) not null primary key |
| 184 | + , model_version nvarchar(100) not null |
| 185 | + , native_model_object varbinary(max) not null); |
| 186 | +GO |
| 187 | +``` |
| 188 | + |
| 189 | +The following code creates a model based on the **iris** dataset and saves it to the table named **models**. |
| 190 | + |
| 191 | +```sql |
| 192 | +DECLARE @model varbinary(max); |
| 193 | +EXECUTE sp_execute_external_script |
| 194 | + @language = N'R' |
| 195 | + , @script = N' |
| 196 | + iris.sub <- c(sample(1:50, 25), sample(51:100, 25), sample(101:150, 25)) |
| 197 | + iris.dtree <- rxDTree(Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width, data = iris[iris.sub, ]) |
| 198 | + model <- rxSerializeModel(iris.dtree, realtimeScoringOnly = TRUE) |
| 199 | + ' |
| 200 | + , @params = N'@model varbinary(max) OUTPUT' |
| 201 | + , @model = @model OUTPUT |
| 202 | + INSERT [dbo].[ml_models]([model_name], [model_version], [native_model_object]) |
| 203 | + VALUES('iris.dtree','v1', @model) ; |
| 204 | +``` |
| 205 | + |
| 206 | +> [!NOTE] |
| 207 | +> Be sure to use the [rxSerializeModel](https://docs.microsoft.com/machine-learning-server/r-reference/revoscaler/rxserializemodel) function from RevoScaleR to save the model. The standard R `serialize` function cannot generate the required format. |
| 208 | +
|
| 209 | +You can run a statement such as the following to view the stored model in binary format: |
| 210 | + |
| 211 | +```sql |
| 212 | +SELECT *, datalength(native_model_object)/1024. as model_size_kb |
| 213 | +FROM ml_models; |
| 214 | +``` |
| 215 | + |
| 216 | +#### Step 2. Run PREDICT on the model |
| 217 | + |
| 218 | +The following simple PREDICT statement gets a classification from the decision tree model using the **native scoring** function. It predicts the iris species based on attributes you provide, petal length and width. |
| 219 | + |
| 220 | +```sql |
| 221 | +DECLARE @model varbinary(max) = ( |
| 222 | + SELECT native_model_object |
| 223 | + FROM ml_models |
| 224 | + WHERE model_name = 'iris.dtree' |
| 225 | + AND model_version = 'v1'); |
| 226 | +SELECT d.*, p.* |
| 227 | + FROM PREDICT(MODEL = @model, DATA = dbo.iris_rx_data as d) |
| 228 | + WITH(setosa_Pred float, versicolor_Pred float, virginica_Pred float) as p; |
| 229 | +go |
| 230 | +``` |
| 231 | + |
| 232 | +If you get the error, "Error occurred during execution of the function PREDICT. Model is corrupt or invalid", it usually means that your query didn't return a model. Check whether you typed the model name correctly, or if the models table is empty. |
| 233 | + |
| 234 | +> [!NOTE] |
| 235 | +> Because the columns and values returned by **PREDICT** can vary by model type, you must define the schema of the returned data by using a **WITH** clause. |
| 236 | +::: moniker-end |
| 237 | + |
| 238 | +## Next steps |
| 239 | + |
| 240 | ++ [PREDICT T-SQL function](../../t-sql/queries/predict-transact-sql.md) |
| 241 | ++ [SQL machine learning documentation](../index.yml) |
| 242 | ++ [Machine learning and AI with ONNX in SQL Edge](/azure/azure-sql-edge/onnx-overview) |
| 243 | ++ [Deploy and make predictions with an ONNX model in Azure SQL Edge](/azure/azure-sql-edge/deploy-onnx) |
0 commit comments