Applies to: SQL Server 2016 (13.x),
SQL Server 2017 (14.x), and
SQL Server 2019 (15.x)
本文适用于 SQL Server 2016 (13.x)、SQL Server 2017 (14.x) 和 SQL Server 2019 (15.x)。
This article explains how to use PowerShell to add free pretrained machine learning models for sentiment analysis and image featurization to a SQL Server instance having R or Python integration. 预先训练的模型由 Microsoft 生成并且可供使用,作为安装后任务添加到实例中。 For more information about these models, see the Resources section of this article.
从 SQL Server 2022 (16.x) 起,R、Python 和 Java 的运行时不再随 SQL 安装程序一起安装。 请改为安装所需的 R 和/或 Python 自定义运行时和包。 有关详细信息,请参阅在 Windows 上安装 SQL Server 2022 机器学习服务(Python 和 R)。
安装完成后,预先训练的模型将被视为一种实现细节,它支持 MicrosoftML (R) 和 MicrosoftML (Python) 库中的特定功能。 不应(并且不能)查看、自定义或重新定型模型,也不能在自定义代码或成对的其他函数中将它们视为独立的资源。
若要使用预先定型的模型,请调用下表中列出的函数。
| R 函数 (MicrosoftML) | Python 函数 (microsoftml) | Usage |
|---|---|---|
| getSentiment | get_sentiment | 对文本输入生成正负情绪分数。 |
| featurizeImage | featurize_image | 从图像文件输入中提取文本信息。 |
Prerequisites
机器学习算法是计算密集型的。 对于低等到中等的工作负荷,包括使用所有示例数据来完成教程演练,我们建议使用 16 GB RAM。
必须具有计算机和 SQL Server 的管理员权限,才能添加预先训练的模型。
必须启用外部脚本,并且必须运行 SQL Server LaunchPad 服务。 安装说明提供了启用和验证这些功能的步骤。
下载并安装你的 SQL Server 版本的最新累积更新。 请参阅 Microsoft SQL Server 的最新更新。
MicrosoftML R 包或 microsoftml Python 包包含预先训练的模型。
SQL Server 机器学习服务包含机器学习库的两种语言版本,因此无需执行任何其他操作即可满足此必备条件。 由于存在这些库,所以能使用本文中所述的 PowerShell 脚本将预先训练的模型添加到这些库。
MicrosoftML R 包包含预先训练的模型。
SQL Server R Services(仅 R)不包含现成的 MicrosoftML 包。 To add MicrosoftML, you must do a component upgrade. 组件升级的一个优点是可以同时添加预先训练的模型,所以无需运行 PowerShell 脚本。 不过如果已经升级,但第一次没有添加预先训练的模型,则可以按照本文所述的内容运行 PowerShell 脚本。 SQL Server 的两个版本都适用。 在执行此操作之前,请确认 MicrosoftML 库存在于 C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\R_SERVICES\library。
检查是否安装了预先训练的模型
R 和 Python 模型的安装路径如下所示:
R 模型:
C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\R_SERVICES\library\MicrosoftML\mxLibs\x64Python 模型:
C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\PYTHON_SERVICES\Lib\site-packages\microsoftml\mxLibs
模型文件名位于以下列表中:
AlexNet_Updated.modelImageNet1K_mean.xmlpretrained.modelResNet_101_Updated.modelResNet_18_Updated.modelResNet_50_Updated.model
If the models are already installed, skip ahead to the validation step to confirm availability.
下载安装脚本
Visit https://aka.ms/mlm4sql to download the file Install-MLModels.ps1. 在 GitHub 页上,选择“下载原始文件”。
使用提升的权限执行
Start PowerShell. 在任务栏上,右键单击 PowerShell 程序图标,然后选择“以管理员身份运行” 。
安装期间建议的执行策略为“RemoteSigned”。 For more information on setting the PowerShell execution policy, see Set-ExecutionPolicy. For example:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser输入安装脚本文件的完全限定路径,并且包含实例名称。 这里假定为“下载”文件夹和默认实例,该命令可能如下所示:
PS C:\WINDOWS\system32> C:\Users\<user-name>\Downloads\Install-MLModels.ps1 MSSQLSERVER
Output
在连接 Internet 的 SQL Server 机器学习服务默认实例(带有 R 和 Python)上,应会看到类似于以下内容的消息。
MSSQL14.MSSQLSERVER
Verifying R models [9.2.0.24]
Downloading R models [C:\Users\<user-name>\AppData\Local\Temp]
Installing R models [C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\R_SERVICES\]
Verifying Python models [9.2.0.24]
Installing Python models [C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\PYTHON_SERVICES\]
PS C:\WINDOWS\system32>
Verify installation
First, check for the new files in the mxlibs folder. 接下来,运行演示代码以确认模型已安装且正常工作。
R 验证步骤
Start RGUI.EXE at
C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\R_SERVICES\bin\x64.在命令提示符处粘贴以下 R 脚本。
# Create the data CustomerReviews <- data.frame(Review = c( "I really did not like the taste of it", "It was surprisingly quite good!", "I will never ever ever go to that place again!!"), stringsAsFactors = FALSE) # Get the sentiment scores sentimentScores <- rxFeaturize(data = CustomerReviews, mlTransforms = getSentiment(vars = list(SentimentScore = "Review"))) # Let's translate the score to something more meaningful sentimentScores$PredictedRating <- ifelse(sentimentScores$SentimentScore > 0.6, "AWESOMENESS", "BLAH") # Let's look at the results sentimentScoresPress Enter to view the sentiment scores. 输出应如下所示:
> sentimentScores Review SentimentScore 1 I really did not like the taste of it 0.4617899 2 It was surprisingly quite good! 0.9601924 3 I will never ever ever go to that place again!! 0.3103435 PredictedRating 1 BLAH 2 AWESOMENESS 3 BLAH
Python 验证步骤
Start Python.exe at
C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\PYTHON_SERVICES.在命令提示符处粘贴以下 Python 脚本。
import numpy import pandas from microsoftml import rx_logistic_regression, rx_featurize, rx_predict, get_sentiment # Create the data customer_reviews = pandas.DataFrame(data=dict(review=[ "I really did not like the taste of it", "It was surprisingly quite good!", "I will never ever ever go to that place again!!"])) # Get the sentiment scores sentiment_scores = rx_featurize( data=customer_reviews, ml_transforms=[get_sentiment(cols=dict(scores="review"))]) # Let's translate the score to something more meaningful sentiment_scores["eval"] = sentiment_scores.scores.apply( lambda score: "AWESOMENESS" if score > 0.6 else "BLAH") print(sentiment_scores)按 Enter 打印分数。 输出应如下所示:
>>> print(sentiment_scores) review scores eval 0 I really did not like the taste of it 0.461790 BLAH 1 It was surprisingly quite good! 0.960192 AWESOMENESS 2 I will never ever ever go to that place again!! 0.310344 BLAH >>>
Note
如果演示脚本失败,请首先检查文件位置。 在具有多个 SQL Server 实例的系统上,或者对于与独立版本并行运行的实例,安装脚本可能会错误地读取环境,并将文件放在错误的位置。 一般情况下,手动将文件复制到正确的 mxlib 文件夹可以解决此问题。
使用预先训练的模型的示例
下面的链接包含调用预先训练的模型的示例代码。
研究和资源
目前可用的模型是用于情绪分析和图像分类的深度神经网络 (DNN) 模型。 所有预先训练的模型都通过 Microsoft 的计算网络工具包 (CNTK) 进行训练。
每个网络的配置都基于以下引用实现:
ResNet-18ResNet-50ResNet-101AlexNet
有关这些深度学习模型中使用的算法的详细信息,以及如何使用 CNTK 将其实现和定型的详细信息,请参阅以下文章:
Microsoft Researchers' Algorithm Sets ImageNet Challenge Milestone(Microsoft 研究人员的算法成为 ImageNet 挑战中的里程碑)
Microsoft Computational Network Toolkit offers most efficient distributed deep learning computational performance(Microsoft 计算网络工具包提供最有效的分布式深度学习计算性能)