1

我正在尝试使用 scikit-learn 的 MLPClassifier 在 Google 的 AI 平台上编写自定义预测例程。我已经成功打包并部署了模型,但是当我通过 请求在线预测时gcloud ai-platform predict,我得到了错误"error": "Prediction failed: unknown error."然后我去控制台在我的模型的“测试和使用”部分手动测试我的模型并收到相同的错误.

训练向量是具有 6 个元素的 numpy 数组(例如 [1,2,3,4,5,6]),目标是 0、1 或 2。

这是我的 preprocess.py 代码:

import numpy as np
class MySimpleScaler(object):
  def __init__(self):
    self._means = None
    self._stds = None

  def preprocess(self, data):
    if self._means is None: # during training only
      self._means = np.mean(data, axis=0)

    if self._stds is None: # during training only
      self._stds = np.std(data, axis=0)
      if not self._stds.all():
        raise ValueError('At least one column has standard deviation of 0.')

    return (data - self._means) / self._stds

这是我的 predictor.py 代码:

import os
import pickle
import numpy as np
from sklearn.externals import joblib
from sklearn.neural_network import MLPClassifier

class MyPredictor(object):
  def __init__(self, model, preprocessor):
    self._model = model
    self._preprocessor = preprocessor
    self._class_names = ["0-6 months", "7-18 months", "18+ months"]

  def predict(self, instances, **kwargs):
    inputs = np.asarray(instances)
    preprocessed_inputs = self._preprocessor.preprocess(inputs)
    if kwargs.get('probabilities'):
      probabilities = self._model.predict_proba(preprocessed_inputs)
      return probabilities.tolist()
    else:
      outputs = self._model.predict(preprocessed_inputs)
      return [self._class_names[class_num] for class_num in outputs]

  @classmethod
  def from_path(cls, model_dir):
    model_path = os.path.join(model_dir, 'model.joblib')
    model = joblib.load(model_path)

    preprocessor_path = os.path.join(model_dir, 'preprocessor.pkl')
    with open(preprocessor_path, 'rb') as f:
      preprocessor = pickle.load(f)

    return cls(model, preprocessor)

这是我训练和导出模型的代码:

scaler = MySimpleScaler()
y = data[:, [0]]
features_scaled = scaler.preprocess(data[:, 1:])
scaled_data = np.concatenate((y, features_scaled), 1) # put the scaled features and the y column back 
    together
X = scaled_data[:, 1:]
clf = MLPClassifier()
clf.fit(X, y)

# export the model
joblib.dump(clf, 'model.joblib')
with open ('preprocessor.pkl', 'wb') as f:
    pickle.dump(scaler, f)

设置.py:

from setuptools import setup

setup(
    name='my_custom_code',
    version='0.1',
    include_package_data=True,
    scripts=['predictor.py', 'preprocess.py'])

我尝试使用如下所示的 input.json 文件提供在线预测

[1,2,3,4,5,6]

用这个命令

gcloud ai-platform predict --version $CORRECT_VERSION --model $CORRECT_MODEL --json-instances 
   input.json

我得到了上面的错误。有人可以帮忙吗?我希望 Google AI Platform 有更多信息丰富的错误消息。

4

0 回答 0