在上篇博文(http://blog.csdn.net/freeliao/article/details/19424565)介绍了Softmax Regression的模型,现在来做下该模型在MNIST数据集上的识别练习(http://ufldl.stanford.edu/wiki/index.php/Exercise:Softmax_Regression)。MNIST数据集训练集由60000张28*28的图片组成,测试集由10000张同样大小的图片组成。
这里的Softmax Regression没有隐含层,直接以图片的像素值作为输入,得到输出层,输出层有10个单元,每个单元代表输入属于该类别的概率。
回顾一下模型的损失函数和梯度:
matlab实现过程中要注意用Vectorized的实现方式,尽量避免出现构造过大的矩阵,否则容易出现out of memory错误。
实现结果准确率为92.640%。
softmaxExercise.m
%% CS294A/CS294W Softmax Exercise
% Instructions
% ------------
%
% This file contains code that helps you get started on the
% softmax exercise. You will need to write the softmax cost function
% in softmaxCost.m and the softmax prediction function in softmaxPred.m.
% For this exercise, you will not need to change any code in this file,
% or any other files other than those mentioned above.
% (However, you may be required to do so in later exercises)
%%======================================================================
%% STEP 0: Initialise constants and parameters
%
% Here we define and initialise some constants which allow your code
% to be used more generall