mnist fashion ffnew in matlab

Here's an example code to train a feedforward neural network using the MNIST Fashion dataset in MATLAB:

main.m
% Load the MNIST Fashion dataset
[fashionTrainImages, fashionTrainLabels] = mnist_reader('fashion/train-images-idx3-ubyte', 'fashion/train-labels-idx1-ubyte');
[fashionTestImages, fashionTestLabels] = mnist_reader('fashion/t10k-images-idx3-ubyte', 'fashion/t10k-labels-idx1-ubyte');

% Initialize the parameters for the neural network
inputSize = size(fashionTrainImages, 2);
hiddenSize = 100;
numClasses = 10;
lambda = 1e-4;
alpha = 0.01;

% Train the feedforward neural network
options = optimset('MaxIter', 50);
theta = randInitializeWeights(inputSize, hiddenSize, numClasses);
[theta, cost] = fmincg(@(t)(nnCostFunction(t, inputSize, hiddenSize, numClasses, fashionTrainImages, fashionTrainLabels, lambda, alpha)), theta(:), options);

% Make predictions on the test set
[pred, prob] = predict(theta, inputSize, hiddenSize, numClasses, fashionTestImages);

% Calculate the accuracy of the predictions
accuracy = mean(double(pred == fashionTestLabels)) * 100;
fprintf('Accuracy: %g%%\n', accuracy);
1002 chars
23 lines

Note that this code uses the mnist_reader function which can be downloaded from the MATLAB File Exchange. Additionally, the nnCostFunction and randInitializeWeights are helper functions used in the training process. These functions can be found in the ex4 folder of the Stanford Machine Learning course on Coursera.

gistlibby LogSnag