-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredi_home_ML.py
97 lines (82 loc) · 3.2 KB
/
predi_home_ML.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
import time
import numpy as np
import pandas as pd
from sklearn.model_selection import GridSearchCV
from sklearn.neural_network import MLPClassifier
def df_diff(df1, df2):
return (df1 != df2).astype(int).sum().sum()
T = 60 * 10
t_intervals = np.arange(0, 86400, T)
# Import training/history data synchronized to T.
df = pd.read_csv('milan_sched_1.txt', header=None)
# Adaptation Test - Override Training Data
df_override = pd.read_csv('kyoto_sched_1.txt', header=None)
adapt_test = True
err_graph = [] # Errors relative to adaptive dataset.
div_graph = [] # Errors relative to pre-trained dataset.
# Construct training data.
X_train = df.copy()
X_train["Time"] = np.arange(t_intervals.shape[0])
Y_train = df.copy().iloc[np.arange(1-len(df), 1)]
Y_train.index = np.arange(len(df))
X_adapt = df_override.copy()
X_adapt["Time"] = np.arange(t_intervals.shape[0])
Y_adapt = df_override.copy().iloc[np.arange(1-len(df_override), 1)]
Y_adapt.index = np.arange(len(df_override))
# Initialize Pre-Trained MLP Classifier for Reinforcement/Imitation Learning
mlp_params = {
"hidden_layer_sizes": (25, 25, 25, 25),
"activation": 'relu',
"solver": 'adam',
"learning_rate_init": 0.0017,
"max_iter": 24000,
"n_iter_no_change": 2500,
"shuffle": False,
"warm_start": True,
"verbose": False
}
model = MLPClassifier(**mlp_params)
# Overfit.
print("Pre-Training Phase.")
t0 = time.time()
model.fit(X_train, Y_train)
t1 = time.time()
print("Training Time = ", t1 - t0)
# Compute Sequential Trajectory Predictions.
preds = [pd.DataFrame(model.predict(X_train[X_train["Time"] == 0]))]
preds[0]["Time"] = 1
for t in range(2, t_intervals.shape[0]):
preds.append(pd.DataFrame(model.predict(preds[-1])))
preds[-1]["Time"] = t
df_preds = pd.concat(preds, ignore_index=True)
df_preds.index = np.arange(1, t_intervals.shape[0])
pd.DataFrame(model.predict(X_train)).to_csv('preds', ',')
# Compute (propagated) error in predictions.
error = df_diff(df_preds.drop(["Time"], axis=1), X_train.iloc[1:].drop(["Time"], axis=1))
print("Optimal Pre-Train Error =", error)
target_error = df_diff(df_preds.drop(["Time"], axis=1), X_adapt.iloc[1:].drop(["Time"], axis=1))
print("Initial Adapt Error =", target_error)
print("Adaptation Phase.")
for k in range(100):
# Adapt.
model.fit(X_adapt, Y_adapt)
# Test.
preds = [pd.DataFrame(model.predict(X_adapt[X_adapt["Time"] == 0]))]
preds[0]["Time"] = 1
for t in range(2, t_intervals.shape[0]):
preds.append(pd.DataFrame(model.predict(preds[-1])))
preds[-1]["Time"] = t
df_preds = pd.concat(preds, ignore_index=True)
df_preds.index = np.arange(1, t_intervals.shape[0])
# Compute (propagated) error in predictions.
discrep = df_diff(df_preds.drop(["Time"], axis=1), X_adapt.iloc[1:].drop(["Time"], axis=1))
diverge = df_diff(df_preds.drop(["Time"], axis=1), X_train.iloc[1:].drop(["Time"], axis=1))
err_graph.append(discrep)
div_graph.append(diverge)
print('Prediction Error = %d for cycle %d.' % (discrep, k))
print('Divergence Measure = %d for cycle %d' % (diverge, k))
# Output predictions and error stats.
pd.DataFrame(model.predict(X_adapt)).to_csv('preds_adapt', ',')
pd.DataFrame(err_graph).to_csv('error_data', ',', index=False)
pd.DataFrame(div_graph).to_csv('divergence_data', ',', index=False)
print("Test completed.")