【问题标题】:AttributeError: 'DecisionTreeRegressor' object has no attribute 'save' in GCSAttributeError:“DecisionTreeRegressor”对象在 GCS 中没有“保存”属性
【发布时间】:2021-11-22 23:56:24
【问题描述】:

我试图将用于房价预测的自定义 DecisionTreeRegressor 部署到 GCS Vertex AI。我遵循的教程是MPG dataset tutorial的教程

但是,当我尝试使用命令在本地构建和测试容器时:

docker build ./ -t $IMAGE_URI
docker run $IMAGE_URI

错误信息出来了: AttributeError:“DecisionTreeRegressor”对象没有“保存”属性

我作为 train.py 运行的代码:

# Import libraries necessary for this project
import numpy as np
import pandas as pd
from sklearn.model_selection import ShuffleSplit

# Load the Boston housing dataset
data = pd.read_csv('trainer/housing.csv')
prices = data['MEDV']
features = data.drop('MEDV', axis = 1)

# Import 'train_test_split'
from sklearn.model_selection import train_test_split

# Shuffle and split the data into training and testing subsets
X_train, X_test, y_train, y_test = train_test_split(features, prices, test_size=0.2, random_state = 42)

#Defining model fitting and tuning functions

# Import 'make_scorer', 'DecisionTreeRegressor', and 'GridSearchCV'
from sklearn.tree import DecisionTreeRegressor
from sklearn.metrics import make_scorer
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import r2_score   # Import 'r2_score'
from sklearn.metrics import accuracy_score

# TODO: replace `your-gcs-bucket` with the name of the Storage bucket you created earlier
BUCKET = 'gs://gardena-dps-bucket'


def performance_metric(y_true, y_predict):
    """ Calculates and returns the performance score between 
        true (y_true) and predicted (y_predict) values based on the metric chosen. """
    
    score = r2_score(y_true, y_predict)
    
    # Return the score
    return score

def fit_model(X, y):
    """ Performs grid search over the 'max_depth' parameter for a 
        decision tree regressor trained on the input data [X, y]. """
    
    # Create cross-validation sets from the training data
    cv_sets = ShuffleSplit(n_splits = 10, test_size = 0.20, random_state = 0)

    # Create a decision tree regressor object
    regressor = DecisionTreeRegressor()

    # Create a dictionary for the parameter 'max_depth' with a range from 1 to 10
    params = {'max_depth':[1,2,3,4,5,6,7,8,9,10]}

    # Transform 'performance_metric' into a scoring function using 'make_scorer' 
    scoring_fnc = make_scorer(performance_metric)

    # Create the grid search cv object --> GridSearchCV()
    # Make sure to include the right parameters in the object:
    # (estimator, param_grid, scoring, cv) which have values 'regressor', 'params', 'scoring_fnc', and 'cv_sets' respectively.
    grid = GridSearchCV(estimator=regressor, param_grid=params, scoring=scoring_fnc, cv=cv_sets)

    # Fit the grid search object to the data to compute the optimal model
    grid = grid.fit(X, y)

    # Return the optimal model after fitting the data
    return grid.best_estimator_

# Fit the training data to the model using grid search
reg = fit_model(X_train, y_train)

# Produce a matrix for client data
client_data = [[12, 26.3, 16.99885]]  # Client data in 2D array

# Show predictions
reprice = reg.predict(client_data).astype(int)
reprice

# Export model and save to GCS
reg.save(BUCKET + '/housing/model')

【问题讨论】:

    标签: docker machine-learning google-cloud-platform scikit-learn google-cloud-storage


    【解决方案1】:

    Scikit-learn 估计器不提供任何直接保存其状态的方法。来自 Google documentation,将拟合模型存储到 GCS 的最佳方法是使用 joblib 在本地序列化您的模型,然后将其上传到 GCS。

    如下:

    from google.cloud import storage
    from sklearn.externals import joblib
    
    
    # Export the model to a file
    model = 'model.joblib'
    joblib.dump(pipeline, model)
    
    # Upload the model to GCS
    bucket = storage.Client().bucket(BUCKET_NAME)
    blob = bucket.blob('{}/{}'.format(
        datetime.datetime.now().strftime('model_%Y%m%d_%H%M%S'),
        model))
    blob.upload_from_filename(model)
    

    【讨论】:

      猜你喜欢
      • 2019-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-31
      • 2020-10-27
      • 1970-01-01
      • 2012-12-01
      • 2021-04-19
      相关资源
      最近更新 更多