-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_regression.py
125 lines (100 loc) · 3.04 KB
/
test_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
from numpy import ndarray
import numpy as np
from layer.dense import Dense
from loss.mse import MeanSquaredError
from network.neuronalNetwork import NeuralNetwork
from operations.linear import Linear
from operations.sigmoid import Sigmoid
from optimizer.sgd import SGD
from trainer.trainer import Trainer
def mae(y_true: ndarray, y_pred: ndarray):
'''
Compute mean absolute error for a neural network.
'''
return np.mean(np.abs(y_true - y_pred))
def rmse(y_true: ndarray, y_pred: ndarray):
'''
Compute root mean squared error for a neural network.
'''
return np.sqrt(np.mean(np.power(y_true - y_pred, 2)))
def eval_regression_model(model: NeuralNetwork,
X_test: ndarray,
y_test: ndarray):
'''
Compute mae and rmse for a neural network.
'''
preds = model.forward(X_test)
preds = preds.reshape(-1, 1)
print("Mean absolute error: {:.2f}".format(mae(preds, y_test)))
print()
print("Root mean squared error {:.2f}".format(rmse(preds, y_test)))
lr = NeuralNetwork(
layers=[Dense(neurons=1,
activation=Linear())],
loss=MeanSquaredError(),
seed=20190501
)
nn = NeuralNetwork(
layers=[Dense(neurons=13,
activation=Sigmoid()),
Dense(neurons=1,
activation=Linear())],
loss=MeanSquaredError(),
seed=20190501
)
dl = NeuralNetwork(
layers=[Dense(neurons=13,
activation=Sigmoid()),
Dense(neurons=13,
activation=Sigmoid()),
Dense(neurons=1,
activation=Linear())],
loss=MeanSquaredError(),
seed=20190501
)
from sklearn.datasets import load_boston
boston = load_boston()
data = boston.data
target = boston.target
features = boston.feature_names
# Scaling the data
from sklearn.preprocessing import StandardScaler
s = StandardScaler()
data = s.fit_transform(data)
def to_2d_np(a: ndarray,
type: str="col") -> ndarray:
'''
Turns a 1D Tensor into 2D
'''
assert a.ndim == 1, \
"Input tensors must be 1 dimensional"
if type == "col":
return a.reshape(-1, 1)
elif type == "row":
return a.reshape(1, -1)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(data, target, test_size=0.3, random_state=80718)
# make target 2d array
y_train, y_test = to_2d_np(y_train), to_2d_np(y_test)
trainer = Trainer(lr, SGD(lr=0.01))
trainer.fit(X_train, y_train, X_test, y_test,
epochs = 50,
eval_every = 10,
seed=20190501);
print()
eval_regression_model(lr, X_test, y_test)
trainer = Trainer(nn, SGD(lr=0.01))
trainer.fit(X_train, y_train, X_test, y_test,
epochs = 50,
eval_every = 10,
seed=20190501);
print()
eval_regression_model(nn, X_test, y_test)
trainer = Trainer(dl, SGD(lr=0.01))
trainer.fit(X_train, y_train, X_test, y_test,
epochs = 50,
eval_every = 10,
seed=20190501);
print()
eval_regression_model(dl, X_test, y_test)
print()