from keras.datasets import boston_housing
import numpy
print(numpy.shape(boston_housing.load_data()))
(train_data, train_labels), (test_data, test_labels) = boston_housing.load_data()
print(len(train_data))
print(len(train_labels))
print(len(test_data))
print(len(test_labels))
print(numpy.shape(train_data))
print(numpy.shape(train_labels))
print(numpy.shape(test_data))
print(numpy.shape(test_labels))
print(train_labels[:10])
train_data = (train_data - train_data.mean(axis=0)) / train_data.std(axis=0)
test_data = (test_data - train_data.mean(axis=0)) / train_data.std(axis=0)
print(train_data[0])
#-1 ~ +1 ๊น์ง๋ฐ์ดํฐ ์ ๊ทํ
print(test_data[0])
from keras import models
from keras import layers
def build_model() :
model = models.Sequential()
model.add(layers.Dense(64, activation='relu', input_shape = (train_data.shape[1],)))
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(1))
model.compile(optimizer = 'rmsprop', loss = 'mse', metrics = ['mae'])
return model
์ด ๋คํธ์ํฌ์ ๋ง์ง๋ง ์ธต์ ํ๋์ ์ ๋์ ๊ฐ์ง๊ณ ์๊ณ ํ์ฑํ ํจ์๊ฐ ์์.(์ ํ ์ธต).
์ ํ์ ์ธ ์ค์นผ๋ผ ํ๊ท(ํ๋์ ์ฐ์์ ์ธ ๊ฐ์ ์์ธกํ๋ ํ๊ท)๋ฅผ ์ํ ๊ตฌ์ฑ.
๋ง์ง๋ง ์ธต์ด ์์ํ ์ ํ์ด๋ฏ๋ก ๋คํธ์ํฌ๊ฐ ์ด๋ค ๋ฒ์์ ๊ฐ์ด๋ผ๋ ์์ธกํ๋๋ก ์์ ๋กญ๊ฒ ํ์ต๋.
์ด ๋ชจ๋ธ์ mse ์์ค ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ์ปดํ์ผํจ. ํ๊ท ์ ๊ณฑ ์ค์ฐจ(mean squared error)์ ์ฝ์ด๋ก ์์ธก๊ณผ ํ๊น ์ฌ์ด ๊ฑฐ๋ฆฌ์ ์ ๊ณฑ. ํ๊ท ๋ฌธ์ ์์ ๋๋ฆฌ ์ฌ์ฉ๋๋ ์์ค ํจ์.
ํ๋ จํ๋ ๋์ ๋ชจ๋ํฐ๋ง์ ์ํด ์๋ก์ด ์งํ์ธ ํ๊ท ์ ๋ ์ค์ฐจ(Mean Absolute Error, MAE)๋ฅผ ์ธก์ . ์ด๋ ์์ธก๊ณผ ํ๊น ์ฌ์ด ๊ฑฐ๋ฆฌ์ ์ ๋๊ฐ. ์๋ฅผ ๋ค์ด ์ด ์์ ์์ MAE๊ฐ 0.5๋ฉด ์์ธก์ด ํ๊ท ์ ์ผ๋ก 500๋ฌ๋ฌ ์ ๋ ์ฐจ์ด๊ฐ ๋๋ค๋ ๋ป.
true flase ๊ฐ์ ๋ฌธ์ ๊ฐ ์๋๋๊น accuracy ์์.
k =4
num_val_sample = len(train_data) //k
num_epochs = 100
all_scores = []
for i in range(k) :
print("์ฒ๋ฆฌ์ค์ธ ํด๋ : ", i)
val_data = train_data[i*num_val_sample : (i+1) * num_val_sample]
val_targets = train_labels[i*num_val_sample : (i+1) * num_val_sample]
partial_train_data = numpy.concatenate([train_data[ : i*num_val_sample], train_data[(i+1) * num_val_sample : ]], axis = 0)
partial_train_targets = numpy.concatenate([train_labels[ : i*num_val_sample], train_labels[(i+1) * num_val_sample : ]], axis = 0)
model = build_model()
model.fit(partial_train_data, partial_train_targets, epochs = num_epochs, batch_size=1, verbose =1)
val_mse, val_mae = model.evaluate(val_data, val_targets, verbose=1)
all_scores.append(val_mae)
์ฌ๊ธฐ์ concatenate๋ ๋ฐฐ์ด ์ฐ๊ฒฐํด์ฃผ๋ ๊ฒ.
print("์
๋ ฅ๊ฐ: ",all_scores)
numpy.mean(all_scores)
print("ํ๊ท ๊ฐ : " ,numpy.mean(all_scores))
#์ํฌํฌ 500์ผ๋ก ๋ค์ ์ค์
k =4
num_val_sample = len(train_data) //k
num_epochs = 500
all_mae_history = []
for i in range(k) :
print("์ฒ๋ฆฌ์ค์ธ ํด๋ : ", i)
val_data = train_data[i*num_val_sample : (i+1) * num_val_sample]
val_targets = train_labels[i*num_val_sample : (i+1) * num_val_sample]
partial_train_data = numpy.concatenate([train_data[ : i*num_val_sample], train_data[(i+1) * num_val_sample : ]], axis = 0)
partial_train_targets = numpy.concatenate([train_labels[ : i*num_val_sample], train_labels[(i+1) * num_val_sample : ]], axis = 0)
model = build_model()
history = model.fit(partial_train_data, partial_train_targets, epochs = num_epochs, batch_size=1, verbose =1,validation_data=(val_data,val_targets))
mae_history = history.history['val_mae']
all_mae_history.append(mae_history)
print(history.history.keys())
average_mae_history = [numpy.mean([x[i] for x in all_mae_history]) for i in range(num_epochs)]
import matplotlib.pyplot as plt
plt.plot(range(1, len(average_mae_history) + 1), average_mae_history)
plt.xlabel('Epochs')
plt.ylabel('Validation MAE')
plt.show()
def smooth_curve(points, factor=0.9):
smoothed_points = []
for point in points:
if smoothed_points:
previous = smoothed_points[-1]
smoothed_points.append(previous * factor + point * (1 - factor))
else:
smoothed_points.append(point)
return smoothed_points
smooth_mae_history = smooth_curve(average_mae_history[10:])
plt.plot(range(1, len(smooth_mae_history) + 1), smooth_mae_history)
plt.xlabel('Epochs')
plt.ylabel('Validation MAE')
plt.show()
test_mse_score, test_mae_score = model.evaluate(test_data, test_labels)
print(test_mse_score)
print(test_mae_score)