from keras.datasets import boston_housing import numpy as np from keras import models from keras import layers import matplotlib.pyplot as plt #x,13個特徵,一共404條數據 #y,連續值標籤,單位是千美圓 (x_train, y_train), (x_test, y_test) = boston_housing.load_data() #對數據作原處理,特徵標準化 #每一個特徵減去平均值,除以標準差,獲得的特徵平均值爲0,標準差爲1 avg = x_train.mean(axis=0) x_train -= avg std = np.std(x_train,axis=0) x_train /=std network = models.Sequential() network.add(layers.Dense(64,activation='relu')) network.add(layers.Dense(64,activation='relu')) network.add(layers.Dense(1)) #mse均方偏差,預測值與目標值差的平方 #mae平均絕對偏差,預測值與目標值差的絕對值 network.compile(optimizer='rmsprop',loss='mse',metrics=['mae']) history = network.fit(x_train,y_train,batch_size=16,epochs=80,validation_split=0.1) mae = history.history['val_mean_absolute_error'] epochs = range(1,81) #loss的圖 plt.plot(epochs,mae,'g',label = 'mae') plt.xlabel('epochs') plt.ylabel('mean_absolute_error') #顯示圖例 plt.legend() plt.show()