搞了半天,终於有了一个堪用的模型,现在我们要来部署了。一开始部署,我也是吃足了苦头,原因是没有考虑清楚环境的设定,也没有考虑清楚要怎麽跟服务互动,後来搞清楚状况後,就显得相当简单了。最先该做的事情,就是直接使用原本准备好的环境,就87%没有问题了。
部署服务时,一样需要两份 py 档
workspace
利用运算群组执行predict_currency.py
deploy_currency_prediction.py
,在本机执行,将预测的服务部署在workspace
使用服务,也就是标题上所写的 inference,这个词直接查字典的话,意思是推论或推断,可以想做,我们利用过去蒐集到的资料做成一个模型之後,利用此模型推论未来的情境。白话一点,其实就是把做好的模型拿来使用。服务部署完成後,会产生该服务的 API ,便可以透过 API 使用预测服务了。
init
:读取模型。run
:当使用者呼叫 API 时,执行预测,并回传结果。predict_currency.py
import os
from datetime import datetime, timedelta
import pickle
from keras.models import load_model
import investpy
def init():
"""
Load the model
"""
global model
global scaler
# 模型的预设路径就是 /var/azureml-app/azureml-models/,从中找到相对应的模型
for root, _, files in os.walk("/var/azureml-app/azureml-models/", topdown=False):
for name in files:
if name.split(".")[-1] == "h5":
model_path = os.path.join(root, name)
elif name.split(".")[-1] == "pickle":
scaler_path = os.path.join(root, name)
model = load_model(model_path)
with open(scaler_path, "rb") as f_h:
scaler = pickle.load(f_h)
f_h.close()
# 这边的 raw_data 并没有被使用到,因为资料可以直接透过 investpy 取得。
# 但因为直接省略 raw_data ,是无法部署的,所以只好保留。
def run(raw_data):
"""
Prediction
"""
today = datetime.now()
data = investpy.get_currency_cross_historical_data(
"USD/TWD",
from_date=(today - timedelta(weeks=105)).strftime("%d/%m/%Y"),
to_date=today.strftime("%d/%m/%Y"),
)
data.reset_index(inplace=True)
data = data.tail(240).Close.values.reshape(-1, 1)
data = scaler.transform(data)
data = data.reshape((1, 240, 1))
ans = model.predict(data)
ans = scaler.inverse_transform(ans)
# 要注意回传的数值必须要是 JSON 支援的资料格式
return float(ans[0][0])
predict_currency.py
才找得到相对应的模型。deploy_currency_prediction.py
import os
import numpy as np
from azureml.core import Model, Workspace
from azureml.core import Run
from azureml.core.model import InferenceConfig
from azureml.core.webservice import AciWebservice
from azureml.core.authentication import InteractiveLoginAuthentication
def main():
"""
Deploy model to your service
"""
# 为了之後 pipeline 的使用,所以使用两种方式取得 workspace。
run = Run.get_context()
try:
work_space = run.experiment.workspace
except AttributeError:
interactive_auth = InteractiveLoginAuthentication(
tenant_id=os.getenv("TENANT_ID")
)
work_space = Workspace.from_config(auth=interactive_auth)
# 选择之前已经建立好的环境
environment = work_space.environments["train_lstm"]
# 选择模型,如果不挑选版本,则会直接挑选最新模型
model = Model(work_space, "currency")
# scaler 也是会用到
scaler = Model(work_space, name="scaler", version=1)
# 设定部署服务的 config
service_name = "currency-service"
inference_config = InferenceConfig(
entry_script="predict_currency.py", environment=environment
)
# 设定执行服务的资源
aci_config = AciWebservice.deploy_configuration(cpu_cores=1, memory_gb=1)
# 部署服务
service = Model.deploy(
workspace=work_space,
name=service_name,
models=[model, scaler],
inference_config=inference_config,
deployment_config=aci_config,
overwrite=True,
)
service.wait_for_deployment(show_output=True)
print(service.get_logs())
# 印出服务连结,之後就是利用这个连结提供服务
print(service.scoring_uri)
if __name__ == "__main__":
main()
workspace
的端点,检视服务的相关资讯。POST
使用。predict_currency_azml.py
import argparse
import json
import requests
def parse_args():
"""
Parse arguments
"""
parser = argparse.ArgumentParser()
# endpoint url 就是 API 的连结
parser.add_argument("-e", "--endpoint_url", type=str, help="Endpoint url")
args = parser.parse_args()
return args
def main():
"""
Predict mnist data with Azure machine learning
"""
args = parse_args()
data = {"data": ""}
# 将资料转换成 JSON
input_data = json.dumps(data)
# Set the content type
headers = {"Content-Type": "application/json"}
# 使用 POST 呼叫 API
resp = requests.post(args.endpoint_url, input_data, headers=headers)
print("The answer is {}".format(resp.text))
if __name__ == "__main__":
main()
python3.7 predict_currency_azml.py -e 你的 API 连结
,就会得到预测结果了。做到这一步,算是完整的走一遍,利用 Azure machine learning 训练模型与部属服务的流程了,但实际应用上,可能还要考虑日後更新资料和模型的问题,这就会需要用到pipeline
和sechedule
的概念。下一篇,先从pipeline
开始。
继上一篇我们讲到向量类的建立,接着我们在这一篇文机会提到反射行为的模拟~ 反射这种行为,在反射面为铅...
我把从第一天到现在每天的 Home 目录都放上 GitHub 了,README.md 里面有说明 ...
嗨~ 今天来个比较特别的主题,Avatars libraries。很多时候我们需要显示一些头贴,有的...
第一篇记录了时间序列属性,将趋势、季节性等元素拆解、分别画出图表; 第二篇则介绍时间序列转换方法,透...
tags: OC 30 day OC 有哪些数据类型呢? oc 中支援所有C语言的数据变量。 基本数...