分类性能评估
测量用手写体数字的 MNIST 数据库训练的数字识别器的准确度.
首先获取训练和验证数据.
In[1]:=
resource = ResourceObject["MNIST"];
trainingData = ResourceData[resource, "TrainingData"];
testData = ResourceData[resource, "TestData"];
In[2]:=
RandomSample[trainingData, 5]
Out[2]=
定义一个接受 28×28 灰度图像为输入的卷积神经网络.
In[3]:=
lenet = NetChain[{
ConvolutionLayer[20, 5], Ramp,
PoolingLayer[2, 2],
ConvolutionLayer[50, 5], Ramp,
PoolingLayer[2, 2],
FlattenLayer[],
500, Ramp, 10,
SoftmaxLayer[]},
"Output" -> NetDecoder[{"Class", Range[0, 9]}],
"Input" -> NetEncoder[{"Image", {28, 28}, "Grayscale"}]
]
Out[3]=
对网络做三轮训练.
In[4]:=
lenet = NetTrain[lenet, trainingData, ValidationSet -> testData,
MaxTrainingRounds -> 3]
Out[4]=
在从验证集上随机抽样的图像上直接运行经过训练的网络.
In[5]:=
imgs = Keys @ RandomSample[testData, 5];
Thread[imgs -> lenet[imgs]]
Out[5]=
从经过训练的网络创建一个 ClassifierMeasurements 对象和验证集.
In[6]:=
cm = ClassifierMeasurements[lenet, testData]
Out[6]=
获取网络在验证集上的精确度.
In[7]:=
cm["Accuracy"]
Out[7]=
列出被误判为 8 的 3.
In[8]:=
cm[{"Examples", 3 -> 8}]
Out[8]=
获取验证集上网络预测的混淆矩阵的图形.
In[9]:=
cm["ConfusionMatrixPlot"]
Out[9]=