Model Trainer Issue on End-to-End ML Project - TypeError: __init__() got an unexpected keyword argument 'trained_model_file_path'

78 views Asked by At

I am following the process shown on Wine Quality Prediction End-to-End ML Project on Krish Naik's YouTube channel to do a Flight Fare Prediction Project.

I run this cell of model trainer pipeline on 04_model_trainer.ipynb:

try:
    config = ConfigurationManager()
    model_trainer_config = config.get_model_trainer_config()
    model_trainer_config = ModelTrainer(config=model_trainer_config)
    model_trainer_config.train()
except Exception as e:
    raise e

I get this error:

TypeError: __init__() got an unexpected keyword argument 'trained_model_file_path'

Here is the full traceback:

[2023-12-05 08:05:47,529: INFO: common: yaml file: config\config.yaml loaded successfully]
[2023-12-05 08:05:47,544: INFO: common: yaml file: params.yaml loaded successfully]
[2023-12-05 08:05:47,571: INFO: common: yaml file: schema.yaml loaded successfully]
[2023-12-05 08:05:47,573: INFO: common: created directory at: artifacts]
[2023-12-05 08:05:47,578: INFO: common: created directory at: artifacts/model_trainer]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
g:\Machine_Learning_Projects\iNeuron internship\Flight-Fare-Prediction-End-to-End-ML-Project\research\04_model_trainer.ipynb Cell 11 line 7
      <a href='vscode-notebook-cell:/g%3A/Machine_Learning_Projects/iNeuron%20internship/Flight-Fare-Prediction-End-to-End-ML-Project/research/04_model_trainer.ipynb#X12sZmlsZQ%3D%3D?line=4'>5</a>     model_trainer_config.train()
      <a href='vscode-notebook-cell:/g%3A/Machine_Learning_Projects/iNeuron%20internship/Flight-Fare-Prediction-End-to-End-ML-Project/research/04_model_trainer.ipynb#X12sZmlsZQ%3D%3D?line=5'>6</a> except Exception as e:
----> <a href='vscode-notebook-cell:/g%3A/Machine_Learning_Projects/iNeuron%20internship/Flight-Fare-Prediction-End-to-End-ML-Project/research/04_model_trainer.ipynb#X12sZmlsZQ%3D%3D?line=6'>7</a>     raise e

g:\Machine_Learning_Projects\iNeuron internship\Flight-Fare-Prediction-End-to-End-ML-Project\research\04_model_trainer.ipynb Cell 11 line 3
      <a href='vscode-notebook-cell:/g%3A/Machine_Learning_Projects/iNeuron%20internship/Flight-Fare-Prediction-End-to-End-ML-Project/research/04_model_trainer.ipynb#X12sZmlsZQ%3D%3D?line=0'>1</a> try:
      <a href='vscode-notebook-cell:/g%3A/Machine_Learning_Projects/iNeuron%20internship/Flight-Fare-Prediction-End-to-End-ML-Project/research/04_model_trainer.ipynb#X12sZmlsZQ%3D%3D?line=1'>2</a>     config = ConfigurationManager()
----> <a href='vscode-notebook-cell:/g%3A/Machine_Learning_Projects/iNeuron%20internship/Flight-Fare-Prediction-End-to-End-ML-Project/research/04_model_trainer.ipynb#X12sZmlsZQ%3D%3D?line=2'>3</a>     model_trainer_config = config.get_model_trainer_config()
      <a href='vscode-notebook-cell:/g%3A/Machine_Learning_Projects/iNeuron%20internship/Flight-Fare-Prediction-End-to-End-ML-Project/research/04_model_trainer.ipynb#X12sZmlsZQ%3D%3D?line=3'>4</a>     model_trainer_config = ModelTrainer(config=model_trainer_config)
      <a href='vscode-notebook-cell:/g%3A/Machine_Learning_Projects/iNeuron%20internship/Flight-Fare-Prediction-End-to-End-ML-Project/research/04_model_trainer.ipynb#X12sZmlsZQ%3D%3D?line=4'>5</a>     model_trainer_config.train()

g:\Machine_Learning_Projects\iNeuron internship\Flight-Fare-Prediction-End-to-End-ML-Project\research\04_model_trainer.ipynb Cell 11 line 2
     <a href='vscode-notebook-cell:/g%3A/Machine_Learning_Projects/iNeuron%20internship/Flight-Fare-Prediction-End-to-End-ML-Project/research/04_model_trainer.ipynb#X12sZmlsZQ%3D%3D?line=17'>18</a> schema =  self.schema.TARGET_COLUMN
     <a href='vscode-notebook-cell:/g%3A/Machine_Learning_Projects/iNeuron%20internship/Flight-Fare-Prediction-End-to-End-ML-Project/research/04_model_trainer.ipynb#X12sZmlsZQ%3D%3D?line=19'>20</a> create_directories([config.root_dir])
---> <a href='vscode-notebook-cell:/g%3A/Machine_Learning_Projects/iNeuron%20internship/Flight-Fare-Prediction-End-to-End-ML-Project/research/04_model_trainer.ipynb#X12sZmlsZQ%3D%3D?line=21'>22</a> model_trainer_config = ModelTrainerConfig(
     <a href='vscode-notebook-cell:/g%3A/Machine_Learning_Projects/iNeuron%20internship/Flight-Fare-Prediction-End-to-End-ML-Project/research/04_model_trainer.ipynb#X12sZmlsZQ%3D%3D?line=22'>23</a>     root_dir=config.root_dir,
     <a href='vscode-notebook-cell:/g%3A/Machine_Learning_Projects/iNeuron%20internship/Flight-Fare-Prediction-End-to-End-ML-Project/research/04_model_trainer.ipynb#X12sZmlsZQ%3D%3D?line=23'>24</a>     train_data_path = config.train_data_path,
     <a href='vscode-notebook-cell:/g%3A/Machine_Learning_Projects/iNeuron%20internship/Flight-Fare-Prediction-End-to-End-ML-Project/research/04_model_trainer.ipynb#X12sZmlsZQ%3D%3D?line=24'>25</a>     test_data_path = config.test_data_path,
     <a href='vscode-notebook-cell:/g%3A/Machine_Learning_Projects/iNeuron%20internship/Flight-Fare-Prediction-End-to-End-ML-Project/research/04_model_trainer.ipynb#X12sZmlsZQ%3D%3D?line=25'>26</a>     # New Line Added
     <a href='vscode-notebook-cell:/g%3A/Machine_Learning_Projects/iNeuron%20internship/Flight-Fare-Prediction-End-to-End-ML-Project/research/04_model_trainer.ipynb#X12sZmlsZQ%3D%3D?line=26'>27</a>     trained_model_file_path =  os.path.join('artifact', 'model'),
     <a href='vscode-notebook-cell:/g%3A/Machine_Learning_Projects/iNeuron%20internship/Flight-Fare-Prediction-End-to-End-ML-Project/research/04_model_trainer.ipynb#X12sZmlsZQ%3D%3D?line=27'>28</a>     # New Line Ended
     <a href='vscode-notebook-cell:/g%3A/Machine_Learning_Projects/iNeuron%20internship/Flight-Fare-Prediction-End-to-End-ML-Project/research/04_model_trainer.ipynb#X12sZmlsZQ%3D%3D?line=28'>29</a>     model_name = config.model_name,
     <a href='vscode-notebook-cell:/g%3A/Machine_Learning_Projects/iNeuron%20internship/Flight-Fare-Prediction-End-to-End-ML-Project/research/04_model_trainer.ipynb#X12sZmlsZQ%3D%3D?line=29'>30</a>     alpha = params.alpha,
     <a href='vscode-notebook-cell:/g%3A/Machine_Learning_Projects/iNeuron%20internship/Flight-Fare-Prediction-End-to-End-ML-Project/research/04_model_trainer.ipynb#X12sZmlsZQ%3D%3D?line=30'>31</a>     l1_ratio = params.l1_ratio,
     <a href='vscode-notebook-cell:/g%3A/Machine_Learning_Projects/iNeuron%20internship/Flight-Fare-Prediction-End-to-End-ML-Project/research/04_model_trainer.ipynb#X12sZmlsZQ%3D%3D?line=31'>32</a>     target_column = schema.name
     <a href='vscode-notebook-cell:/g%3A/Machine_Learning_Projects/iNeuron%20internship/Flight-Fare-Prediction-End-to-End-ML-Project/research/04_model_trainer.ipynb#X12sZmlsZQ%3D%3D?line=32'>33</a>     
     <a href='vscode-notebook-cell:/g%3A/Machine_Learning_Projects/iNeuron%20internship/Flight-Fare-Prediction-End-to-End-ML-Project/research/04_model_trainer.ipynb#X12sZmlsZQ%3D%3D?line=33'>34</a> )
     <a href='vscode-notebook-cell:/g%3A/Machine_Learning_Projects/iNeuron%20internship/Flight-Fare-Prediction-End-to-End-ML-Project/research/04_model_trainer.ipynb#X12sZmlsZQ%3D%3D?line=35'>36</a> return model_trainer_config

TypeError: __init__() got an unexpected keyword argument 'trained_model_file_path'

Here is the code of ModelTrainer class:

import os
import sys

import pandas as pd
import numpy as np

from sklearn.linear_model import LinearRegression, Lasso, Ridge, ElasticNet
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor, AdaBoostRegressor
from sklearn.tree import DecisionTreeRegressor
from sklearn.svm import SVR
from sklearn.neighbors import KNeighborsRegressor

from sklearn.metrics import r2_score

# from ..exception import CustomException
# from ..logger import logging

# from ..utils import save_obj
# from ..utils import evaluate_model   

# from dataclasses import dataclass

# @dataclass
# class ModelTrainerConfig:
#     trained_model_file_path =  os.path.join('artifact', 'model')


class ModelTrainer:

    def __init__(self):
        self.model_trainer_config = ModelTrainerConfig()    


    def save_obj(file_path, obj):
        try:

            dir_path = os.path.dirname(file_path)
            os.makedirs(dir_path, exist_ok=True)

            with open(file_path, 'wb') as file_obj:
                joblib.dump(obj, file_obj, compress= ('gzip'))

        except Exception as e:
            logger.info('Error occured in utils save_obj')
            raise e
        

    def evaluate_model(X_train, y_train, X_test, y_test, models):

        try:
            report = {}
            for i in range(len(models)):

                model = list(models.values())[i]

                # Train model
                model.fit(X_train,y_train)

                # Predict Testing data
                y_test_pred = model.predict(X_test)

                # Get R2 scores for train and test data
                test_model_score = r2_score(y_test,y_test_pred)

                report[list(models.keys())[i]] =  test_model_score

            return report

        except Exception as e:
            logger.info('Exception occured during model training')
            raise e    



    def initiate_model_training(self, X_train, X_test, y_train, y_test):
        try:
            logger.info('Splitting ')

            models={
            'LinearRegression':LinearRegression(),
            'Lasso':Lasso(),
            'Ridge':Ridge(),
            'Elasticnet':ElasticNet(),
            'RandomForestRegressor': RandomForestRegressor(),
            'GradientBoostRegressor()' : GradientBoostingRegressor(),
            "AdaBoost" : AdaBoostRegressor(),
            'DecisionTreeRegressor' : DecisionTreeRegressor(),
            "SupportVectorRegressor" : SVR(),
            "KNN" : KNeighborsRegressor()
            }

            model_report:dict = ModelTrainer.evaluate_model(X_train,y_train, X_test, y_test, models)
            print(model_report)
            print("\n====================================================================================")
            logger.info(f'Model Report : {model_report}')

            # to get best model score from dictionary
            best_model_score = max(sorted(model_report.values()))

            best_model_name = list(model_report.keys())[
                list(model_report.values()).index(best_model_score)
            ]

            best_model = models[best_model_name]

            print(f"Best Model Found, Model Name :{best_model_name}, R2-score: {best_model_score}")
            print("\n====================================================================================")
            logger.info(f"Best Model Found, Model name: {best_model_name}, R2-score: {best_model_score}")
            logger.info(f"{best_model.feature_names_in_}")
            
            ModelTrainer.save_obj(
            file_path = self.model_trainer_config.trained_model_file_path,
            obj = best_model
            )

        except Exception as e:
            logger.info('Exception occured at model trianing')
            raise e

Here is my file in GitHub.

My file encoding is UTF-8

Would you please help me to fix this issue?

1

There are 1 answers

0
Andrew Ryan On BEST ANSWER

The error that you are facing is relating to:

class ModelTrainer:
    def __init__(self, model_trainer_config):
        self.model_trainer_config = ModelTrainerConfig()

specifically when you call:

    model_trainer_config = ModelTrainer(config=model_trainer_config)

When you call ModelTrainer you are saying for the config variable to be updated, which is impossible as there are no config variables in the ModelTrainer class. So there are two ways to fix this:

Option 1:

Call it it like this:

    model_trainer_config = ModelTrainer(model_trainer_config) # so that you aren't specifying a variable in the class initializer

In your current class should be updated like this to actually make use of the variable.

class ModelTrainer:
    def __init__(self, model_trainer_config):
        self.model_trainer_config = model_trainer_config

Option 2:

Keep it as what you currently have:

    model_trainer_config = ModelTrainer(config=model_trainer_config)

But then in your current class should be updated like this to specify the variable (the : in a function call is used to specify the type of an object that you should be passing in).

class ModelTrainer:
    def __init__(self, config):
        self.model_trainer_config = config

Further, you should use the files that are included in the tutorial, and edit those files for your needs, as there seems to be some differences, which will cause you additional problems likely.

example:

    model_trainer_config = config.get_model_trainer_config() # should be in one variable
    model_trainer_config = ModelTrainer(config=model_trainer_config) # should be in a different variable and not model_trainer_config as train has nothing to do with the config, and is part of the ModelTrainer class 
    model_trainer_config.train()