-
Notifications
You must be signed in to change notification settings - Fork 10
/
model_helper_utils.py
176 lines (140 loc) · 6.07 KB
/
model_helper_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import time
from typing import Tuple, Dict, Any
from sklearn.datasets import fetch_california_housing
from sklearn.ensemble import RandomForestRegressor
from sklearn.tree import DecisionTreeRegressor
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split
import xgboost as xgb
import ray
# states to inspect
STATES = ["INITIALIZED", "RUNNING", "DONE"]
DECISION_TREE_CONFIGS = {"max_depth": 15,
"name": "decision_tree"}
RANDOM_FOREST_CONFIGS = {"n_estimators": 150,
"name": "random_forest"}
XGBOOST_CONFIGS = {"max_depth": 10,
"n_estimators": 150,
"lr": 0.1,
"eta": 0.3,
"colsample_bytree": 1,
"name": "xgboost"}
class ActorCls:
"""
Base class for our Ray Actor workers models
"""
def __init__(self, configs: Dict[Any, Any]) -> None:
self.configs = configs
self.name = configs["name"]
self.state = STATES[0]
self.X, self.y = None, None
self.X_train, self.X_test = None, None
self.y_train, self.y_test = None, None
self.model = None
def get_name(self) -> str:
return self.name
def get_state(self) -> str:
return self.state
def _prepare_data_and_model(self) -> None:
self.X, self.y = fetch_california_housing(return_X_y=True, as_frame=True)
self.X_train, self.X_test, self.y_train, self.y_test = train_test_split(self.X, self.y, test_size=0.2, random_state=4)
def train_and_evaluate_model(self) -> Dict[Any, Any]:
"""
Overwrite this function in super class
"""
pass
@ray.remote
class RFRActor(ActorCls):
"""
An actor model to train and score the calfornia house data using Random Forest Regressor
"""
def __init__(self, configs):
super().__init__(configs)
self.estimators = configs["n_estimators"]
def train_and_evaluate_model(self) -> Dict[Any, Any]:
"""
Train the model and evaluate and report MSE
"""
self._prepare_data_and_model()
self.model = RandomForestRegressor(n_estimators=self.estimators, random_state=42)
print(f"Start training model {self.name} with estimators: {self.estimators} ...")
start_time = time.time()
self.model.fit(self.X_train, self.y_train)
self.state = STATES[1]
y_pred = self.model.predict(self.X_test)
score = mean_squared_error(self.y_test, y_pred)
self.state = STATES[2]
end_time = time.time()
print(f"End training model {self.name} with estimators: {self.estimators} took: {end_time - start_time:.2f} seconds")
return { "state": self.get_state(),
"name": self.get_name(),
"estimators": self.estimators,
"mse": round(score, 4),
"time": round(end_time - start_time, 2)}
@ray.remote
class DTActor(ActorCls):
"""
An actor model to train and score the calfornia house data using Decision Tree Regressor
"""
def __init__(self, configs):
super().__init__(configs)
self.max_depth = configs["max_depth"]
def train_and_evaluate_model(self) -> Dict[Any, Any]:
"""
Train the model and evaluate and report MSE
"""
self._prepare_data_and_model()
self.model = DecisionTreeRegressor(max_depth=self.max_depth, random_state=42)
print(f"Start training model {self.name} with max depth: { self.max_depth } ...")
start_time = time.time()
self.model.fit(self.X_train, self.y_train)
self.state = STATES[1]
y_pred = self.model.predict(self.X_test)
score = mean_squared_error(self.y_test, y_pred)
self.state = STATES[2]
end_time = time.time()
print(f"End training model {self.name} with max_depth tree: {self.max_depth} took: {end_time - start_time:.2f} seconds")
return { "state": self.get_state(),
"name": self.get_name(),
"max_depth": self.max_depth,
"mse": round(score, 4),
"time": round(end_time - start_time, 2)}
@ray.remote
class XGBoostActor(ActorCls):
"""
An actor model to train and score the calfornia house data using XGBoost Regressor
"""
def __init__(self, configs):
super().__init__(configs)
self.max_depth = configs["max_depth"]
self.estimators = configs["n_estimators"]
self.colsample = configs["colsample_bytree"]
self.eta = configs["eta"]
self.lr = configs["lr"]
def train_and_evaluate_model(self) -> Dict[Any, Any]:
"""
Train the model and evaluate and report MSE
"""
self._prepare_data_and_model()
self.model = xgb.XGBRegressor(objective='reg:squarederror',
colsample_bytree=self.colsample,
eta=self.eta,
learning_rate = self.lr,
max_depth=self.max_depth,
n_estimators=self.estimators,
random_state=42)
print(f"Start training model {self.name} with estimators: {self.estimators} and max depth: { self.max_depth } ...")
start_time = time.time()
self.model.fit(self.X_train, self.y_train)
self.state = STATES[1]
y_pred = self.model.predict(self.X_test)
score = mean_squared_error(self.y_test, y_pred)
self.state = STATES[2]
end_time = time.time()
print(f"End training model {self.name} with estimators: {self.estimators} and max depth: { self.max_depth } and took: {end_time - start_time:.2f}")
return {"state": self.get_state(),
"name": self.get_name(),
"max_depth": self.max_depth,
"mse": round(score, 4),
"estimators": self.estimators,
"time": round(end_time - start_time, 2)}