Advertisement
Guest User

Untitled

a guest
Sep 17th, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 10.99 KB | None | 0 0
  1. clear
  2. clc
  3. tic
  4.  
  5. layer1size = 784;%this does not change unless there are different input images
  6. layer2size = 16;%pretty arbitrary
  7. layer3size = 16;%also fairly arbitrary
  8. layer4size = 10;%this does not change, there will always be 10 outputs for 0-9
  9. loopAmount = 5;%how many times the program should go through all the data. Each loops is ~40 minutes
  10. trainSize = 60000;%number of data points to use in training
  11. batchSize = 100;%number of data points to use in each batch
  12.  
  13. %open necessary files
  14. fileID = fopen('train-images.idx3-ubyte');
  15. trainImages = fread(fileID);
  16. fileID = fopen('train-labels.idx1-ubyte');
  17. trainLabels = fread(fileID);
  18. fileID = fopen('t10k-images.idx3-ubyte');
  19. testImages = fread(fileID);
  20. fileID = fopen('t10k-labels.idx1-ubyte');
  21. testLabels = fread(fileID);
  22.  
  23. trainImages = trainImages(17:size(trainImages,1),:);
  24. trainLabels = trainLabels(9:size(trainLabels,1),:);
  25.  
  26. %randomizes the training images
  27. trainImages2 = trainImages;
  28. randomPositions = randperm(trainSize);
  29. for index = 1:trainSize
  30.     trainImages(784*(index)-783:784*(index)) = ...
  31.         trainImages2(784*randomPositions(index)-783:784*randomPositions(index));
  32. end
  33. %reassociates the labels with the images
  34. trainLabels2 = trainLabels;
  35. for index = 1:trainSize
  36.     trainLabels(index) = trainLabels2(randomPositions(index));
  37. end
  38.  
  39. %{
  40. %viewable format
  41. testOutput = zeros((trainSize-1)*28,28);
  42. for k = 0:(trainSize-1)
  43.     for n = 1:28
  44.         for m = 1:28
  45.             testOutput(n+28*k,m) = trainImages(-27+28*n+m+k*28^2);
  46.         end
  47.     end
  48. end
  49. %}
  50. randSize = 10;
  51.  
  52. step1WeightMatrix = -randSize/2+randSize*rand(784,layer2size);
  53. step1bias = -randSize/2+randSize*rand(1,layer2size);
  54.  
  55. step2WeightMatrix = -randSize/2+randSize*rand(layer2size,layer3size);
  56. step2bias = -randSize/2+randSize*rand(1,layer3size);
  57.  
  58. step3WeightMatrix = -randSize/2+randSize*rand(layer3size,10);
  59. step3bias = -randSize/2+randSize*rand(1,10);
  60.  
  61. step3WeightAdjust = zeros(size(step3WeightMatrix,1)*batchSize,size(step3WeightMatrix,2));
  62. step3WeightAdjustAverage = zeros(size(step3WeightMatrix,1),size(step3WeightMatrix,2));
  63. step3BiasAdjust = zeros(size(step3bias,1)*batchSize,size(step3bias,2));
  64. step3BiasAdjustAverage = zeros(size(step3bias,1),size(step3bias,2));
  65. step3layer3backProp = zeros(1,layer3size);
  66.  
  67. step2WeightAdjust = zeros(size(step2WeightMatrix,1)*batchSize,size(step2WeightMatrix,2));
  68. step2WeightAdjustAverage = zeros(size(step2WeightMatrix,1),size(step2WeightMatrix,2));
  69. step2BiasAdjust = zeros(size(step2bias,1)*batchSize,size(step2bias,2));
  70. step2BiasAdjustAverage = zeros(size(step2bias,1),size(step2bias,2));
  71. step2layer2backProp = zeros(1,layer2size);
  72.  
  73. step1WeightAdjust = zeros(size(step1WeightMatrix,1)*batchSize,size(step1WeightMatrix,2));
  74. step1WeightAdjustAverage = zeros(size(step1WeightMatrix,1),size(step1WeightMatrix,2));
  75. step1BiasAdjust = zeros(size(step1bias,1)*batchSize,size(step1bias,2));
  76. step1BiasAdjustAverage = zeros(size(step1bias,1),size(step1bias,2));
  77.  
  78. cost = zeros(1,10);
  79. costTotal = 0;
  80. resultsArray = zeros(loopAmount*trainSize,2);
  81.  
  82. for n = 1:loopAmount
  83.     for trainingIndex=0:(trainSize/batchSize)-1
  84.         for batchIndex = 0:batchSize-1
  85.            
  86.             currentData(1,:) = trainImages(1+(batchIndex + trainingIndex*batchSize)*784:...
  87.                 ((batchIndex + trainingIndex*batchSize)+1)*784);%makes currentData the next up data set
  88.             currentData(1,:) = currentData(1,:)/256;%scales data down
  89.             currentLayer2 = currentData*step1WeightMatrix - step1bias; %applies weights and biases
  90.             currentLayer2 = 1./(1+exp(-currentLayer2)); %makes values 0-1 (sigmoid/logistic function)
  91.            
  92.             currentLayer3 = currentLayer2*step2WeightMatrix - step2bias; %applies weights and biases
  93.             currentLayer3 = 1./(1+exp(-currentLayer3)); %makes values 0-1
  94.            
  95.             currentLayer4 = currentLayer3*step3WeightMatrix - step3bias; %applies weights and biases
  96.             currentLayer4 = 1./(1+exp(-currentLayer4)); %makes values 0-1
  97.            
  98.             costMatrix = zeros(1,10); %find the cost of the image
  99.             costMatrix(1,trainLabels((batchIndex + trainingIndex*batchSize)+1)+1) = 1;
  100.             cost(1,:) = (currentLayer4(1,:)-costMatrix(1,:)).^2;
  101.             costTotal = sum(cost);
  102.            
  103.             %step 3 training
  104.             %weight adjustment
  105.             for columns = batchIndex*layer3size+1:batchIndex*layer3size+layer3size %1-15
  106.                 for rows = 1:layer4size%1-10
  107.                     step3WeightAdjust(columns,rows) = currentLayer3(1,columns-batchIndex*layer3size)...
  108.                         *sigmoidDerivative(currentLayer3*step3WeightMatrix(:,rows)+step3bias(rows))...
  109.                         *2*(currentLayer4(1,rows)-costMatrix(1,rows));
  110.                 end
  111.             end
  112.             %bias adjustment
  113.             for rows = 1:layer4size%1-10
  114.                 step3BiasAdjust(batchIndex+1,rows) = sigmoidDerivative(currentLayer3*step3WeightMatrix(:,rows)+step3bias(rows))...
  115.                     *2*(currentLayer4(1,rows)-costMatrix(1,rows));
  116.             end
  117.             %value adjustment
  118.             for columns = 1:layer3size
  119.                 for sums = 1:layer4size
  120.                     step3layer3backProp(1,columns) = step3layer3backProp(1,columns)+ ...
  121.                         step3WeightMatrix(columns,sums)*sigmoidDerivative(currentLayer3*step3WeightMatrix(:,sums)+step3bias(sums))...
  122.                         *2*(currentLayer4(1,sums)-costMatrix(1,sums));
  123.                 end
  124.             end
  125.             %step3layer3backProp = -step3layer3backProp;
  126.             %step 2 training
  127.             %weight adjustment
  128.             for columns = batchIndex*layer2size+1:batchIndex*layer2size+layer2size %1-20
  129.                 for rows = 1:layer3size %1-15
  130.                     step2WeightAdjust(columns,rows) = currentLayer2(1,columns-batchIndex*layer2size)...
  131.                         *sigmoidDerivative(currentLayer2*step2WeightMatrix(:,rows)+step2bias(rows))...
  132.                         *step3layer3backProp(1,rows);
  133.                 end
  134.             end
  135.             %bias adjustment
  136.             for rows = 1:layer3size%1-15
  137.                 step2BiasAdjust(batchIndex+1,rows) = sigmoidDerivative(currentLayer2...
  138.                     *step2WeightMatrix(:,rows)+step2bias(rows))*step3layer3backProp(1,rows);
  139.             end
  140.             %value adjustment
  141.             for columns = 1:layer2size%1-20
  142.                 for sums = 1:layer3size%1-15
  143.                     step2layer2backProp(1,columns) = step2layer2backProp(1,columns)+ ...
  144.                         step2WeightMatrix(columns,sums)*sigmoidDerivative(currentLayer2*step2WeightMatrix(:,sums)+step2bias(sums))...
  145.                         *step3layer3backProp(1,sums);
  146.                 end
  147.             end
  148.             %step2layer2backProp = -step2layer2backProp;
  149.             %step 1 training
  150.             %weight adjustment
  151.             for columns = batchIndex*layer1size+1:batchIndex*layer1size+layer1size %1-784
  152.                 for rows = 1:layer2size %1-20
  153.                     step1WeightAdjust(columns,rows) = currentData(1,columns-batchIndex*layer1size)...
  154.                         *sigmoidDerivative(currentData*step1WeightMatrix(:,rows)+step1bias(rows))...
  155.                         *step2layer2backProp(1,rows);
  156.                 end
  157.             end
  158.             %bias ajustment
  159.             for rows = 1:layer2size%1-20
  160.                 step1BiasAdjust(batchIndex+1,rows) = sigmoidDerivative(currentData...
  161.                     *step1WeightMatrix(:,rows)+step1bias(rows))*step2layer2backProp(1,rows);
  162.             end
  163.             %update results array
  164.             a= max(costMatrix);
  165.             b= max(currentLayer4);
  166.             if find(costMatrix == a) == find(currentLayer4 == b)
  167.                 resultsArray((n-1)*trainSize+trainingIndex*batchSize+batchIndex+1,1) = 1;
  168.             end
  169.             resultsArray((n-1)*trainSize+trainingIndex*batchSize+batchIndex+1,2) = sum(cost);
  170.         end
  171.        
  172.         %average weightadjust, biasadjust step 1
  173.         for index = 1:size(step1WeightAdjustAverage,1)
  174.             for matrixIterator = 0:batchSize-1
  175.                 step1WeightAdjustAverage(index,:) = step1WeightAdjustAverage(index,:)+...
  176.                     step1WeightAdjust(index+matrixIterator*size(step1WeightAdjustAverage,1),:);
  177.             end
  178.         end
  179.         step1WeightAdjustAverage = step1WeightAdjustAverage./batchSize;
  180.         for index = 1:size(step1BiasAdjustAverage,1)
  181.             for matrixIterator = 0:batchSize-1
  182.                 step1BiasAdjustAverage(index,:) = step1BiasAdjustAverage(index,:)+...
  183.                     step1BiasAdjust(index+matrixIterator*size(step1BiasAdjustAverage,1),:);
  184.             end
  185.         end
  186.         step1BiasAdjustAverage = step1BiasAdjustAverage./batchSize;
  187.         %average weightadjust, biasadjust step 2
  188.         for index = 1:size(step2WeightAdjustAverage,1)
  189.             for matrixIterator = 0:batchSize-1
  190.                 step2WeightAdjustAverage(index,:) = step2WeightAdjustAverage(index,:)+...
  191.                     step2WeightAdjust(index+matrixIterator*size(step2WeightAdjustAverage,1),:);
  192.             end
  193.         end
  194.         step2WeightAdjustAverage = step2WeightAdjustAverage./batchSize;
  195.         for index = 1:size(step2BiasAdjustAverage,1)
  196.             for matrixIterator = 0:batchSize-1
  197.                 step2BiasAdjustAverage(index,:) = step2BiasAdjustAverage(index,:)+...
  198.                     step2BiasAdjust(index+matrixIterator*size(step2BiasAdjustAverage,1),:);
  199.             end
  200.         end
  201.         step2BiasAdjustAverage = step2BiasAdjustAverage./batchSize;
  202.         %average weightadjust, biasadjust step 3
  203.         for index = 1:size(step3WeightAdjustAverage,1)
  204.             for matrixIterator = 0:batchSize-1
  205.                 step3WeightAdjustAverage(index,:) = step3WeightAdjustAverage(index,:)+...
  206.                     step3WeightAdjust(index+matrixIterator*size(step3WeightAdjustAverage,1),:);
  207.             end
  208.         end
  209.         step3WeightAdjustAverage = step3WeightAdjustAverage./batchSize;
  210.         for index = 1:size(step3BiasAdjustAverage,1)
  211.             for matrixIterator = 0:batchSize-1
  212.                 step3BiasAdjustAverage(index,:) = step3BiasAdjustAverage(index,:)+...
  213.                     step3BiasAdjust(index+matrixIterator*size(step3BiasAdjustAverage,1),:);
  214.             end
  215.         end
  216.         step3BiasAdjustAverage = step3BiasAdjustAverage./batchSize;
  217.         %update biases, weights
  218.         step1bias = step1bias - step1BiasAdjustAverage;
  219.         step1WeightMatrix = step1WeightMatrix - step1WeightAdjustAverage;
  220.        
  221.         step2bias = step2bias - step2BiasAdjustAverage;
  222.         step2WeightMatrix = step2WeightMatrix - step2WeightAdjustAverage;
  223.        
  224.         step3bias = step3bias - step3BiasAdjustAverage;
  225.         step3WeightMatrix = step3WeightMatrix - step3WeightAdjustAverage;
  226.        
  227.     end
  228. end
  229. toc
  230.  
  231. function y = sigmoidDerivative(x)
  232. z = exp(-x)./(1+exp(-x)).^2;
  233. if isnan(z)
  234.     y = 0;%arbitrary fix if 0/0 (nan)
  235. elseif z < 10^(-7) && z > -10^(-7)
  236.     y = 0;
  237. else
  238.     y = z;
  239. end
  240. end
  241.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement