1

パイプラインを別のファイルで定義していますmodel.py

class TextSelector(BaseEstimator, TransformerMixin):
    def __init__(self, field):
        self.field = field
    def fit(self, X, y=None):
        return self
    def transform(self, X):
        return X[self.field]

class NumberSelector(BaseEstimator, TransformerMixin):
    def __init__(self, field):
        self.field = field
    def fit(self, X, y=None):
        return self
    def transform(self, X):
        return X[[self.field]]

text_features = Pipeline([
    ('selector', TextSelector(field='text')),
    ('vectorizer', TfidfVectorizer(min_df=5, max_df=0.25, ngram_range=(1, 1))),
    ('decomposer', TruncatedSVD(n_components=300))
])

features = FeatureUnion([
    ('text_features', text_features),
    ('other_feature', NumberSelector(field='other')),
])

pipeline = Pipeline([
    ('features', features),
    ('lgbm', LGBMClassifier(max_depth=-1, n_estimators=300,
                            learning_rate=0.1, n_jobs=2,
                            class_weight='balanced'))
])

モデルをトレーニングしてダンプするには

from model import pipeline

clf = pipeline.fit(X, y)
joblib.dump(clf, 'model.joblib')

モデルをロードするには、スクリプトにアクセスする必要がありますmodel.py。Google mlエンジンを使用する場合、このファイルをどこに置くべきですか?

私は試した

gcloud ml-engine local predict --model-dir=/path/to/models  --json-instances=input.json --framework=SCIKIT_LEARN

ディレクトリmodel.py内で。path/to/models

エラー

cloud.ml.prediction.prediction_utils.PredictionError: モデルの読み込みに失敗しました: モデルを読み込めませんでした: /path/to/the/model/model.joblib。「モデル」という名前のモジュールはありません。(エラーコード:0)

lightgbm別の質問は、 ml-engine 予測で代わりに使用することは可能ですか?

4

1 に答える 1