hw1: trying t1 and t2

This commit is contained in:
Claudio Maggioni 2021-04-24 16:10:29 +02:00
parent 57b20b502d
commit 547909e8a2
2 changed files with 85 additions and 0 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
ml-venv/
.idea
*.DS_Store*

83
assignment_1/src/t1.py Normal file
View File

@ -0,0 +1,83 @@
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
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)
X_train -= mean
X_train /= std
X_val -= mean
X_val /= std
network = Sequential()
network.add(Dense(100, activation='relu'))
network.add(Dense(100, activation='relu'))
network.add(Dense(1, activation='sigmoid'))
network.compile(optimizer='rmsprop', loss='mse', metrics=['mse'])
network.fit(X_train, y_train, epochs=2000, verbose=1, batch_size=1000, validation_data=(X_val, y_val))
X_val = A
msq = np.mean((network.predict((X_val[:, 1:] - mean) / std) - y_val) ** 2)
print(msq)
msq = np.mean((network.predict((X_test[:, 1:] - mean) / std) - y_test) ** 2)
print(msq)