-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathex2_2_ann_rg.py
53 lines (40 loc) · 1.5 KB
/
ex2_2_ann_rg.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
# 1. 회귀 ANN 모델링
from keras import layers, models
class ANN(models.Model):
def __init__(self, Nin, Nh, Nout):
# Prepare network layers and activate functions
hidden = layers.Dense(Nh)
output = layers.Dense(Nout)
relu = layers.Activation('relu')
# Connect network elements
x = layers.Input(shape=(Nin,))
h = relu(hidden(x))
y = output(h)
super().__init__(x, y)
self.compile(loss='mse', optimizer='sgd')
# 2. 학습과 평가용 데이터 불러오기
from keras import datasets
from sklearn import preprocessing
def Data_func():
(X_train, y_train), (X_test, y_test) = datasets.boston_housing.load_data()
scaler = preprocessing.MinMaxScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
return (X_train, y_train), (X_test, y_test)
# 3. 회귀 ANN 학습 결과 그래프 구현
import matplotlib.pyplot as plt
from keraspp.skeras import plot_loss
# 4. 회귀 ANN 학습 및 성능 분석
def main():
Nin = 13
Nh = 5
Nout = 1
model = ANN(Nin, Nh, Nout)
(X_train, y_train), (X_test, y_test) = Data_func()
history = model.fit(X_train, y_train, epochs=100, batch_size=100, validation_split=0.2, verbose=2)
performace_test = model.evaluate(X_test, y_test, batch_size=100)
print('\nTest Loss -> {:.2f}'.format(performace_test))
plot_loss(history)
plt.show()
if __name__ == '__main__':
main()