【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区分训练数据和测试数据。

否则训练结果很不好,以下是打乱前后的对比图。红色数据是随机加噪声,因此两个图随机噪声不一样,但是结果还是有代表性的。

在这里插入图片描述在这里插入图片描述

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