Improving the autoencoder in Matlab
Show older comments
I am trying to reconstruct the handwritten letters using Matlab. I get the output but the images are too blurry. The same data set in Keras produce a much better result. One important to thing to mention is that in Keras I used cross-entropy for losses. In Matlab I cannot figure out how to change it for autoencoder (found how to do it for softmax).
% Implementation of autoencoder
% Import dataset
% Train images and lables
train_images = loadMNISTImages('train-images-idx3-ubyte');
train_labels = loadMNISTLabels('train-labels-idx1-ubyte');
% Test images and lables
test_images = loadMNISTImages('t10k-images-idx3-ubyte');
test_labels = loadMNISTLabels('t10k-labels-idx1-ubyte');
rng('default')
% rng ('shuffle')
hiddenSize1 = 16;
autoenc1 = trainAutoencoder(train_images,hiddenSize1, ...
'MaxEpochs',100, ...
'L2WeightRegularization',0.004, ...
'SparsityRegularization',4, ...
'SparsityProportion',0.15, ...
'ScaleData', false);
feat1 = encode(autoenc1,train_images);
hiddenSize2 = 8;
autoenc2 = trainAutoencoder(feat1,hiddenSize2, ...
'MaxEpochs',100, ...
'L2WeightRegularization',0.002, ...
'SparsityRegularization',4, ...
'SparsityProportion',0.1, ...
'ScaleData', false);
feat2 = encode(autoenc2,feat1);
softnet = trainSoftmaxLayer(feat2,train_labels','MaxEpochs',100,'LossFunction','crossentropy');
deepnet = stack(autoenc1,autoenc2,softnet);
deepnet = train(deepnet, train_images,train_labels');
recon = predict (autoenc1,test_images);
recon = reshape (recon, 28,28,10000);
% Reconstructed Images
figure (1);
for i = 1:10
subplot(2,5,i)
imshow(recon(:,:,i))
end


Answers (0)
Categories
Find more on Pattern Recognition in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!