1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
| function [y]=ReLU(x) //RELU FUNCTION
y= max(0,x)
endfunction
function [y]= Softmax(x) //SOFTMAX FUNCTION
ex = exp(x);
y = ex/sum(ex);
endfunction
function [y] = Pool(x) // POOLING FUNCTION
[xrow,xcol,numFilters] = size(x);
y= zeros (xrow/2,xcol/2,numFilters);
for k=1:numFilters
filt = ones(2,2)/(2*2)
image = conv2(x(:,:,k),filt, 'valid');
[irow,icol,numFilters] = size(image);
y(:,:,k) = image (1:2:irow,1:2:icol)
end
endfunction
function [y] = Convo(x,W) // CONVOLUTION FUNCTION
[wrow,wcol,numFilters] = size(W);
[xrow,xcol] = size(x);
yrow = xrow-wrow+1;
ycol = xcol-wcol+1;
y = zeros (yrow,ycol,numFilters);
for k = 1:numFilters
filt = W(:,:,k);
//disp (filt)
filt = filt(wrow:-1:1,wcol:-1:1)
//disp(size(y))
y(:,:,k) = conv2 (x,filt,'valid')
end
end
function images = loadMNISTImages(filename) //MNIST DATASET IMAGES LOADING FUNCTION
fd = mopen(filename, 'r')
magic = mget(1, 'ib', fd) //Be sure to use the right name and path to the file
numImages = mget(1, 'ib', fd)
numRows = mget(1, 'ib', fd)
numCols = mget(1, 'ib', fd)
images = mgeti(numImages*numRows*numCols, 'uc', fd)
mclose(fd)
images = matrix(images, numCols, numRows, numImages)
images = permute(images, [2 1 3])
images = matrix(images,size(images, 1),size(images, 2),size(images, 3))
images = double(images)/255
endfunction
function labels= loadMNISTLabels(filename) // MNIST DATASET LABELS LOADING FUNCTION
fd = mopen (filename,'r')
magic = mget(1, 'ib', fd) //Be sure to use the right name and path to the file
numLabels = mget(1, 'ib', fd)
labels = mget (numLabels,'uc',fd)
mclose(fd)
endfunction
function [W1,W5,Wo]= MNISTConv (W1,W5, Wo, X, D) //TRAINING FUNCTION
alpha = 0.01;
bet = 0.95;
momentum1 = zeros (9,9,20);
momentum5 = zeros (100,2000);
momentumo = zeros (10,100);
N = length(D);
bsize=100; //setting a batch size
blist=1:bsize:(N-bsize+1); //finding all the starting indice of the different batchs
for batch = 1:length(blist)
disp(batch) //keeping the user updated about the number of the current batch
dW1 = zeros(9,9,20);
dW5 = zeros(100,2000);
dWo = zeros(10,100);
begin = blist(batch);
for k= begin:begin+bsize-1 //propagation
x = X(:,:,k);
y1 = Convo(x,W1);
y2 = ReLU (y1);
y3 = Pool (y2);
y4 = matrix (y3,-1,1);
v5 = W5*y4;
y5 = ReLU(v5);
v = Wo*y5;
y = Softmax(v);
d = zeros (10,1); //comparing to expected result
d(sub2ind(size(d),D(k),1))=1;
e=d-y; //calculating error vector
delta =e;
e5= Wo'*delta;
delta5 = (y5 > 0) .*e5; //backpropagation
e4 = W5'*delta5;
e3 = matrix(e4,size(y3));
e2 = zeros(20,20,20);
W3 = ones (20,20,20)/(2*2);
for c = 1:20
e2(:,:,c) = e3(:,:,c).*.ones(2,2)
e2(:,:,c) = e2(:,:,c).*W3(:,:,c);
end
delta2 = (y2 > 0).*e2;
delta1_x = zeros(9,9,20);
rotdelta2= delta2(20:-1:1,20:-1:1,:)
for c=1:20
delta1_x(:,:,c)= conv2(x(:,:), rotdelta2(:,:,c),'valid');
end
dW1 = dW1 + delta1_x; //saving the error vector in an error "batch pool"
dW5 = dW5 + delta5*y4';
dWo = dWo + delta *y5';
end
dW1 = dW1 / bsize; //creating a "batch error vector"
dW5 = dW5 / bsize;
dWo = dWo / bsize;
momentum1 = alpha*dW1 + bet*momentum1; //applying it
W1 = W1+ momentum1;
momentum5 = alpha*dW5 + bet*momentum5;
W5 = W5+ momentum5;
momentumo = alpha*dWo + bet*momentumo;
Wo = Wo+ momentumo;
end
endfunction
//PROGRAM
Images = loadMNISTImages('t10k-images.idx3-ubyte'); //Loading training images
Images = matrix (Images,28,28,-1);
Labels = loadMNISTLabels('t10k-labels.idx1-ubyte') //Loading traininn images labels
Labels (Labels == 0) = 10
W1 = 2*rand(9,9,20)-1 //Randomly asigning wheight to convolution filters, and Neural connection
W5= (2*rand(100,2000)-1) * sqrt(6)/sqrt(360+2000);
Wo = (2*rand(10,100) -1) * sqrt(6)/sqrt(10 + 100);
X = Images(:,:, 1:8000); //Selecting the first 8000 images for training
D = Labels(1:8000);
for epoch =1:3
disp('\\\\\',epoch, '/////') //Training the network by using the training function.
[W1,W5,Wo] = MNISTConv (W1,W5,Wo,X,D); //Inputs are the weigths, Images and Labels,
//Output are the new weights, that we will send again in the next epoch, or test.
end
X = Images(:,:, 8001:10000); //Selecting the last 2000 images we didn't use to use them as a test for the network
D = Labels(8001:10000); //Same for the labels
acc = 0;
N = length(D);
disp('test') //Beginning of the test
for k= 1:N
x = X(:,:,k); //Propagation
y1 = Convo(x,W1);
y2 = ReLU(y1);
y3 = Pool (y2);
y4 = matrix (y3, -1, 1);
v5 = W5*y4;
y5 = ReLU(v5);
v = Wo*y5;
y = Softmax (v);
[m,i ]= max(y); //Calculating accuracy
if i == D(k)
acc = acc+1;
end
end
acc = acc/N;
disp ('accuracy is') //Dysplaying accuracy
disp (acc); |
Partager