詳解tensorflow之過擬合問題實戰(zhàn)
過擬合問題實戰(zhàn)
1.構(gòu)建數(shù)據(jù)集
我們使用的數(shù)據(jù)集樣本特性向量長度為 2,標(biāo)簽為 0 或 1,分別代表了 2 種類別。借助于 scikit-learn 庫中提供的 make_moons 工具我們可以生成任意多數(shù)據(jù)的訓(xùn)練集。
import matplotlib.pyplot as plt # 導(dǎo)入數(shù)據(jù)集生成工具 import numpy as np import seaborn as sns from sklearn.datasets import make_moons from sklearn.model_selection import train_test_split from tensorflow.keras import layers, Sequential, regularizers from mpl_toolkits.mplot3d import Axes3D
為了演示過擬合現(xiàn)象,我們只采樣了 1000 個樣本數(shù)據(jù),同時添加標(biāo)準(zhǔn)差為 0.25 的高斯噪聲數(shù)據(jù):
def load_dataset(): # 采樣點數(shù) N_SAMPLES = 1000 # 測試數(shù)量比率 TEST_SIZE = None # 從 moon 分布中隨機采樣 1000 個點,并切分為訓(xùn)練集-測試集 X, y = make_moons(n_samples=N_SAMPLES, noise=0.25, random_state=100) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=TEST_SIZE, random_state=42) return X, y, X_train, X_test, y_train, y_test
make_plot 函數(shù)可以方便地根據(jù)樣本的坐標(biāo) X 和樣本的標(biāo)簽 y 繪制出數(shù)據(jù)的分布圖:
def make_plot(X, y, plot_name, file_name, XX=None, YY=None, preds=None, dark=False, output_dir=OUTPUT_DIR): # 繪制數(shù)據(jù)集的分布, X 為 2D 坐標(biāo), y 為數(shù)據(jù)點的標(biāo)簽 if dark: plt.style.use('dark_background') else: sns.set_style("whitegrid") axes = plt.gca() axes.set_xlim([-2, 3]) axes.set_ylim([-1.5, 2]) axes.set(xlabel="$x_1$", ylabel="$x_2$") plt.title(plot_name, fontsize=20, fontproperties='SimHei') plt.subplots_adjust(left=0.20) plt.subplots_adjust(right=0.80) if XX is not None and YY is not None and preds is not None: plt.contourf(XX, YY, preds.reshape(XX.shape), 25, alpha=0.08, cmap=plt.cm.Spectral) plt.contour(XX, YY, preds.reshape(XX.shape), levels=[.5], cmap="Greys", vmin=0, vmax=.6) # 繪制散點圖,根據(jù)標(biāo)簽區(qū)分顏色m=markers markers = ['o' if i == 1 else 's' for i in y.ravel()] mscatter(X[:, 0], X[:, 1], c=y.ravel(), s=20, cmap=plt.cm.Spectral, edgecolors='none', m=markers, ax=axes) # 保存矢量圖 plt.savefig(output_dir + '/' + file_name) plt.close()
def mscatter(x, y, ax=None, m=None, **kw): import matplotlib.markers as mmarkers if not ax: ax = plt.gca() sc = ax.scatter(x, y, **kw) if (m is not None) and (len(m) == len(x)): paths = [] for marker in m: if isinstance(marker, mmarkers.MarkerStyle): marker_obj = marker else: marker_obj = mmarkers.MarkerStyle(marker) path = marker_obj.get_path().transformed( marker_obj.get_transform()) paths.append(path) sc.set_paths(paths) return sc
X, y, X_train, X_test, y_train, y_test = load_dataset() make_plot(X,y,"haha",'月牙形狀二分類數(shù)據(jù)集分布.svg')
2.網(wǎng)絡(luò)層數(shù)的影響
為了探討不同的網(wǎng)絡(luò)深度下的過擬合程度,我們共進行了 5 次訓(xùn)練實驗。在𝑛 ∈ [0,4]時,構(gòu)建網(wǎng)絡(luò)層數(shù)為n + 2層的全連接層網(wǎng)絡(luò),并通過 Adam 優(yōu)化器訓(xùn)練 500 個 Epoch
def network_layers_influence(X_train, y_train): # 構(gòu)建 5 種不同層數(shù)的網(wǎng)絡(luò) for n in range(5): # 創(chuàng)建容器 model = Sequential() # 創(chuàng)建第一層 model.add(layers.Dense(8, input_dim=2, activation='relu')) # 添加 n 層,共 n+2 層 for _ in range(n): model.add(layers.Dense(32, activation='relu')) # 創(chuàng)建最末層 model.add(layers.Dense(1, activation='sigmoid')) # 模型裝配與訓(xùn)練 model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) model.fit(X_train, y_train, epochs=N_EPOCHS, verbose=1) # 繪制不同層數(shù)的網(wǎng)絡(luò)決策邊界曲線 # 可視化的 x 坐標(biāo)范圍為[-2, 3] xx = np.arange(-2, 3, 0.01) # 可視化的 y 坐標(biāo)范圍為[-1.5, 2] yy = np.arange(-1.5, 2, 0.01) # 生成 x-y 平面采樣網(wǎng)格點,方便可視化 XX, YY = np.meshgrid(xx, yy) preds = model.predict_classes(np.c_[XX.ravel(), YY.ravel()]) print(preds) title = "網(wǎng)絡(luò)層數(shù):{0}".format(2 + n) file = "網(wǎng)絡(luò)容量_%i.png" % (2 + n) make_plot(X_train, y_train, title, file, XX, YY, preds, output_dir=OUTPUT_DIR + '/network_layers')
3.Dropout的影響
為了探討 Dropout 層對網(wǎng)絡(luò)訓(xùn)練的影響,我們共進行了 5 次實驗,每次實驗使用 7 層的全連接層網(wǎng)絡(luò)進行訓(xùn)練,但是在全連接層中間隔插入 0~4 個 Dropout 層并通過 Adam優(yōu)化器訓(xùn)練 500 個 Epoch
def dropout_influence(X_train, y_train): # 構(gòu)建 5 種不同數(shù)量 Dropout 層的網(wǎng)絡(luò) for n in range(5): # 創(chuàng)建容器 model = Sequential() # 創(chuàng)建第一層 model.add(layers.Dense(8, input_dim=2, activation='relu')) counter = 0 # 網(wǎng)絡(luò)層數(shù)固定為 5 for _ in range(5): model.add(layers.Dense(64, activation='relu')) # 添加 n 個 Dropout 層 if counter < n: counter += 1 model.add(layers.Dropout(rate=0.5)) # 輸出層 model.add(layers.Dense(1, activation='sigmoid')) # 模型裝配 model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) # 訓(xùn)練 model.fit(X_train, y_train, epochs=N_EPOCHS, verbose=1) # 繪制不同 Dropout 層數(shù)的決策邊界曲線 # 可視化的 x 坐標(biāo)范圍為[-2, 3] xx = np.arange(-2, 3, 0.01) # 可視化的 y 坐標(biāo)范圍為[-1.5, 2] yy = np.arange(-1.5, 2, 0.01) # 生成 x-y 平面采樣網(wǎng)格點,方便可視化 XX, YY = np.meshgrid(xx, yy) preds = model.predict_classes(np.c_[XX.ravel(), YY.ravel()]) title = "無Dropout層" if n == 0 else "{0}層 Dropout層".format(n) file = "Dropout_%i.png" % n make_plot(X_train, y_train, title, file, XX, YY, preds, output_dir=OUTPUT_DIR + '/dropout')
4.正則化的影響
為了探討正則化系數(shù)𝜆對網(wǎng)絡(luò)模型訓(xùn)練的影響,我們采用 L2 正則化方式,構(gòu)建了 5 層的神經(jīng)網(wǎng)絡(luò),其中第 2,3,4 層神經(jīng)網(wǎng)絡(luò)層的權(quán)值張量 W 均添加 L2 正則化約束項:
def build_model_with_regularization(_lambda): # 創(chuàng)建帶正則化項的神經(jīng)網(wǎng)絡(luò) model = Sequential() model.add(layers.Dense(8, input_dim=2, activation='relu')) # 不帶正則化項 # 2-4層均是帶 L2 正則化項 model.add(layers.Dense(256, activation='relu', kernel_regularizer=regularizers.l2(_lambda))) model.add(layers.Dense(256, activation='relu', kernel_regularizer=regularizers.l2(_lambda))) model.add(layers.Dense(256, activation='relu', kernel_regularizer=regularizers.l2(_lambda))) # 輸出層 model.add(layers.Dense(1, activation='sigmoid')) model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) # 模型裝配 return model
下面我們首先來實現(xiàn)一個權(quán)重可視化的函數(shù)
def plot_weights_matrix(model, layer_index, plot_name, file_name, output_dir=OUTPUT_DIR): # 繪制權(quán)值范圍函數(shù) # 提取指定層的權(quán)值矩陣 weights = model.layers[layer_index].get_weights()[0] shape = weights.shape # 生成和權(quán)值矩陣等大小的網(wǎng)格坐標(biāo) X = np.array(range(shape[1])) Y = np.array(range(shape[0])) X, Y = np.meshgrid(X, Y) # 繪制3D圖 fig = plt.figure() ax = fig.gca(projection='3d') ax.xaxis.set_pane_color((1.0, 1.0, 1.0, 0.0)) ax.yaxis.set_pane_color((1.0, 1.0, 1.0, 0.0)) ax.zaxis.set_pane_color((1.0, 1.0, 1.0, 0.0)) plt.title(plot_name, fontsize=20, fontproperties='SimHei') # 繪制權(quán)值矩陣范圍 ax.plot_surface(X, Y, weights, cmap=plt.get_cmap('rainbow'), linewidth=0) # 設(shè)置坐標(biāo)軸名 ax.set_xlabel('網(wǎng)格x坐標(biāo)', fontsize=16, rotation=0, fontproperties='SimHei') ax.set_ylabel('網(wǎng)格y坐標(biāo)', fontsize=16, rotation=0, fontproperties='SimHei') ax.set_zlabel('權(quán)值', fontsize=16, rotation=90, fontproperties='SimHei') # 保存矩陣范圍圖 plt.savefig(output_dir + "/" + file_name + ".svg") plt.close(fig)
在保持網(wǎng)絡(luò)結(jié)構(gòu)不變的條件下,我們通過調(diào)節(jié)正則化系數(shù) 𝜆 = 0.00001,0.001,0.1,0.12,0.13
來測試網(wǎng)絡(luò)的訓(xùn)練效果,并繪制出學(xué)習(xí)模型在訓(xùn)練集上的決策邊界曲線
def regularizers_influence(X_train, y_train): for _lambda in [1e-5, 1e-3, 1e-1, 0.12, 0.13]: # 設(shè)置不同的正則化系數(shù) # 創(chuàng)建帶正則化項的模型 model = build_model_with_regularization(_lambda) # 模型訓(xùn)練 model.fit(X_train, y_train, epochs=N_EPOCHS, verbose=1) # 繪制權(quán)值范圍 layer_index = 2 plot_title = "正則化系數(shù):{}".format(_lambda) file_name = "正則化網(wǎng)絡(luò)權(quán)值_" + str(_lambda) # 繪制網(wǎng)絡(luò)權(quán)值范圍圖 plot_weights_matrix(model, layer_index, plot_title, file_name, output_dir=OUTPUT_DIR + '/regularizers') # 繪制不同正則化系數(shù)的決策邊界線 # 可視化的 x 坐標(biāo)范圍為[-2, 3] xx = np.arange(-2, 3, 0.01) # 可視化的 y 坐標(biāo)范圍為[-1.5, 2] yy = np.arange(-1.5, 2, 0.01) # 生成 x-y 平面采樣網(wǎng)格點,方便可視化 XX, YY = np.meshgrid(xx, yy) preds = model.predict_classes(np.c_[XX.ravel(), YY.ravel()]) title = "正則化系數(shù):{}".format(_lambda) file = "正則化_%g.svg" % _lambda make_plot(X_train, y_train, title, file, XX, YY, preds, output_dir=OUTPUT_DIR + '/regularizers')
regularizers_influence(X_train, y_train)
到此這篇關(guān)于詳解tensorflow之過擬合問題實戰(zhàn)的文章就介紹到這了,更多相關(guān)tensorflow 過擬合內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于K.image_data_format() == ''channels_first'' 的理解
這篇文章主要介紹了基于K.image_data_format() == 'channels_first' 的理解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06PyQt5 關(guān)于Qt Designer的初步應(yīng)用和打包過程詳解
Qt Designer中的操作方式十分靈活,其通過拖拽的方式放置控件可以隨時查看控件效果。這篇文章主要介紹了PyQt5 關(guān)于Qt Designer的初步應(yīng)用和打包,需要的朋友可以參考下2021-09-09Python wxPython庫使用wx.ListBox創(chuàng)建列表框示例
這篇文章主要介紹了Python wxPython庫使用wx.ListBox創(chuàng)建列表框,結(jié)合實例形式分析了wxPython庫使用wx.ListBox創(chuàng)建列表框的簡單實現(xiàn)方法及ListBox函數(shù)相關(guān)選項的功能,需要的朋友可以參考下2018-09-09Pytorch中關(guān)于model.eval()的作用及分析
這篇文章主要介紹了Pytorch中關(guān)于model.eval()的作用及分析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02