-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_MLP.py
55 lines (46 loc) · 2.11 KB
/
run_MLP.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
import pandas as pd
import numpy as np
from keras.engine.saving import load_model
from keras.layers import Dense
from keras.models import Sequential
variables = ["day_cos", "day_sin", "hour_cos", "hour_sin", "GFS_temp", "NAM_temp", "GFS_hum", "NAM_dew", "load_t_72", "load_t_78", "load_t_84", "load_t_90"]
def train(dataset, verbose=False):
print("Training with "+dataset)
data = pd.read_csv("intermediate/processed_training_input_"+dataset+".csv")
data = data.drop(columns=data.columns[0])
data["day_sin"] = np.sin(data["day"]*2*np.pi/365)
data["day_cos"] = np.cos(data["day"]*2*np.pi/365)
data["hour_sin"] = np.sin(data["hour"]*2*np.pi/24)
data["hour_cos"] = np.cos(data["hour"]*2*np.pi/24)
dependent = np.array(data["load"])
data = data[variables]
independent = np.array(data)
model = Sequential()
model.add(Dense(100, activation = "relu", input_dim = len(variables)))
model.add(Dense(100, activation="relu")) #Experiment: adding a second layer
model.add(Dense(1))
model.compile(loss = "mse", optimizer = "adam")
model.fit(independent, dependent, epochs = 100, batch_size = 100, verbose=verbose)
print("\tTrained! Serializing.")
model.save("models/MLP_"+dataset+".h5")
models = {}
def load(dataset):
print("Loading model for "+dataset)
models[dataset] = load_model("models/MLP_"+dataset+".h5")
def predict(dataset, day, hour, GFS_temp, NAM_temp, GFS_hum, NAM_dew, load_t_72, load_t_78, load_t_84, load_t_90):
if not dataset in models:
load(dataset)
day_sin = np.sin(day*2*np.pi/365)
day_cos = np.cos(day*2*np.pi/365)
hour_sin = np.sin(hour*2*np.pi/24)
hour_cos = np.cos(hour*2*np.pi/24)
return models[dataset].predict(np.array([day_cos, day_sin, hour_cos, hour_sin, GFS_temp, NAM_temp, GFS_hum, NAM_dew, load_t_72, load_t_78, load_t_84, load_t_90]).reshape(1, len(variables)))[0][0]
def main(verbose=True):
train("load_1", verbose)
train("load_12", verbose)
train("load_51", verbose)
if __name__ == "__main__":
main()
# print_importances("load_1")
# print_importances("load_12")
# print_importances("load_51")