You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This article explains how scoring in near-real-time works for SQL Server relational data, using machine learning models written in R.
18
-
19
-
> [!Note]
20
-
> Native scoring is a special implementation of real-time scoring that uses the native T-SQL PREDICT function for very fast scoring. For more information and availability, see [Native scoring](sql-native-scoring.md).
17
+
Real-time scoring uses the CLR extension capabilities in SQL Server for high-performance predictions or scores in forecasting workloads. Because real-time scoring is language-agnostic, it executes with no dependencies on R or Python run times. Assuming a model created from Microsoft functions, trained, and serialized to a binary format in SQL Server, you can use real-time scoring to generate predicted outcomes on new data inputs on SQL Server instances that do not have the R or Python add-on features installed.
21
18
22
19
## How real-time scoring works
23
20
@@ -32,41 +29,58 @@ Real-time scoring is a multi-step process:
32
29
3. You provide new input data, either tabular or single rows, as input to the model.
33
30
4. To generate scores, call the sp_rxPredict stored procedure.
34
31
35
-
## Get started
32
+
> [!TIP]
33
+
> For an example of real-time scoring in action, see [End to End Loan ChargeOff Prediction Built Using Azure HDInsight Spark Clusters and SQL Server 2016 R Service](https://blogs.msdn.microsoft.com/rserver/2017/06/29/end-to-end-loan-chargeoff-prediction-built-using-azure-hdinsight-spark-clusters-and-sql-server-2016-r-service/)
36
34
37
-
For code examples and instructions, see [How to perform native scoring or real-time scoring](r/how-to-do-realtime-scoring.md).
35
+
## Prerequisites
38
36
39
-
For an example of how rxPredict can be used for scoring, see [End to End Loan ChargeOff Prediction Built Using Azure HDInsight Spark Clusters and SQL Server 2016 R Service](https://blogs.msdn.microsoft.com/rserver/2017/06/29/end-to-end-loan-chargeoff-prediction-built-using-azure-hdinsight-spark-clusters-and-sql-server-2016-r-service/)
37
+
+[Enable SQL Server CLR integration](https://docs.microsoft.com/dotnet/framework/data/adonet/sql/introduction-to-sql-server-clr-integration).
40
38
41
-
> [!TIP]
42
-
> If you are working exclusively in R code, you can also use the [rxPredict](https://docs.microsoft.com/r-server/r-reference/revoscaler/rxpredict) function for fast scoring.
+ The model must be trained in advance using one of the supported **rx** algorithms. For R, real-time scoring with `sp_rxPredict` works with [RevoScaleR and MicrosoftML supported algorithms](#bkmk_rt_supported_algos). For Python, see [revoscalepy and microsoftml supported algorithms](#bkmk_py_supported_algos)
43
42
44
-
## Requirements
43
+
+ 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.
45
44
46
-
Real-time scoring is supported on these platforms:
45
+
> [!Note]
46
+
> Real-time scoring is currently optimized for fast predictions on smaller data sets, ranging from a few rows to hundreds of thousands of rows. On big datasets, using [rxPredict](https://docs.microsoft.com/machine-learning-server/r-reference/revoscaler/rxpredict) might be faster.
47
47
48
-
+ SQL Server 2017 Machine Learning Services
49
-
+ SQL Server R Services 2016, with an upgrade of R components to 9.1.0 or later
48
+
<aname="bkmk_py_supported_algos"></a>
50
49
51
-
On SQL Server, you must enable the real-time scoring feature in advance to add the CLR-based libraries to SQL Server.
50
+
## Supported algorithms
52
51
53
-
For information regarding real-time scoring in a distributed environment based on Microsoft R Server, refer to the [publishService](https://docs.microsoft.com/machine-learning-server/r-reference/mrsdeploy/publishservice) function available in the [mrsDeploy package](https://docs.microsoft.com/machine-learning-server/r-reference/mrsdeploy/mrsdeploy-package), which supports publishing models for real-time scoring as a new a web service running on R Server.
52
+
### Python algorithms using real-time scoring
54
53
55
-
### Restrictions
54
+
+ revoscalepy models
56
55
57
-
+ The model must be trained in advance using one of the supported **rx** algorithms. For details, see [Supported algorithms](#bkmk_rt_supported_algos). Real-time scoring with `sp_rxPredict` supports both RevoScaleR and MicrosoftML algorithms.
Models marked with \* also support native scoring with the PREDICT function.
58
63
59
-
+The model must be saved using the new serialization functions: [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.
64
+
+microsoftml models
60
65
61
-
+ Real-time scoring does not use an interpreter; therefore, any functionality that might require an interpreter is not supported during the scoring step. These might include:
+ Models using the `rxGlm` or `rxNaiveBayes` algorithms are not currently supported
73
+
+ Transformations supplied by microsoftml
64
74
65
-
+ RevoScaleR models that use an R transformation function, or a formula that contains a transformation, such as <code>A ~ log(B)</code> are not supported in real-time scoring. To use a model of this type, we recommend that you perform the transformation on the to input data before passing the data to real-time scoring.
+ Real-time scoring is currently optimized for fast predictions on smaller data sets, ranging from a few rows to hundreds of thousands of rows. On big datasets, using [rxPredict](https://docs.microsoft.com/machine-learning-server/r-reference/revoscaler/rxpredict) might be faster.
68
80
69
-
### <aname="bkmk_rt_supported_algos">Algorithms that support real-time scoring
81
+
<aname="bkmk_rt_supported_algos"></a>
82
+
83
+
### R algorithms using real-time scoring
70
84
71
85
+ RevoScaleR models
72
86
@@ -97,15 +111,91 @@ For information regarding real-time scoring in a distributed environment based o
97
111
98
112
### Unsupported model types
99
113
100
-
Real-time scoring is not supported for R transformations other than those explicitly listed in the previous section.
114
+
Real-time scoring does not use an interpreter; therefore, any functionality that might require an interpreter is not supported during the scoring step. These might include:
115
+
116
+
+ Models using the `rxGlm` or `rxNaiveBayes` algorithms are not supported.
117
+
118
+
+ Models using a transformation function or formula containing a transformation, such as <code>A ~ log(B)</code> are not supported in real-time scoring. To use a model of this type, we recommend that you perform the transformation on input data before passing the data to real-time scoring.
119
+
120
+
121
+
## Example: sp_rxPredict
122
+
123
+
This section describes the steps required to set up **real-time** prediction, and provides an example in R of how to call the function from T-SQL.
124
+
125
+
<aname ="bkmk_enableRtScoring"></a>
126
+
127
+
### Step 1. Enable the real-time scoring procedure
128
+
129
+
You must enable this feature for each database that you want to use for scoring. The server administrator should run the command-line utility, RegisterRExt.exe, which is included with the RevoScaleR package.
130
+
131
+
> [!NOTE]
132
+
> In order for real-time scoring to work, SQL CLR functionality needs to be enabled in the instance; additionally, the database needs to be marked trustworthy. When you run the script, these actions are performed for you. However, consider the additional security implications before doing this!
133
+
134
+
1. Open an elevated command prompt, and navigate to the folder where RegisterRExt.exe is located. The following path can be used in a default installation:
The instance name is optional if the database is on the default instance. If you are using a named instance, you must specify the instance name.
147
+
148
+
3. RegisterRExt.exe creates the following objects:
149
+
150
+
+ Trusted assemblies
151
+
+ The stored procedure `sp_rxPredict`
152
+
+ A new database role, `rxpredict_users`. The database administrator can use this role to grant permission to users who use the real-time scoring functionality.
153
+
154
+
4. Add any users who need to run `sp_rxPredict` to the new role.
155
+
156
+
> [!NOTE]
157
+
>
158
+
> In SQL Server 2017, additional security measures are in place to prevent problems with CLR integration. These measures impose additional restrictions on the use of this stored procedure as well.
159
+
160
+
### Step 2. Prepare and save the model
161
+
162
+
The binary format required by sp\_rxPredict is the same as the format required to use the PREDICT function. Therefore, in your R code, include a call to [rxSerializeModel](https://docs.microsoft.com/machine-learning-server/r-reference/revoscaler/rxserializemodel), and be sure to specify `realtimeScoringOnly = TRUE`, as in this example:
You call sp\_rxPredict as you would any other stored procedure. In the current release, the stored procedure takes only two parameters: _\@model_ for the model in binary format, and _\@inputData_ for the data to use in scoring, defined as a valid SQL query.
171
+
172
+
Because the binary format is the same that is used by the PREDICT function, you can use the models and data table from the preceding example.
173
+
174
+
```SQL
175
+
DECLARE @irismodel varbinary(max)
176
+
SELECT @irismodel = [native_model_object] from [ml_models]
177
+
WHERE model_name ='iris.dtree'
178
+
AND model_version ='v1''
179
+
180
+
EXEC sp_rxPredict
181
+
@model = @irismodel,
182
+
@inputData = N'SELECT*FROM iris_rx_data'
183
+
```
184
+
185
+
> [!NOTE]
186
+
>
187
+
> The call to sp\_rxPredict fails if the input data for scoring does not include columns that match the requirements of the model. Currently, only the following .NET data types are supported: double, float, short, ushort, long, ulong and string.
188
+
>
189
+
> Therefore, you might need to filter out unsupported types in your input data before using it for real-time scoring.
190
+
>
191
+
> For information about corresponding SQL types, see [SQL-CLR Type Mapping](/dotnet/framework/data/adonet/sql/linq/sql-clr-type-mapping) or [Mapping CLR Parameter Data](https://docs.microsoft.com/sql/relational-databases/clr-integration-database-objects-types-net-framework/mapping-clr-parameter-data).
192
+
193
+
## Disable real-time scoring
194
+
195
+
To disable real-time scoring functionality, open an elevated command prompt, and run the following command: `RegisterRExt.exe /uninstallrts /database:<database_name> [/instance:name]`
108
196
109
197
## Next steps
110
198
111
-
[How to do real-time scoring](r/how-to-do-realtime-scoring.md)
199
+
For an example of how rxPredict can be used for scoring, see [End to End Loan ChargeOff Prediction Built Using Azure HDInsight Spark Clusters and SQL Server 2016 R Service](https://blogs.msdn.microsoft.com/rserver/2017/06/29/end-to-end-loan-chargeoff-prediction-built-using-azure-hdinsight-spark-clusters-and-sql-server-2016-r-service/).
200
+
201
+
For more background on scoring in SQL Server, see [How to generate predictions in SQL Server machine learning](r/how-to-do-realtime-scoring.md).
0 commit comments