-
Notifications
You must be signed in to change notification settings - Fork 0
/
nn_regression.py
127 lines (98 loc) · 3.48 KB
/
nn_regression.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.neural_network import MLPRegressor
import warnings
import argparse
import os
warnings.filterwarnings('ignore')
def sigmoid(a):
return 1 / (1 + np.exp(-a))
def sigmoid_gradient(a):
return (1 - sigmoid(a)) * (sigmoid(a))
def forward(X):
Hidden = X.dot(W1)
# Hidden_sig = sigmoid(Hidden)
Hidden_sig = np.tanh(Hidden)
Out = Hidden_sig.dot(W2)
np.around(Out, decimals=2)
return Hidden_sig, Out
def gradient_W2(Hidden_sig, Y, Out):
# return (Y - Out).dot(Hidden_sig)
return (Out - Y).dot(Hidden_sig)
def gradient_W1(X, Hidden_sig, Y, Out, W2):
# dZ = np.outer(Y - Out, W2) * sigmoid_gradient(Hidden_sig)
dZ = np.outer(Out - Y, W2) * (1 - Hidden_sig * Hidden_sig) # tanh derivative
dZ = X.transpose().dot(dZ)
return dZ
def learn(X, Hidden_sig, Y, Out, W1, W2, learning_rate=0.005):
dW2 = gradient_W2(Hidden_sig, Y, Out)
dW1 = gradient_W1(X, Hidden_sig, Y, Out, W2)
W2 -= learning_rate * dW2
W1 -= learning_rate * dW1
return W1, W2
def get_squared_error(Y, Out):
c = np.square(Y - Out)
return c.mean() # .sum() / N
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--input-layer",
type=int,
default=2,
help="value for input layer length")
parser.add_argument("-H", "--hidden-layer",
type=int,
default=50,
help="value for hidden layer length")
parser.add_argument("-f", "--file", required=True, help="dataset file")
args = parser.parse_args()
print(f"Opening: {args.file}")
df = pd.read_csv(os.path.join(os.path.dirname(__file__), args.file))
X = np.array([df.x, df.y]).transpose()
Y = np.array(df.z)
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.scatter(X[:, 0], X[:, 1], Y, c='red')
plt.show()
D = args.input_layer
M = args.hidden_layer
W1 = np.random.rand(D, M)
W2 = np.random.rand(M)
costs = []
Hidden_sig, Out = forward(X)
cost = get_squared_error(Y, Out)
print(cost)
i = 0
while cost > 0.1:
Hidden_sig, Out = forward(X)
W1, W2 = learn(X, Hidden_sig, Y, Out, W1, W2)
cost = get_squared_error(Y, Out)
costs.append(cost)
if i % 25 == 0:
print(cost)
i += 1
print(f"Took {i} iterations")
plt.plot(costs)
plt.show()
line = np.linspace(0, 2, 50)
xx, yy = np.meshgrid(line, line)
# Create a new dataset for Keras model
K = np.vstack((xx.flatten(), yy.flatten())).T
_, Out = forward(K)
model = MLPRegressor(hidden_layer_sizes=(50,),
activation='tanh',
solver='adam',
alpha=0.01,
max_iter=9500)
df = pd.read_csv(args.file)
X = np.array([df.x, df.y]).transpose()
Y = np.array(df.z)
model.fit(X, Y)
# Prediction of Keras model
Out2 = model.predict(K)
# surface plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_trisurf(K[:, 0], K[:, 1], Out, linewidth=0.2, antialiased=True, alpha=0.5)
ax.plot_trisurf(K[:, 0], K[:, 1], Out2, linewidth=0.2, antialiased=True, color='red', alpha=0.7)
plt.show()