一、前言:
最近在做中文语料三分类模型,在调参这边碰到了问题,一直存在过拟合。为了找到过拟合原因,将训练状况可视化,所以对数据集重新进行了划分。
二、如何实现
1.使用sklearn划分训练集合测试集
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(combined, y, test_size=0.2)
其中combined,y是我的全部数据集,comnined是中文分词合集,y是分类集。通过y来记录combined的分类。(这一部分与要说的无关,只要知道combined合y是我要分的语料合集就可以了)。
参数test_size=0.2表示划分训练集占0.8,测试集占0.2.
2.使用模型训练里的 validation_split参数对训练集和验证集进行划分
我用的keras框架里的Sequential模型。
训练代码如下:
history = model.fit(x_train, y_train, batch_size=batch_size ,epochs=n_epoch,validation_split=0.2)
#绘图
pyplot.plot(history.history['loss'])
pyplot.plot(history.history['val_loss'])
pyplot.title('mod