【Keras問題集合】使用Keras問題及其理解

1、使用sklearn的個歸一化
https://blog.csdn.net/Qbluesky/article/details/78002223
https://blog.csdn.net/soloyuyang/article/details/80181342
https://www.cnblogs.com/kingboy100/p/11151736.html 迴歸擬合

from sklearn.preprocessing import MinMaxScaler
# The normalized
min_max_scaler_x = MinMaxScaler((0,1))
min_max_scaler_y = MinMaxScaler((0,1))
x_train = min_max_scaler_x.fit_transform(x.reshape(-1,1))
y_train = min_max_scaler_y.fit_transform(y.reshape(-1,1))
# inverse normalized
x_inv = min_max_scaler_x.inverse_transform(x_train );
y_inv = min_max_scaler_y.inverse_transform(y_train );

min_max_scaler_x 會根據要歸一化的樣本改變類內的變量數值,因此只能歸一化。對其他數據歸一化使用的新數據的最大值最小值。

2、Keras用下面的validation_split=0.1需要考慮一下,因爲可能按照順序分割了再shuffle打亂,丟失後半部分樣本數據。

history = model.fit(x_train, y_train, validation_split=0.1, epochs=500, 
                    batch_size=12, shuffle=True, verbose=1)

使用history獲取訓練和測試誤差,在有validation_split時候纔有測試樣本誤差。

# summarize history for loss
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.ylim(0,0.01)
plt.show()

隨機打亂數據代碼如下:

permutation = np.random.permutation(y_train.shape[0])
shuffled_x_train = x_train[permutation, :]
shuffled_y_train = y_train[permutation, :]

總結如下:

1、數據訓練前要歸一化處理。
2、對歸一化數據要立即隨機打亂操作,然後訓練。
3、隨機打亂了可以使用validation_split區分訓練數據和測試數據。

否則訓練結果很不好,以下是打亂前後的對比圖。紅色數據是隨機加噪聲,因此兩個圖隨機噪聲不一樣,但是結果還是有代表性的。

在這裏插入圖片描述在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章