Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. lose all;
  2. clear all;
  3.  
  4. mnistImgs = loadMNISTImages('../../../Database/MNIST/train-images.idx3-ubyte');
  5. mnistLabels = loadMNISTLabels('../../../Database/MNIST/train-labels.idx1-ubyte');
  6.  
  7. nDigits = 10;
  8. labelsLen = length(mnistLabels);
  9.  
  10. % Transform the labels to correct target values.
  11. targetValues = zeros(nDigits, labelsLen);
  12. for n = 1 : labelsLen
  13. % +1 to avoid 0 index
  14. targetValues(mnistLabels(n) + 1, n) = 1;
  15. end;
  16.  
  17. nNeurons = 20;
  18. batchSize = 100;
  19. epochs = 500;
  20. learningRate = 0.1;
  21. activFunction = @logisticSigmoid;
  22. dActivFunction = @dLogisticSigmoid;
  23.  
  24. fprintf('Number of neurons: %d\n', nNeurons);
  25. fprintf('Number of Epochs: %d\n', epochs);
  26. fprintf('Batch number: %d\n', batchSize);
  27. fprintf('Learning rate: %d\n', learningRate);
  28.  
  29. [hiddenWeights, ...
  30. outputWeights, ...
  31. error] = trainStochasticSquaredErrorTwoLayerPerceptron(activFunction,
  32. dActivFunction,
  33. nNeurons,
  34. mnistImgs,
  35. targetValues,
  36. epochs,
  37. batchSize,
  38. learningRate);
  39.  
  40. toFile(hiddenWeights, "hiddenweights");
  41.  
  42. toFile(outputWeights, "outputweights");
  43.  
  44. # NOWE
  45.  
  46. labels = [0,1,2,3,4,5,6,7,8,9];
  47.  
  48. predicted_labels = zeros(1, length(digit_data_base));
  49.  
  50. for idx_img in range(length(digit_data_base)):
  51. inputVector = digit_data_base(idx_img)
  52. hiddenActualInput = hiddenWeights * inputVector;
  53. hiddenOutputVector = activFunction(hiddenActualInput);
  54.  
  55. outputActualInput = outputWeights * hiddenOutputVector;
  56. outputVector = activFunction(outputActualInput);
  57.  
  58. predicted_labels(idx_img) = labels(idx_from_max(outputVector))
  59.  
  60.  
  61. cnf_matrix = confusion_matrix(predicted_labels , mnistLabels )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement