This commit is contained in:
Claudio Maggioni (maggicl) 2021-05-04 11:45:05 +02:00
parent 8a4c98d0d1
commit 4d4e529dbc
8 changed files with 83 additions and 26 deletions

Binary file not shown.

File diff suppressed because one or more lines are too long

54
assignment_1/deliverable/run_model.py Normal file → Executable file
View file

@ -1,6 +1,10 @@
#!/usr/bin/env python3
import joblib import joblib
import numpy as np import numpy as np
from keras import models
import sys
from keras.models import Model
def load_data(filename): def load_data(filename):
""" """
@ -57,13 +61,51 @@ if __name__ == '__main__':
# EDITABLE SECTION OF THE SCRIPT: if you need to edit the script, do it here # EDITABLE SECTION OF THE SCRIPT: if you need to edit the script, do it here
############################################################################ ############################################################################
# Load the trained model ms = {"baseline", "linear", "nonlinear"}
baseline_model_path = './baseline_model.pickle'
baseline_model = load_model(baseline_model_path)
# Predict on the given samples if len(sys.argv) != 2:
y_pred = baseline_model.predict(x) print("%s {model_name}" % sys.argv[0])
sys.exit(1)
if sys.argv[1] not in ms:
print("model name must be one of " + str(ms))
sys.exit(2)
y_pred = None
if sys.argv[1] == "baseline":
# Load the trained model
baseline_model_path = './baseline_model.pickle'
baseline_model = load_model(baseline_model_path)
# Predict on the given samples
y_pred = baseline_model.predict(x)
else:
X = np.zeros([x.shape[0], 5])
X[:, 0] = 1
X[:, 1:3] = x
X[:, 3] = x[:, 0] * x[:, 1]
X[:, 4] = np.sin(x[:, 0])
if sys.argv[1] == "linear":
# Load the linear model
linear_model_path = './linear_regression.pickle'
linear_model = load_model(linear_model_path)
# Predict on the given samples
y_pred = linear_model.predict(X)
else:
# Load saved Keras model
nonlinear_model = models.load_model('./nonlinear_model')
# Load saved normalizing factors for Xs
normalizers = load_model('./nonlinear_model_normalizers.pickle')
# Normalize Xs with normalizing factors
X = X[:, 1:3]
X -= normalizers["mean"]
X /= normalizers["std"]
# Run model on normalized data
y_pred = nonlinear_model.predict(X)
y_pred = y_pred[:, 0]
############################################################################ ############################################################################
# STOP EDITABLE SECTION: do not modify anything below this point. # STOP EDITABLE SECTION: do not modify anything below this point.

View file

@ -2,14 +2,18 @@ import numpy as np
from sklearn.linear_model import LinearRegression from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split from sklearn.model_selection import train_test_split
from tensorflow.keras import Input, Model from tensorflow.keras import Input, Model
from tensorflow.keras.models import Sequential from tensorflow.keras.models import Sequential, save_model
from tensorflow.keras.layers import Dense from tensorflow.keras.layers import Dense
from tensorflow.keras import losses from tensorflow.keras import losses
from tensorflow import keras from tensorflow import keras
import tensorflow as tf import tensorflow as tf
from sklearn.metrics import mean_squared_error from sklearn.metrics import mean_squared_error
from utils import save_sklearn_model, save_keras_model
import os
data = np.load("../data/data.npz") d = os.path.dirname(__file__)
data = np.load(d + "/../data/data.npz")
xs = data["x"] # 2000x2 xs = data["x"] # 2000x2
y = data["y"] # 2000x1 y = data["y"] # 2000x1
points = 2000 points = 2000
@ -43,6 +47,8 @@ print("# Linear regression:")
# Print the resulting parameters # Print the resulting parameters
print("f(x) = %g + %g * x_1 + %g * x_2 + %g * x_1 * x_2 + %g * sin(x_1)" % tuple(reg.coef_)) print("f(x) = %g + %g * x_1 + %g * x_2 + %g * x_1 * x_2 + %g * sin(x_1)" % tuple(reg.coef_))
save_sklearn_model(reg, d + "/../deliverable/linear_regression.pickle")
# Test using MSQ on test set # Test using MSQ on test set
score = reg.score(X_test, y_test) score = reg.score(X_test, y_test)
print("MSQ error on test set is: %g" % (score)) print("MSQ error on test set is: %g" % (score))
@ -53,48 +59,45 @@ print("\n# Feed-forward NN:")
A = X_val A = X_val
X_train = X_train[:, 1:] X_train = X_train[:, 1:3]
X_val = X_val[:, 1:] X_val = X_val[:, 1:3]
# X_train = X_train[:, 1:3] # X_train = X_train[:, 1:3]
# X_val = X_val[:, 1:3] # X_val = X_val[:, 1:3]
mean = np.mean(X_train, axis=0) mean = np.mean(X_train, axis=0)
std = np.std(X_train, axis=0) std = np.std(X_train, axis=0)
#y_mean = np.mean(y_train, axis=0)
#y_std = np.std(y_train, axis=0)
y_mean = 0
y_std = 1
X_train -= mean X_train -= mean
X_train /= std X_train /= std
y_train -= y_mean
y_train /= y_std
X_val -= mean X_val -= mean
X_val /= std X_val /= std
y_val -= y_mean
y_val /= y_std
network = Sequential() network = Sequential()
network.add(Dense(35, activation='relu')) network.add(Dense(30, activation='relu'))
network.add(Dense(20, activation='relu'))
network.add(Dense(20, activation='relu'))
network.add(Dense(10, activation='relu')) network.add(Dense(10, activation='relu'))
network.add(Dense(5, activation='relu'))
network.add(Dense(3, activation='relu')) network.add(Dense(3, activation='relu'))
network.add(Dense(2, activation='relu'))
network.add(Dense(1, activation='linear')) network.add(Dense(1, activation='linear'))
network.compile(optimizer='rmsprop', loss='mse', metrics=['mse']) network.compile(optimizer='rmsprop', loss='mse', metrics=['mse'])
epochs = 10000 epochs = 100000
callback = tf.keras.callbacks.EarlyStopping(monitor='loss', patience=1000) callback = tf.keras.callbacks.EarlyStopping(monitor='loss', patience=40)
network.fit(X_train, y_train, epochs=epochs, verbose=1, batch_size=100, validation_data=(X_val, y_val), callbacks=[callback]) network.fit(X_train, y_train, epochs=epochs, verbose=1, batch_size=15,
validation_data=(X_val, y_val), callbacks=[callback])
network.save(d + "/../deliverable/nonlinear_model")
save_sklearn_model({"mean": mean, "std": std}, d + "/../deliverable/nonlinear_model_normalizers.pickle")
msq = mean_squared_error(network.predict(X_val), y_val) msq = mean_squared_error(network.predict(X_val), y_val)
print(msq) print(msq)
X_test = X_test[:, 1:] X_test = X_test[:, 1:3]
X_test -= mean X_test -= mean
X_test /= std X_test /= std
y_test -= y_mean
y_test /= y_std
msq = mean_squared_error(network.predict(X_test), y_test) msq = mean_squared_error(network.predict(X_test), y_test)
print(msq) print(msq)