Inder-26 commited on
Commit
ef7b899
·
1 Parent(s): d3b5bd8

mlflow implemented in model_trainer.py

Browse files
networksecurity/components/model_trainer.py CHANGED
@@ -1,4 +1,4 @@
1
- import os,sys
2
  from networksecurity.exception.exception import NetworkSecurityException
3
  from networksecurity.logging.logger import logging
4
 
@@ -23,6 +23,17 @@ class ModelTrainer:
23
  self.data_transformation_artifact = data_transformation_artifact
24
  except Exception as e:
25
  raise NetworkSecurityException(e,sys)
 
 
 
 
 
 
 
 
 
 
 
26
 
27
  def train_model(self,X_train,X_test,y_train,y_test):
28
  model = {
@@ -41,15 +52,15 @@ class ModelTrainer:
41
  "Random Forest": {
42
  #'criterion':['gini','entropy','log_loss'],
43
  #'max_features':['sqrt','log2'],
44
- 'n_estimators':[8,16,32,64,128,256]
45
  },
46
  "Gradient Boosting": {
47
  'learning_rate':[.1,.01,.05,.001],
48
- 'subsample':[0.6,0.7,0.75,0.8,0.85,0.9],
49
  'n_estimators':[8,16,32,64,128,256]
50
  },
51
  "AdaBoost": {
52
- 'learning_rate':[.1,.01,.05,.001],
53
  'n_estimators':[8,16,32,64,128,256]
54
  },
55
  "Logistic Regression": {},
@@ -74,7 +85,10 @@ class ModelTrainer:
74
  classification_test_metric=get_classification_score(y_true=y_test, y_pred=y_test_pred)
75
 
76
 
77
- ## Track with mlflow
 
 
 
78
 
79
 
80
  preprocessor = load_object(file_path=self.data_transformation_artifact.transformed_object_file_path)
 
1
+ import os,sys,mlflow
2
  from networksecurity.exception.exception import NetworkSecurityException
3
  from networksecurity.logging.logger import logging
4
 
 
23
  self.data_transformation_artifact = data_transformation_artifact
24
  except Exception as e:
25
  raise NetworkSecurityException(e,sys)
26
+
27
+ def track_model_mlflow(self, best_model, classification_metric):
28
+ with mlflow.start_run():
29
+ f1_score = classification_metric.f1_score
30
+ precision_score = classification_metric.precision_score
31
+ recall_score = classification_metric.recall_score
32
+
33
+ mlflow.log_metric("F1_score", f1_score)
34
+ mlflow.log_metric("Precision Score", precision_score)
35
+ mlflow.log_metric("Recall Score", recall_score)
36
+ mlflow.sklearn.log_model(best_model, "is the best model")
37
 
38
  def train_model(self,X_train,X_test,y_train,y_test):
39
  model = {
 
52
  "Random Forest": {
53
  #'criterion':['gini','entropy','log_loss'],
54
  #'max_features':['sqrt','log2'],
55
+ 'n_estimators':[8,16,32,128,256]
56
  },
57
  "Gradient Boosting": {
58
  'learning_rate':[.1,.01,.05,.001],
59
+ 'subsample':[0.6,0.7,0.75,0.85,0.9],
60
  'n_estimators':[8,16,32,64,128,256]
61
  },
62
  "AdaBoost": {
63
+ 'learning_rate':[.1,.01,.001],
64
  'n_estimators':[8,16,32,64,128,256]
65
  },
66
  "Logistic Regression": {},
 
85
  classification_test_metric=get_classification_score(y_true=y_test, y_pred=y_test_pred)
86
 
87
 
88
+ ## Track with experiments with mlflow
89
+ self.track_model_mlflow(best_model,classification_train_metric)
90
+ self.track_model_mlflow(best_model,classification_test_metric)
91
+
92
 
93
 
94
  preprocessor = load_object(file_path=self.data_transformation_artifact.transformed_object_file_path)
requirements.txt CHANGED
@@ -7,4 +7,5 @@ certifi
7
  pymongo[srv]==3.11
8
  scikit-learn
9
  pyaml
 
10
  #-e .
 
7
  pymongo[srv]==3.11
8
  scikit-learn
9
  pyaml
10
+ mlflow
11
  #-e .