我用 Python 编写了一个遗传算法脚本,并使用 Flask API 对其进行了测试。它接收 JSON 并返回 JSON。 现在我们希望在 Azure 中运行遗传算法,但我们不确定使用哪个服务。我一直在尝试将它集成到 Azure Machine Learning Studio 中,但我觉得它应该被一个完整的机器学习模型使用,而不仅仅是一个 python 脚本。 我们需要的是一种可扩展的方式来运行可通过端点访问的遗传算法。如果可能,它应该以某种方式与 Azure ml studio 兼容。尚未确定哪种类型的数据库,可能会在以后添加。 首选服务是什么?
我尝试在 Azure 机器学习工作室中集成 python 脚本。我们希望这是为此目的提供的适当服务,但现在想知道这到底是否是正确的选择。
我使用 azure 函数应用程序来使用通用算法。在这里,我创建了一个函数应用程序,在向带有数据的函数 URL 发送发布请求后,它会给出模型输出结果。模型结果是通过使用 azure ML 端点实现的,您的模型可以在其中注册并创建一个实时端点,在该实时端点上提出
POST
请求。
这里是要遵循的步骤, 首先,您需要在 Azure ML 中注册您的模型。
创建新的 ML 工作区 > 启动 ML 工作室。
注册模型后去部署如下图
选择实时端点并开始配置询问的详细信息。
接下来选择身份验证作为密钥,如下所示
在环境部分,您需要添加当请求来自客户端时需要执行的源文件。 您参考以下文档如何注册模型和端点创建。 模型注册和端点创建
创建端点后,您将获得如下所示的 rest api url
接下来,您需要在发出请求时获取身份验证密钥。
根据您在 azure CLI 中的配置输入以下命令以获取密钥
az ml online-endpoint get-credentials --name <your endpoint name> --resource-group <your resource grp> --workspace-name <ML workspace name> -o tsv --query primaryKey
拿到钥匙后保存。
使用您的订阅和资源组在 Azure 中创建函数应用。 创建函数初始后,函数选项卡中将没有函数,如下所示, 在这里对我来说,因为我在显示之前部署了功能。
以下是在可视化代码中部署功能的步骤。 转到扩展 > 搜索 azure function 安装它。
安装后,您会在左下角找到蓝色符号。
转到 azure 选项卡,如上所示,有一个创建函数的选项。 单击它并使用
HTTP Trigger
创建函数,给出函数的名称和
授权级别为anonymous
.
成功创建函数后,您将获得如下
__init__.py
和项目结构。
默认情况下,代码如下`init.py.
`用以下代码替换该代码。 您可以在此处添加通用脚本并从模型中获取结果。大家可以根据自己的需要修改,调用ML端点url。
import logging
import urllib.request
import json
import os
import ssl
import azure.functions as func
def allowSelfSignedHttps(allowed):#bypass the server certificate verification on client side
if allowed and not os.environ.get('PYTHONHTTPSVERIFY', '') and getattr(ssl, '_create_unverified_context', None):
ssl._create_default_https_context = ssl._create_unverified_context
allowSelfSignedHttps(True)
def getmodelres(data):
body = str.encode(json.dumps(data))
endpointurl = 'https://xxxx.centralindia.inference.ml.azure.com/score'
endpoint_api_key = "xxxxxxxxxx" # Replace this with the key or token you obtained
print(api_key)
headers = {'Content-Type':'application/json', 'Authorization':('Bearer '+ endpoint_api_key)}
req = urllib.request.Request(endpointurl, body, headers)
try:
response = urllib.request.urlopen(req)
result = response.read()
print(result)
return result
except urllib.error.HTTPError as error:
print("The request failed with status code: " + str(error.code))# Print the headers - they include the requert ID and the timestamp, which are useful for debugging the failure
print(error.info())
print(error.read().decode("utf8", 'ignore'))
return error.info()
def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
data = req.get_json()
name = getmodelres(data)
if name:
return func.HttpResponse(f"Model result, {name}. This HTTP triggered function executed successfully.")
else:
return func.HttpResponse("This HTTP triggered function executed successfully. Pass in the request body for a personalized response."
status_code=200)
接下来在创建端点的脚本中提供端点 url 和端点密钥。
更新代码后,有一个名为 deploy 的选项,如图所示。 单击它并选择您之前在 azure 中创建的
subscription id
、resource group
和 azure function
。
这将开始部署。
然后在 Azure 中转到您的应用程序功能,在那里您可以在功能选项卡下找到已部署的功能,单击它。 您将获得如下详细信息。 在那里你可以获得函数 url。
部署后然后测试功能 URL 作为
POST
请求,正文具有输入数据以 json 格式建模。
我在 postman 中用 azure function url 测试了这个。 您可以在此处看到结果,我向具有数据的函数 url 和正文发送了 post 请求,这些数据是模型的输入。