-
Notifications
You must be signed in to change notification settings - Fork 1
/
mlp_model.py
70 lines (52 loc) · 2 KB
/
mlp_model.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
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import StandardScaler, MinMaxScaler, PolynomialFeatures
from sklearn.neural_network import MLPRegressor
import sti.sti_core
import pickle
# Data from optimization
filename_data ='data/merged/data_boost.csv'
# Store the final model here for use later
filename_model = 'models/mlp-boost'
df = pd.read_csv(filename_data)
# Using standardized problem, so start state is 0
X = df.iloc[:, 5:11].values
# Intermediate points we'd like to predict
y = df.iloc[:,11:].values
# Create training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.1, random_state=42)
# Network
# Depth-width relationship based on https://arxiv.org/abs/2001.07523
# Estimate a range of archs.
for i in range(4, 10):
depth = i
width = depth * 8
condensation0 = int(width / 2)
network = [width] * depth
network.append(condensation0)
print("MLP depth: ", depth)
print("MLP hidden arch:", network)
# Training epohcs
epochs = 1000
# Pipeline definition inc. scaling
model = Pipeline([
# ('scaler', StandardScaler()),
('scaler', MinMaxScaler((-1,1))),
# ('poly', PolynomialFeatures(degree=2)),
('mlp', MLPRegressor(hidden_layer_sizes=network, max_iter=epochs, verbose=True))
])
# Fit the model to the training data
model.fit(X_train, y_train)
# Predict on the test data: y_pred
y_pred = model.predict(X_test)
# Compute and print R^2 and RMSE
print("R^2: {}".format(model.score(X_test, y_test)))
rmse = np.sqrt(mean_squared_error(y_test, y_pred))
print("Root Mean Squared Error: {}".format(rmse))
filename_this_model = filename_model + str(depth) + ".sav"
# Store the model
with open(filename_this_model, 'wb') as file:
pickle.dump(model, file)