回答思路
神经网络参数量巨大,理论上可以拟合任何函数,但正因为神经网络如此强大的学习能力,而现实生活中的数据通常是包含噪声的,这也导致经常出现过拟合的问题:
当模型试着预测噪声较多的数据的趋势时,由于模型参数过多、过于复杂,就会导致过拟合。过拟合的模型通常是不精确的,因为这样的预测趋势并不会反映数据的真实情况。
也就是说,我们训练出的模型在已知的数据(训练集)中有好的预测结果,但在未知的数据(测试集)中表现较差。
横坐标是模型复杂度,纵坐标是模型性能,可以看到,随着模型复杂度的增加,训练准确度越来越高(蓝色);然而,在评估集的性能却会先提高再降低。
这就说明模型出现了过拟合:训练集上很准确,但测试集上效果很差,这是需要避免的。
可视化代码
#演示过拟合import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 1, 1000) y1 = -(x - 0.5) ** 2 y2 = y1 - 0.33 + np.exp(x - 1) fig, ax = plt.subplots() ax.plot(x, y2, lw=10, alpha=0.5, color='blue') ax.plot(x, y1, lw=10, alpha=0.5, color='red') ax.text(0.15, 0.2, "training score", rotation=45, size=16, color='blue') ax.text(0.2, -0.05, "validation score", rotation=20, size=16, color='red') ax.text(0.02, 0.1, r'$\longleftarrow$ High Bias', size=18, rotation=90, va='center') ax.text(0.98, 0.1, r'$\longleftarrow$ High Variance $\longrightarrow$', size=18, rotation=90, ha='right', va='center') ax.text(0.48, -0.12, 'Best$\\longrightarrow$\nModel', size=18, rotation=90, va='center') ax.set_xlim(0, 1) ax.set_ylim(-0.3, 0.5) ax.set_xlabel(r'model complexity $\longrightarrow$', size=14) ax.set_ylabel(r'model score $\longrightarrow$', size=14) ax.xaxis.set_major_formatter(plt.NullFormatter()) ax.yaxis.set_major_formatter(plt.NullFormatter()) ax.set_title("Validation Curve Schematic", size=16) # fig.savefig('validation-curve.png') plt.show()