从文档:
如果要启用联机预测日志记录,则必须在创建模型资源或创建模型版本资源时对其进行配置,具体取决于要启用的日志记录类型。有三种日志记录类型,可以分别启用:
访问日志记录,它将针对每个请求的时间戳和等待时间等信息记录到Stackdriver Logging。
创建模型资源时,可以启用访问日志记录。
流日志记录,它将来自预测节点的stderr和stdout流记录到Stackdriver
Logging中,并且对于调试很有用。此类型的日志记录处于beta版本,并且Compute Engine(N1)计算机类型不支持。创建模型资源时,可以启用流日志记录。
请求-响应日志记录,它将在线预测请求和响应的样本记录到BigQuery表。这种类型的日志记录处于beta状态。
您可以通过创建模型版本资源,然后更新该版本来启用请求-响应日志记录。
对于您的用例,请使用以下模板将自定义信息记录到StackDriver中:
模型
gcloud beta ai-platform models create {MODEL_NAME} --regions {REGION} --enable-logging --enable-console-logging型号版本
gcloud beta ai-platform versions create {VERSION_NAME} --model {MODEL_NAME} --origin gs://{BUCKET}/{MODEL_DIR} --python-version 3.7 --runtime-version 1.15 --package-uris gs://{BUCKET}/{PACKAGES_DIR}/custom-model-0.1.tar.gz --prediction-class=custom_prediction.CustomModelPrediction --service-account custom@project_id.iam.gserviceaccount.com我尝试了这一点,并工作正常:
- 由于@classmethod装饰器,我对构造函数做了一些修改。
- 创建一个服务帐户并授予其“ Stackdriver Debugger User”角色,在创建模型版本时使用它
- 将
google-cloud-logging
库添加到您的setup.py
- 考虑启用StackDriver日志记录的额外成本
- 使用时
log_struct
检查是否传递了正确的类型。(如果使用str
,请确保使用转换bytes
为str
Python 3.depre('utf-8')) 在Stackdriver client创建过程中定义project_id参数
logging.Client()
,否则您将获得:ERROR:root:Prediction failed: 400 Name “projects//logs/my-custom-prediction-log” is missing the parent component. Expected the form projects/[PROJECT_ID]/logs/[ID]”
代码如下:
%%writefile cloud_logging.pyimport osimport pickleimport numpy as npfrom datetime import datefrom google.cloud import loggingimport tensorflow.keras as kerasLOG_NAME = 'my-custom-prediction-log'class CustomModelPrediction(object): def __init__(self, model, processor, client): self._model = model self._processor = processor self._client = client def _postprocess(self, predictions): labels = ['negative', 'positive'] return [ { "label":labels[int(np.round(prediction))], "score":float(np.round(prediction, 4)) } for prediction in predictions] def predict(self, instances, **kwargs): logger = self._client.logger(LOG_NAME) logger.log_struct({'instances':instances}) preprocessed_data = self._processor.transform(instances) predictions = self._model.predict(preprocessed_data) labels = self._postprocess(predictions) return labels @classmethod def from_path(cls, model_dir): client = logging.Client(project='project_id') # Change to your project model = keras.models.load_model( os.path.join(model_dir,'keras_saved_model.h5')) with open(os.path.join(model_dir, 'processor_state.pkl'), 'rb') as f: processor = pickle.load(f) return cls(model, processor, client)# Verify model locallyfrom cloud_logging import CustomModelPredictionclassifier = CustomModelPrediction.from_path('.')requests = ["God I hate the north", "god I love this"]response = classifier.predict(requests)response然后我检查示例库:
python snippets.py my-custom-prediction-log listListing entries for logger my-custom-prediction-log:* 2020-02-19T19:51:45.809767+00:00: {u'instances': [u'God I hate the north', u'god I love this']}* 2020-02-19T19:57:18.615159+00:00: {u'instances': [u'God I hate the north', u'god I love this']}要可视化日志,请在StackDriver>日志记录>选择全局和您的日志名称中,如果要查看模型日志,则应该能够选择Cloud ML模型版本。
您可以在此处使用我的文件:模型和预处理器



