原代码
def train_model(xtr, ytr, batch_size, epochs, n_val):
model = LeNet_5()
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['acc'])
tensorboard = tf.keras.callbacks.TensorBoard(histogram_freq=1)
model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_split=n_val, shuffle=True,callbacks=[tensorboard])
return model, tensorboard
训练过程中出现以下问题
Epoch 1/50
298/298 [==============================] - 361s 1s/step - loss: 2.8044 - acc: 0.5306 - val_loss: 1.1681 - val_acc: 0.0000e+00
Epoch 2/50
298/298 [==============================] - 361s 1s/step - loss: 2.2574 - acc: 0.7035 - val_loss: 0.7011 - val_acc: 0.0000e+00
……
疑问和分析
样本排布有规律,验证样本可能与训练样本差异太大,导致验证精度很差,所以需要打乱每个类别的样本排列顺序。
但是model.fit中明明设置了shuffle=True,为什么验证的精度为0(极差)?
注意modle.fit中的shuffle=True仅仅是针对于1个batch的数据进行打乱,并不针对于训练和验证样本,所以需要在训练集和验证集分配前先对样本进行一次打乱。
修改代码为
def train_model(xtr, ytr, batch_size, epochs, n_val):
model = LeNet_5()
index = [i for i in range(len(xtr))]
np.random.shuffle(index)
x_train = xtr[index]
y_train = ytr[index]
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['acc'])
tensorboard = tf.keras.callbacks.TensorBoard(histogram_freq=1)
model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_split=n_val, shuffle=True,callbacks=[tensorboard])
return model, tensorboard