TensorFlow2.0之过拟合问题实战

TensorFlow2.0之过拟合问题实战

import tensorflow as tf
from sklearn.datasets import make_moons
from sklearn.model_selection import train_test_split
from matplotlib import pyplot as plt
import seaborn as sns
import matplotlib.markers as mmarkers
import numpy as np
from tensorflow.keras import layers, Sequential, regularizers
from mpl_toolkits.mplot3d import Axes3D
from tensorflow_core.python.keras.layers import Dense

plt.rcParams['font.size'] = 16
plt.rcParams['font.family'] = ['STKaiti']
plt.rcParams['axes.unicode_minus'] = False

OUTPUT_DIR = '../images'
N_EPOCHS = 500


def load_dataset():
    # 采样点数
    N_SAMPLES = 1000
    # 测试数量比率
    TEST_SIZE = None
    # 从 moon 分布中随机采样 1000 个点,并切分为训练集-测试集
    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


def mscatter(x, y, ax=None, m=None, **kw):
    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


def make_plot(x, y, plot_name, file_name, xx=None, yy=None, preds=None, dark=False, output_dir=OUTPUT_DIR):
    # 绘制数据集的分布,x为2D坐标,y是数据点的标签
    if dark:
        plt.style.use('dark_background')
    else:
        sns.set_style("whitegrid")
    plt.figure()
    axes = plt.gca()
    axes.set_xlim([-2, 3])
    axes.set_ylim([-1.5, 2])
    axes = plt.gca()
    axes.set(xlabel="$x_1$", ylabel="$x_2$")
    plt.title(plot_name, fontsize=20, fontproperties='SimHei')
    plt.subplots_adjust(left=0.2)
    plt.subplots_adjust(left=0.8)
    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)
    # 绘制散点图,根据标签区分颜色
    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):
    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


def network_layers_influence(x_train, y_train):
    for n in range(5):
        model = Sequential()
        model.add(Dense(8, input_dim=2, activation='relu'))
        for _ in range(n):
            model.add(Dense(32, activation='sigmoid'))
            # 创建最末层
            model.add(layers.Dense(1, activation='sigmoid'))
            model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
            model.fit(x_train, y_train, epochs=N_EPOCHS, verbose=1)
            # 可视化的 x 坐标范围为[-2, 3]
            xx = np.arange(-2, 3, 0.01)
            # 可视化的 y 坐标范围为[-1.5, 2]
            yy = np.arange(-1.5, 2, 0.01)
            # 生成 x-y 平面采样网格点,方便可视化
            XX, YY = np.meshgrid(xx, yy)
            preds = model.predict_classes(np.c_[XX.ravel(), YY.ravel()])
            title = "网络层数:{0}".format(2 + n)
            file = "网络容量_%i.png" % (2 + n)
            make_plot(x_train, y_train, title, file, XX, YY, preds)


def dropout_influence(x_train, y_train):
    for n in range(5):
        model = Sequential()
        model.add(Dense(8, input_dim=2, activation='relu'))
        count = 0
        for _ in range(5):
            model.add(Dense(64, activation='relu'))
            if count < n:
                count += 1
                model.add(layers.Dropout(rate=0.5))
        model.add(Dense(1, activation='sigmoid'))
        model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
        # 绘制不同 Dropout 层数的决策边界曲线
        # 可视化的 x 坐标范围为[-2, 3]
        xx = np.arange(-2, 3, 0.01)
        # 可视化的 y 坐标范围为[-1.5, 2]
        yy = np.arange(-1.5, 2, 0.01)
        # 生成 x-y 平面采样网格点,方便可视化
        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')


def build_model_with_regularization(_lambda):
    # 创建带正则化项的神经网络
    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


def plot_weights_matrix(model, layer_index, plot_name, plot_title, file_name, output_dir):
    # 绘制权值范围函数
    # 提取指定层的权值矩阵
    weights = model.layers[layer_index].get_weights()[0]
    shape = weights.shape
    # 生成和权值矩阵等大小的网格坐标
    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')
    # 绘制权值矩阵范围
    ax.plot_surface(X, Y, weights, cmap=plt.get_cmap('rainbow'), linewidth=0)
    # 设置坐标轴名
    ax.set_xlabel('网格x坐标', fontsize=16, rotation=0, fontproperties='SimHei')
    ax.set_ylabel('网格y坐标', fontsize=16, rotation=0, fontproperties='SimHei')
    ax.set_zlabel('权值', fontsize=16, rotation=90, fontproperties='SimHei')
    # 保存矩阵范围图
    plt.savefig(output_dir + "/" + file_name + ".svg")
    plt.close(fig)


def regularizers_influence(X_train, y_train):
    for _lambda in [1e-5, 1e-3, 1e-1, 0.12, 0.13]:  # 设置不同的正则化系数
        # 创建带正则化项的模型
        model = build_model_with_regularization(_lambda)
        # 模型训练
        model.fit(X_train, y_train, epochs=N_EPOCHS, verbose=1)
        # 绘制权值范围
        layer_index = 2
        plot_title = "正则化系数:{}".format(_lambda)
        file_name = "正则化网络权值_" + str(_lambda)
        # 绘制网络权值范围图
        plot_weights_matrix(model, layer_index, plot_title, file_name, output_dir=OUTPUT_DIR + '/regularizers')
        # 绘制不同正则化系数的决策边界线
        # 可视化的 x 坐标范围为[-2, 3]
        xx = np.arange(-2, 3, 0.01)
        # 可视化的 y 坐标范围为[-1.5, 2]
        yy = np.arange(-1.5, 2, 0.01)
        # 生成 x-y 平面采样网格点,方便可视化
        XX, YY = np.meshgrid(xx, yy)
        preds = model.predict_classes(np.c_[XX.ravel(), YY.ravel()])
        title = "正则化系数:{}".format(_lambda)
        file = "正则化_%g.svg" % _lambda
        make_plot(X_train, y_train, title, file, XX, YY, preds, output_dir=OUTPUT_DIR + '/regularizers')


def main():
    x, y, x_train, x_test, y_train, y_test = load_dataset()
    # 绘制数据集分布
    # make_plot(x, y, None, "月牙形状二分类数据集分布.svg")
    # 网络层数的影响
    network_layers_influence(x_train, y_train)
    # Dropout的影响
    # dropout_influence(x_train, y_train)
    # # 正则化的影响
    # regularizers_influence(x_train, y_train)


if __name__ == '__main__':
    main()
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值