101 lines
2.6 KiB
Python
101 lines
2.6 KiB
Python
import numpy as np
|
|
from sklearn.linear_model import LinearRegression
|
|
from sklearn.model_selection import train_test_split
|
|
from tensorflow.keras import Input, Model
|
|
from tensorflow.keras.models import Sequential
|
|
from tensorflow.keras.layers import Dense
|
|
from tensorflow.keras import losses
|
|
from tensorflow import keras
|
|
import tensorflow as tf
|
|
from sklearn.metrics import mean_squared_error
|
|
|
|
data = np.load("../data/data.npz")
|
|
xs = data["x"] # 2000x2
|
|
y = data["y"] # 2000x1
|
|
points = 2000
|
|
|
|
# We manually include in the feature vectors a '1' column corresponding to theta_0,
|
|
# so disable
|
|
lr = LinearRegression(fit_intercept=False)
|
|
|
|
# Build x feature vector with columns for theta_3 and theta_4
|
|
# variable name explained here: https://vimeo.com/380021022
|
|
X = np.zeros([points, 5])
|
|
X[:, 0] = 1
|
|
X[:, 1:3] = xs
|
|
X[:, 3] = xs[:, 0] * xs[:, 1]
|
|
X[:, 4] = np.sin(xs[:, 0])
|
|
|
|
# Shuffle our data for division in training, and test set
|
|
np.random.seed(0) # seed the generation for reproducibility purposes
|
|
|
|
train_ratio = 0.1
|
|
validation_ratio = 0.1
|
|
|
|
X_t, X_test, y_t, y_test = train_test_split(X, y, test_size=train_ratio)
|
|
X_train, X_val, y_train, y_val = train_test_split(X_t, y_t, test_size=validation_ratio)
|
|
|
|
# Fit with train data
|
|
reg = lr.fit(X_t, y_t)
|
|
|
|
print("# Linear regression:")
|
|
|
|
# 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_))
|
|
|
|
# Test using MSQ on test set
|
|
score = reg.score(X_test, y_test)
|
|
print("MSQ error on test set is: %g" % (score))
|
|
|
|
### Non-linear regression:
|
|
|
|
print("\n# Feed-forward NN:")
|
|
|
|
A = X_val
|
|
|
|
X_train = X_train[:, 1:]
|
|
X_val = X_val[:, 1:]
|
|
|
|
# X_train = X_train[:, 1:3]
|
|
# X_val = X_val[:, 1:3]
|
|
|
|
mean = np.mean(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 /= std
|
|
y_train -= y_mean
|
|
y_train /= y_std
|
|
|
|
X_val -= mean
|
|
X_val /= std
|
|
y_val -= y_mean
|
|
y_val /= y_std
|
|
|
|
network = Sequential()
|
|
network.add(Dense(35, activation='relu'))
|
|
network.add(Dense(10, activation='relu'))
|
|
network.add(Dense(3, activation='relu'))
|
|
network.add(Dense(1, activation='linear'))
|
|
network.compile(optimizer='rmsprop', loss='mse', metrics=['mse'])
|
|
|
|
epochs = 10000
|
|
callback = tf.keras.callbacks.EarlyStopping(monitor='loss', patience=1000)
|
|
network.fit(X_train, y_train, epochs=epochs, verbose=1, batch_size=100, validation_data=(X_val, y_val), callbacks=[callback])
|
|
|
|
msq = mean_squared_error(network.predict(X_val), y_val)
|
|
print(msq)
|
|
|
|
X_test = X_test[:, 1:]
|
|
X_test -= mean
|
|
X_test /= std
|
|
y_test -= y_mean
|
|
y_test /= y_std
|
|
msq = mean_squared_error(network.predict(X_test), y_test)
|
|
print(msq)
|