【小白学习keras教程】二、基于CIFAR-10数据集训练简单的MLP分类模型
「@Author:Runsen」
分类任务的MLP
当目标(「y」)是离散的(分类的) 对于损失函数,使用交叉熵;对于评估指标,通常使用accuracy
数据集描述
CIFAR-10数据集包含10个类中的60000个图像—50000个用于培训,10000个用于测试 有关更多信息,请参阅官方文档
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.utils import to_categorical
# load data and flatten X data to fit into MLP
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
x_train, x_test = x_train.reshape(x_train.shape[0], -1), x_test.reshape(x_test.shape[0], -1)
y_train, y_test = to_categorical(y_train), to_categorical(y_test)
print(x_train.shape, x_test.shape, y_train.shape, y_test.shape)
1.创建模型
与回归模型相同-使用 Sequentia()
model = Sequential()
1-1.添加层
Keras层可以「添加」到模型中 添加层就像一个接一个地堆叠乐高积木 应注意的是,由于这是一个分类问题,应添加sigmoid层(针对多类问题的softmax) 文档:https://keras.io/layers/core/
# Keras model with two hidden layer with 10 neurons each
model.add(Dense(50, input_shape = (x_train.shape[-1],))) # Input layer => input_shape should be explicitly designated
model.add(Activation('sigmoid'))
model.add(Dense(50)) # Hidden layer => only output dimension should be designated
model.add(Activation('sigmoid'))
model.add(Dense(50)) # Hidden layer => only output dimension should be designated
model.add(Activation('sigmoid'))
model.add(Dense(10)) # Output layer => output dimension = 1 since it is regression problem
model.add(Activation('sigmoid'))
# This is equivalent to the above code block
model.add(Dense(50, input_shape = (x_train.shape[-1],), activation = 'sigmoid'))
model.add(Dense(50, activation = 'sigmoid'))
model.add(Dense(50, activation = 'sigmoid'))
model.add(Dense(10, activation = 'sigmoid'))
1-2.模型编译
Keras模型应在培训前“编译” 应指定损失类型(函数)和优化器 文档(优化器):https://keras.io/optimizers/ 文档(损失):https://keras.io/losses/
from tensorflow.keras import optimizers
sgd = optimizers.SGD(lr = 0.01) # stochastic gradient descent optimizer
model.compile(optimizer = sgd, loss = 'categorical_crossentropy', metrics = ['accuracy'])
模型摘要
model.summary()
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense (Dense) (None, 50) 153650
_________________________________________________________________
activation (Activation) (None, 50) 0
_________________________________________________________________
dense_1 (Dense) (None, 50) 2550
_________________________________________________________________
activation_1 (Activation) (None, 50) 0
_________________________________________________________________
dense_2 (Dense) (None, 50) 2550
_________________________________________________________________
activation_2 (Activation) (None, 50) 0
_________________________________________________________________
dense_3 (Dense) (None, 10) 510
_________________________________________________________________
activation_3 (Activation) (None, 10) 0
=================================================================
Total params: 159,260
Trainable params: 159,260
Non-trainable params: 0
_________________________________________________________________
2.训练
使用提供的训练数据训练模型
model.fit(x_train, y_train, batch_size = 128, epochs = 50, verbose = 1)
3.评估
Keras模型可以用 evaluate()
函数计算文档:https://keras.io/metrics/
results = model.evaluate(x_test, y_test)
print(model.metrics_names) # list of metric names the model is employing
print(results) # actual figure of metrics computed
评论