接着上一部分做模型的训练和优化。
#转化为ont-hot 独热编码
train_label = tf.keras.utils.to_categorical(train_label,num_classes=10,dtype='int')
val_label = tf.keras.utils.to_categorical(val_label,num_classes=10,dtype='int')
test_label = tf.keras.utils.to_categorical(test_label,num_classes=10,dtype='int')
#加载训练集数据并打乱,设定batch-size
train_dataset = tf.data.Dataset.from_tensor_slices((train_,train_label))
train_dataset = train_dataset.prefetch(buffer_size = tf.data.experimental.AUTOTUNE)
train_dataset = train_dataset.shuffle(buffer_size = 23000)
train_dataset = train_dataset.batch(batch_size=128)
#加载验证集数据并打乱,设定batch-size
val_dataset = tf.data.Dataset.from_tensor_slices((val_,val_label))
val_dataset = val_dataset.prefetch(tf.data.experimental.AUTOTUNE)
val_dataset = val_dataset.shuffle(buffer_size=23000)
val_dataset = val_dataset.batch(batch_size=256)
#设置学习率
learning_rate = 0.001
#设置损失函数
loss_object = tf.keras.losses.CategoricalCrossentropy()
#设置优化器
optimizer = tf.keras.optimizers.Adam(learning_rate=learning_rate)
#训练集评估指标
train_loss = tf.keras.metrics.Mean(name='train_loss')
train_accuracy = tf.keras.metrics.CategoricalAccuracy(name='train_accuracy')
#测试集评估指标
test_loss = tf.keras.metrics.Mean(name='test_loss')
test_accuracy = tf.keras.metrics.CategoricalAccuracy(name='test_accuracy')
#下边是自定义训练的两个函数,之间有过讲解,这里不再说明
#mini-batch
def train_one_step(contents, labels):
with tf.GradientTape() as tape:
predictions = model(contents)
loss = loss_object(labels, predictions)
gradients = tape.gradient(loss, model.trainable_variables)
optimizer.apply_gradients(zip(gradients, model.trainable_variables))
train_loss(loss) #update
train_accuracy(labels, predictions)#update
def test_one_step(contents, labels):
predictions = model(contents)
t_loss = loss_object(labels, predictions)
test_loss(t_loss)
test_accuracy(labels, predictions)
#设定10个周期 开始训练
EPOCHS=10
for epoch in range(EPOCHS):
# 在下一个epoch开始时,重置评估指标
train_loss.reset_states()
train_accuracy.reset_states()
test_loss.reset_states()
test_accuracy.reset_states()
for content, labels in train_dataset:
train_one_step(content, labels) #mini-batch 更新
for val_content, val_labels in val_dataset:
test_one_step(val_content, val_labels)
template = 'Epoch {}, Loss: {}, Accuracy: {}, Test Loss: {}, Test Accuracy: {}'
print(template.format(epoch + 1,
train_loss.result(),
train_accuracy.result() * 100,
test_loss.result(),
test_accuracy.result() * 100
))