├── .gitignore ├── Deep Belief Networks ├── Autoencoder_Code.tar ├── CG_CLASSIFY.m ├── CG_CLASSIFY_INIT.m ├── CG_MNIST.m ├── backprop.asv ├── backprop.m ├── backpropclassify.m ├── backpropnew.asv ├── backpropnew.m ├── converter.m ├── csi2batch.m ├── csidata.asv ├── csidata.m ├── dbinv.m ├── get_scaled_csi.m ├── get_total_rss.m ├── getbatchdata.m ├── gettestbatchdata.asv ├── gettestbatchdata.m ├── longdata ├── main.asv ├── main.m ├── main_test.m ├── makebatches.m ├── minimize.m ├── mnistclassify.m ├── mnistdeepauto.asv ├── mnistdeepauto.m ├── mnistdisp.m ├── rbm.m ├── rbmhidlinear.m ├── read_bf_file.m ├── read_bfee.c ├── read_bfee.mexa64 ├── read_bfee.mexmaci64 ├── read_bfee.mexw32 ├── t10k-images-idx3-ubyte.gz ├── t10k-labels-idx1-ubyte.gz ├── testerror.asv ├── testerror.m ├── train-images-idx3-ubyte.gz └── train-labels-idx1-ubyte.gz ├── DeepFi ├── .PPP.m.swp ├── CG_MNIST.m ├── GaussianRBM.m ├── backpropagation.m ├── backpropagationtest.m ├── dbinv.m ├── dis.m ├── errfunc1.m ├── getE.asv ├── getE.m ├── get_eff_SNRs.m ├── get_scaled_csi.m ├── get_total_rss.m ├── getbatchdata.m ├── geterror.m ├── getonebatchdata.m ├── homeposition.asv ├── homeposition.m ├── labposition.m ├── labvedio.asv ├── main.m ├── minimize.m ├── position.m ├── rbm.m ├── rbmhidlinear.m ├── read_bf_file.m ├── read_bfee.c ├── read_bfee.mexa64 ├── read_bfee.mexmaci64 ├── read_bfee.mexw32 ├── savedata.m ├── test.m ├── testall.m ├── testcorr.m ├── testerr.asv ├── testerr.m ├── teststd.m └── xxxc.m ├── README.md └── Restricted Boltzmann Machines ├── AIS_RBM_Code.tar ├── RBM_AIS.m ├── README.txt ├── base_rate.m ├── calculate_logprob.m ├── calculate_true_partition.m ├── converter.asv ├── converter.m ├── demo_rbm.m ├── demo_toy.m ├── free_energy.m ├── logdiff.m ├── logsum.m ├── makebatches.asv ├── makebatches.m ├── mnistdisp.m ├── rbm.m ├── t10k-images.idx3-ubyte ├── t10k-labels.idx1-ubyte ├── train-images.idx3-ubyte └── train-labels.idx1-ubyte /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | 3 | # Mobile Tools for Java (J2ME) 4 | .mtj.tmp/ 5 | 6 | # Package Files # 7 | *.jar 8 | *.war 9 | *.ear 10 | 11 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 12 | hs_err_pid* 13 | -------------------------------------------------------------------------------- /Deep Belief Networks/CG_CLASSIFY.m: -------------------------------------------------------------------------------- 1 | % Version 1.000 2 | % 3 | % Code provided by Ruslan Salakhutdinov and Geoff Hinton 4 | % 5 | % Permission is granted for anyone to copy, use, modify, or distribute this 6 | % program and accompanying programs and documents for any purpose, provided 7 | % this copyright notice is retained and prominently displayed, along with 8 | % a note saying that the original programs are available from our 9 | % web page. 10 | % The programs and documents are distributed without any warranty, express or 11 | % implied. As the programs were written for research purposes only, they have 12 | % not been tested to the degree that would be advisable in any important 13 | % application. All use of these programs is entirely at the user's own risk. 14 | 15 | 16 | function [f, df] = CG_CLASSIFY(VV,Dim,XX,target); 17 | 18 | l1 = Dim(1); 19 | l2 = Dim(2); 20 | l3= Dim(3); 21 | l4= Dim(4); 22 | l5= Dim(5); 23 | N = size(XX,1); 24 | 25 | % Do decomversion. 26 | w1 = reshape(VV(1:(l1+1)*l2),l1+1,l2); 27 | xxx = (l1+1)*l2; 28 | w2 = reshape(VV(xxx+1:xxx+(l2+1)*l3),l2+1,l3); 29 | xxx = xxx+(l2+1)*l3; 30 | w3 = reshape(VV(xxx+1:xxx+(l3+1)*l4),l3+1,l4); 31 | xxx = xxx+(l3+1)*l4; 32 | w_class = reshape(VV(xxx+1:xxx+(l4+1)*l5),l4+1,l5); 33 | 34 | 35 | XX = [XX ones(N,1)]; 36 | w1probs = 1./(1 + exp(-XX*w1)); w1probs = [w1probs ones(N,1)]; 37 | w2probs = 1./(1 + exp(-w1probs*w2)); w2probs = [w2probs ones(N,1)]; 38 | w3probs = 1./(1 + exp(-w2probs*w3)); w3probs = [w3probs ones(N,1)]; 39 | 40 | targetout = exp(w3probs*w_class); 41 | targetout = targetout./repmat(sum(targetout,2),1,10); 42 | f = -sum(sum( target(:,1:end).*log(targetout))) ; 43 | 44 | IO = (targetout-target(:,1:end)); 45 | Ix_class=IO; 46 | dw_class = w3probs'*Ix_class; 47 | 48 | Ix3 = (Ix_class*w_class').*w3probs.*(1-w3probs); 49 | Ix3 = Ix3(:,1:end-1); 50 | dw3 = w2probs'*Ix3; 51 | 52 | Ix2 = (Ix3*w3').*w2probs.*(1-w2probs); 53 | Ix2 = Ix2(:,1:end-1); 54 | dw2 = w1probs'*Ix2; 55 | 56 | Ix1 = (Ix2*w2').*w1probs.*(1-w1probs); 57 | Ix1 = Ix1(:,1:end-1); 58 | dw1 = XX'*Ix1; 59 | 60 | df = [dw1(:)' dw2(:)' dw3(:)' dw_class(:)']'; 61 | -------------------------------------------------------------------------------- /Deep Belief Networks/CG_CLASSIFY_INIT.m: -------------------------------------------------------------------------------- 1 | % Version 1.000 2 | % 3 | % Code provided by Ruslan Salakhutdinov and Geoff Hinton 4 | % 5 | % Permission is granted for anyone to copy, use, modify, or distribute this 6 | % program and accompanying programs and documents for any purpose, provided 7 | % this copyright notice is retained and prominently displayed, along with 8 | % a note saying that the original programs are available from our 9 | % web page. 10 | % The programs and documents are distributed without any warranty, express or 11 | % implied. As the programs were written for research purposes only, they have 12 | % not been tested to the degree that would be advisable in any important 13 | % application. All use of these programs is entirely at the user's own risk. 14 | 15 | 16 | function [f, df] = CG_CLASSIFY_INIT(VV,Dim,w3probs,target); 17 | l1 = Dim(1); 18 | l2 = Dim(2); 19 | N = size(w3probs,1); 20 | % Do decomversion. 21 | w_class = reshape(VV,l1+1,l2); 22 | w3probs = [w3probs ones(N,1)]; 23 | 24 | targetout = exp(w3probs*w_class); 25 | targetout = targetout./repmat(sum(targetout,2),1,10); 26 | f = -sum(sum( target(:,1:end).*log(targetout))) ; 27 | IO = (targetout-target(:,1:end)); 28 | Ix_class=IO; 29 | dw_class = w3probs'*Ix_class; 30 | 31 | df = [dw_class(:)']'; 32 | 33 | -------------------------------------------------------------------------------- /Deep Belief Networks/CG_MNIST.m: -------------------------------------------------------------------------------- 1 | % Version 1.000 2 | % 3 | % Code provided by Ruslan Salakhutdinov and Geoff Hinton 4 | % 5 | % Permission is granted for anyone to copy, use, modify, or distribute this 6 | % program and accompanying programs and documents for any purpose, provided 7 | % this copyright notice is retained and prominently displayed, along with 8 | % a note saying that the original programs are available from our 9 | % web page. 10 | % The programs and documents are distributed without any warranty, express or 11 | % implied. As the programs were written for research purposes only, they have 12 | % not been tested to the degree that would be advisable in any important 13 | % application. All use of these programs is entirely at the user's own risk. 14 | 15 | function [f, df] = CG_MNIST(VV,Dim,XX); 16 | 17 | l1 = Dim(1); 18 | l2 = Dim(2); 19 | l3 = Dim(3); 20 | l4= Dim(4); 21 | l5= Dim(5); 22 | l6= Dim(6); 23 | l7= Dim(7); 24 | l8= Dim(8); 25 | l9= Dim(9); 26 | N = size(XX,1); 27 | 28 | % Do decomversion. 29 | w1 = reshape(VV(1:(l1+1)*l2),l1+1,l2); 30 | xxx = (l1+1)*l2; 31 | w2 = reshape(VV(xxx+1:xxx+(l2+1)*l3),l2+1,l3); 32 | xxx = xxx+(l2+1)*l3; 33 | w3 = reshape(VV(xxx+1:xxx+(l3+1)*l4),l3+1,l4); 34 | xxx = xxx+(l3+1)*l4; 35 | w4 = reshape(VV(xxx+1:xxx+(l4+1)*l5),l4+1,l5); 36 | xxx = xxx+(l4+1)*l5; 37 | w5 = reshape(VV(xxx+1:xxx+(l5+1)*l6),l5+1,l6); 38 | xxx = xxx+(l5+1)*l6; 39 | w6 = reshape(VV(xxx+1:xxx+(l6+1)*l7),l6+1,l7); 40 | xxx = xxx+(l6+1)*l7; 41 | w7 = reshape(VV(xxx+1:xxx+(l7+1)*l8),l7+1,l8); 42 | xxx = xxx+(l7+1)*l8; 43 | w8 = reshape(VV(xxx+1:xxx+(l8+1)*l9),l8+1,l9); 44 | 45 | 46 | XX = [XX ones(N,1)]; 47 | w1probs = 1./(1 + exp(-XX*w1)); w1probs = [w1probs ones(N,1)]; 48 | w2probs = 1./(1 + exp(-w1probs*w2)); w2probs = [w2probs ones(N,1)]; 49 | w3probs = 1./(1 + exp(-w2probs*w3)); w3probs = [w3probs ones(N,1)]; 50 | w4probs = w3probs*w4; w4probs = [w4probs ones(N,1)]; 51 | w5probs = 1./(1 + exp(-w4probs*w5)); w5probs = [w5probs ones(N,1)]; 52 | w6probs = 1./(1 + exp(-w5probs*w6)); w6probs = [w6probs ones(N,1)]; 53 | w7probs = 1./(1 + exp(-w6probs*w7)); w7probs = [w7probs ones(N,1)]; 54 | XXout = 1./(1 + exp(-w7probs*w8)); 55 | 56 | f = -1/N*sum(sum( XX(:,1:end-1).*log(XXout) + (1-XX(:,1:end-1)).*log(1-XXout))); 57 | IO = 1/N*(XXout-XX(:,1:end-1)); 58 | Ix8=IO; 59 | dw8 = w7probs'*Ix8; 60 | 61 | Ix7 = (Ix8*w8').*w7probs.*(1-w7probs); 62 | Ix7 = Ix7(:,1:end-1); 63 | dw7 = w6probs'*Ix7; 64 | 65 | Ix6 = (Ix7*w7').*w6probs.*(1-w6probs); 66 | Ix6 = Ix6(:,1:end-1); 67 | dw6 = w5probs'*Ix6; 68 | 69 | Ix5 = (Ix6*w6').*w5probs.*(1-w5probs); 70 | Ix5 = Ix5(:,1:end-1); 71 | dw5 = w4probs'*Ix5; 72 | 73 | Ix4 = (Ix5*w5'); 74 | Ix4 = Ix4(:,1:end-1); 75 | dw4 = w3probs'*Ix4; 76 | 77 | Ix3 = (Ix4*w4').*w3probs.*(1-w3probs); 78 | Ix3 = Ix3(:,1:end-1); 79 | dw3 = w2probs'*Ix3; 80 | 81 | Ix2 = (Ix3*w3').*w2probs.*(1-w2probs); 82 | Ix2 = Ix2(:,1:end-1); 83 | dw2 = w1probs'*Ix2; 84 | 85 | Ix1 = (Ix2*w2').*w1probs.*(1-w1probs); 86 | Ix1 = Ix1(:,1:end-1); 87 | dw1 = XX'*Ix1; 88 | 89 | df = [dw1(:)' dw2(:)' dw3(:)' dw4(:)' dw5(:)' dw6(:)' dw7(:)' dw8(:)' ]'; 90 | 91 | 92 | -------------------------------------------------------------------------------- /Deep Belief Networks/backprop.asv: -------------------------------------------------------------------------------- 1 | % Version 1.000 2 | % 3 | % Code provided by Ruslan Salakhutdinov and Geoff Hinton 4 | % 5 | % Permission is granted for anyone to copy, use, modify, or distribute this 6 | % program and accompanying programs and documents for any purpose, provided 7 | % this copyright notice is retained and prominently displayed, along with 8 | % a note saying that the original programs are available from our 9 | % web page. 10 | % The programs and documents are distributed without any warranty, express or 11 | % implied. As the programs were written for research purposes only, they have 12 | % not been tested to the degree that would be advisable in any important 13 | % application. All use of these programs is entirely at the user's own risk. 14 | 15 | % This program fine-tunes an autoencoder with backpropagation. 16 | % Weights of the autoencoder are going to be saved in mnist_weights.mat 17 | % and trainig and test reconstruction errors in mnist_error.mat 18 | % You can also set maxepoch, default value is 200 as in our paper. 19 | 20 | maxepoch=50; 21 | fprintf(1,'\nFine-tuning deep autoencoder by minimizing cross entropy error. \n'); 22 | % fprintf(1,'60 batches of 1000 cases each. \n'); 23 | 24 | load mnistvh 25 | load mnisthp 26 | load mnisthp2 27 | load mnistpo 28 | 29 | % makebatches; 30 | [numcases numdims numbatches]=size(batchdata); 31 | N=numcases; 32 | 33 | %%%% PREINITIALIZE WEIGHTS OF THE AUTOENCODER %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 34 | w1=[vishid; hidrecbiases]; 35 | w2=[hidpen; penrecbiases]; 36 | w3=[hidpen2; penrecbiases2]; 37 | w4=[hidtop; toprecbiases]; 38 | w5=[hidtop'; topgenbiases]; 39 | w6=[hidpen2'; hidgenbiases2]; 40 | w7=[hidpen'; hidgenbiases]; 41 | w8=[vishid'; visbiases]; 42 | 43 | %%%%%%%%%% END OF PREINITIALIZATIO OF WEIGHTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 44 | 45 | l1=size(w1,1)-1; 46 | l2=size(w2,1)-1; 47 | l3=size(w3,1)-1; 48 | l4=size(w4,1)-1; 49 | l5=size(w5,1)-1; 50 | l6=size(w6,1)-1; 51 | l7=size(w7,1)-1; 52 | l8=size(w8,1)-1; 53 | l9=l1; 54 | test_err=[]; 55 | train_err=[]; 56 | 57 | 58 | for epoch = 1:maxepoch 59 | 60 | %%%%%%%%%%%%%%%%%%%% COMPUTE TRAINING RECONSTRUCTION ERROR %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 61 | err=0; 62 | [numcases numdims numbatches]=size(batchdata); 63 | N=numcases; 64 | for batch = 1:numbatches 65 | data = [batchdata(:,:,batch)]; 66 | data = [data ones(N,1)]; 67 | w1probs = 1./(1 + exp(-data*w1)); w1probs = [w1probs ones(N,1)]; 68 | w2probs = 1./(1 + exp(-w1probs*w2)); w2probs = [w2probs ones(N,1)]; 69 | w3probs = 1./(1 + exp(-w2probs*w3)); w3probs = [w3probs ones(N,1)]; 70 | w4probs = w3probs*w4; w4probs = [w4probs ones(N,1)]; 71 | w5probs = 1./(1 + exp(-w4probs*w5)); w5probs = [w5probs ones(N,1)]; 72 | w6probs = 1./(1 + exp(-w5probs*w6)); w6probs = [w6probs ones(N,1)]; 73 | w7probs = 1./(1 + exp(-w6probs*w7)); w7probs = [w7probs ones(N,1)]; 74 | dataout = 1./(1 + exp(-w7probs*w8)); 75 | err= err + 1/N*sum(sum( (data(:,1:end-1)-dataout).^2 )); 76 | end 77 | train_err(epoch)=err/numbatches; 78 | 79 | %%%%%%%%%%%%%% END OF COMPUTING TRAINING RECONSTRUCTION ERROR %%%%%%%%%%%%%%%%%%%%%%%%%%%%% 80 | 81 | %%%% DISPLAY FIGURE TOP ROW REAL DATA BOTTOM ROW RECONSTRUCTIONS %%%%%%%%%%%%%%%%%%%%%%%%% 82 | % fprintf(1,'Displaying in figure 1: Top row - real data, Bottom row -- reconstructions \n'); 83 | % output=[]; 84 | % for ii=1:15 85 | % output = [output data(ii,1:end-1)' dataout(ii,:)']; 86 | % end 87 | % if epoch==1 88 | % close all 89 | % figure('Position',[100,600,1000,200]); 90 | % else 91 | % figure(1) 92 | % end 93 | % mnistdisp(output); 94 | % drawnow; 95 | 96 | %%%%%%%%%%%%%%%%%%%% COMPUTE TEST RECONSTRUCTION ERROR %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 97 | [testnumcases testnumdims testnumbatches]=size(testbatchdata); 98 | N=testnumcases; 99 | err=0; 100 | for batch = 1:testnumbatches 101 | data = [testbatchdata(:,:,batch)]; 102 | data = [data ones(N,1)]; 103 | w1probs = 1./(1 + exp(-data*w1)); w1probs = [w1probs ones(N,1)]; 104 | w2probs = 1./(1 + exp(-w1probs*w2)); w2probs = [w2probs ones(N,1)]; 105 | w3probs = 1./(1 + exp(-w2probs*w3)); w3probs = [w3probs ones(N,1)]; 106 | w4probs = w3probs*w4; w4probs = [w4probs ones(N,1)]; 107 | w5probs = 1./(1 + exp(-w4probs*w5)); w5probs = [w5probs ones(N,1)]; 108 | w6probs = 1./(1 + exp(-w5probs*w6)); w6probs = [w6probs ones(N,1)]; 109 | w7probs = 1./(1 + exp(-w6probs*w7)); w7probs = [w7probs ones(N,1)]; 110 | dataout = 1./(1 + exp(-w7probs*w8)); 111 | err = err + 1/N*sum(sum( (data(:,1:end-1)-dataout).^2 )); 112 | end 113 | test_err(epoch)=err/testnumbatches; 114 | fprintf(1,'Before epoch %d Train squared error: %6.3f Test squared error: %6.3f \t \t \n',epoch,train_err(epoch),test_err(epoch)); 115 | 116 | %%%%%%%%%%%%%% END OF COMPUTING TEST RECONSTRUCTION ERROR %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 117 | 118 | tt=0; 119 | for batch = 1:numbatches/5 120 | fprintf(1,'epoch %d batch %d\r',epoch,batch); 121 | 122 | %%%%%%%%%%% COMBINE 10 MINIBATCHES INTO 1 LARGER MINIBATCH %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 123 | tt=tt+1; 124 | data=[]; 125 | for kk=1:5 126 | data=[data 127 | batchdata(:,:,(tt-1)*5+kk)]; 128 | end 129 | 130 | %%%%%%%%%%%%%%% PERFORM CONJUGATE GRADIENT WITH 3 LINESEARCHES %%%%%%%%%%%%%%%%%%%%%%%%%%%%% 131 | max_iter=3; 132 | VV = [w1(:)' w2(:)' w3(:)' w4(:)' w5(:)' w6(:)' w7(:)' w8(:)']'; 133 | Dim = [l1; l2; l3; l4; l5; l6; l7; l8; l9]; 134 | 135 | [X, fX] = minimize(VV,'CG_MNIST',max_iter,Dim,data); 136 | 137 | w1 = reshape(X(1:(l1+1)*l2),l1+1,l2); 138 | xxx = (l1+1)*l2; 139 | w2 = reshape(X(xxx+1:xxx+(l2+1)*l3),l2+1,l3); 140 | xxx = xxx+(l2+1)*l3; 141 | w3 = reshape(X(xxx+1:xxx+(l3+1)*l4),l3+1,l4); 142 | xxx = xxx+(l3+1)*l4; 143 | w4 = reshape(X(xxx+1:xxx+(l4+1)*l5),l4+1,l5); 144 | xxx = xxx+(l4+1)*l5; 145 | w5 = reshape(X(xxx+1:xxx+(l5+1)*l6),l5+1,l6); 146 | xxx = xxx+(l5+1)*l6; 147 | w6 = reshape(X(xxx+1:xxx+(l6+1)*l7),l6+1,l7); 148 | xxx = xxx+(l6+1)*l7; 149 | w7 = reshape(X(xxx+1:xxx+(l7+1)*l8),l7+1,l8); 150 | xxx = xxx+(l7+1)*l8; 151 | w8 = reshape(X(xxx+1:xxx+(l8+1)*l9),l8+1,l9); 152 | 153 | %%%%%%%%%%%%%%% END OF CONJUGATE GRADIENT WITH 3 LINESEARCHES %%%%%%%%%%%%%%%%%%%%%%%%%%%%% 154 | 155 | end 156 | 157 | save mnist_weights w1 w2 w3 w4 w5 w6 w7 w8 158 | save mnist_error test_err train_err; 159 | 160 | end 161 | 162 | 163 | 164 | -------------------------------------------------------------------------------- /Deep Belief Networks/backprop.m: -------------------------------------------------------------------------------- 1 | % Version 1.000 2 | % 3 | % Code provided by Ruslan Salakhutdinov and Geoff Hinton 4 | % 5 | % Permission is granted for anyone to copy, use, modify, or distribute this 6 | % program and accompanying programs and documents for any purpose, provided 7 | % this copyright notice is retained and prominently displayed, along with 8 | % a note saying that the original programs are available from our 9 | % web page. 10 | % The programs and documents are distributed without any warranty, express or 11 | % implied. As the programs were written for research purposes only, they have 12 | % not been tested to the degree that would be advisable in any important 13 | % application. All use of these programs is entirely at the user's own risk. 14 | 15 | % This program fine-tunes an autoencoder with backpropagation. 16 | % Weights of the autoencoder are going to be saved in mnist_weights.mat 17 | % and trainig and test reconstruction errors in mnist_error.mat 18 | % You can also set maxepoch, default value is 200 as in our paper. 19 | 20 | maxepoch=3; 21 | fprintf(1,'\nFine-tuning deep autoencoder by minimizing cross entropy error. \n'); 22 | % fprintf(1,'60 batches of 1000 cases each. \n'); 23 | 24 | load mnistvh 25 | load mnisthp 26 | load mnisthp2 27 | load mnistpo 28 | 29 | % makebatches; 30 | [numcases numdims numbatches]=size(batchdata); 31 | N=numcases; 32 | 33 | %%%% PREINITIALIZE WEIGHTS OF THE AUTOENCODER %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 34 | w1=[vishid; hidrecbiases]; 35 | w2=[hidpen; penrecbiases]; 36 | w3=[hidpen2; penrecbiases2]; 37 | w4=[hidtop; toprecbiases]; 38 | w5=[hidtop'; topgenbiases]; 39 | w6=[hidpen2'; hidgenbiases2]; 40 | w7=[hidpen'; hidgenbiases]; 41 | w8=[vishid'; visbiases]; 42 | 43 | %%%%%%%%%% END OF PREINITIALIZATIO OF WEIGHTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 44 | 45 | l1=size(w1,1)-1; 46 | l2=size(w2,1)-1; 47 | l3=size(w3,1)-1; 48 | l4=size(w4,1)-1; 49 | l5=size(w5,1)-1; 50 | l6=size(w6,1)-1; 51 | l7=size(w7,1)-1; 52 | l8=size(w8,1)-1; 53 | l9=l1; 54 | test_err=[]; 55 | train_err=[]; 56 | 57 | 58 | for epoch = 1:maxepoch 59 | 60 | %%%%%%%%%%%%%%%%%%%% COMPUTE TRAINING RECONSTRUCTION ERROR %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 61 | err=0; 62 | [numcases numdims numbatches]=size(batchdata); 63 | N=numcases; 64 | for batch = 1:numbatches 65 | data = [batchdata(:,:,batch)]; 66 | data = [data ones(N,1)]; 67 | w1probs = 1./(1 + exp(-data*w1)); w1probs = [w1probs ones(N,1)]; 68 | w2probs = 1./(1 + exp(-w1probs*w2)); w2probs = [w2probs ones(N,1)]; 69 | w3probs = 1./(1 + exp(-w2probs*w3)); w3probs = [w3probs ones(N,1)]; 70 | w4probs = w3probs*w4; w4probs = [w4probs ones(N,1)]; 71 | w5probs = 1./(1 + exp(-w4probs*w5)); w5probs = [w5probs ones(N,1)]; 72 | w6probs = 1./(1 + exp(-w5probs*w6)); w6probs = [w6probs ones(N,1)]; 73 | w7probs = 1./(1 + exp(-w6probs*w7)); w7probs = [w7probs ones(N,1)]; 74 | dataout = 1./(1 + exp(-w7probs*w8)); 75 | err= err + 1/N*sum(sum( (data(:,1:end-1)-dataout).^2 )); 76 | end 77 | train_err(epoch)=err/numbatches; 78 | 79 | %%%%%%%%%%%%%% END OF COMPUTING TRAINING RECONSTRUCTION ERROR %%%%%%%%%%%%%%%%%%%%%%%%%%%%% 80 | 81 | %%%% DISPLAY FIGURE TOP ROW REAL DATA BOTTOM ROW RECONSTRUCTIONS %%%%%%%%%%%%%%%%%%%%%%%%% 82 | % fprintf(1,'Displaying in figure 1: Top row - real data, Bottom row -- reconstructions \n'); 83 | % output=[]; 84 | % for ii=1:15 85 | % output = [output data(ii,1:end-1)' dataout(ii,:)']; 86 | % end 87 | % if epoch==1 88 | % close all 89 | % figure('Position',[100,600,1000,200]); 90 | % else 91 | % figure(1) 92 | % end 93 | % mnistdisp(output); 94 | % drawnow; 95 | 96 | %%%%%%%%%%%%%%%%%%%% COMPUTE TEST RECONSTRUCTION ERROR %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 97 | [testnumcases testnumdims testnumbatches]=size(testbatchdata); 98 | N=testnumcases; 99 | err=0; 100 | for batch = 1:testnumbatches 101 | data = [testbatchdata(:,:,batch)]; 102 | data = [data ones(N,1)]; 103 | w1probs = 1./(1 + exp(-data*w1)); w1probs = [w1probs ones(N,1)]; 104 | w2probs = 1./(1 + exp(-w1probs*w2)); w2probs = [w2probs ones(N,1)]; 105 | w3probs = 1./(1 + exp(-w2probs*w3)); w3probs = [w3probs ones(N,1)]; 106 | w4probs = w3probs*w4; w4probs = [w4probs ones(N,1)]; 107 | w5probs = 1./(1 + exp(-w4probs*w5)); w5probs = [w5probs ones(N,1)]; 108 | w6probs = 1./(1 + exp(-w5probs*w6)); w6probs = [w6probs ones(N,1)]; 109 | w7probs = 1./(1 + exp(-w6probs*w7)); w7probs = [w7probs ones(N,1)]; 110 | dataout = 1./(1 + exp(-w7probs*w8)); 111 | err = err + 1/N*sum(sum( (data(:,1:end-1)-dataout).^2 )); 112 | end 113 | test_err(epoch)=err/testnumbatches; 114 | fprintf(1,'Before epoch %d Train squared error: %6.3f Test squared error: %6.3f \t \t \n',epoch,train_err(epoch),test_err(epoch)); 115 | 116 | %%%%%%%%%%%%%% END OF COMPUTING TEST RECONSTRUCTION ERROR %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 117 | 118 | tt=0; 119 | for batch = 1:numbatches/10 120 | fprintf(1,'epoch %d batch %d\r',epoch,batch); 121 | 122 | %%%%%%%%%%% COMBINE 10 MINIBATCHES INTO 1 LARGER MINIBATCH %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 123 | tt=tt+1; 124 | data=[]; 125 | for kk=1:10 126 | data=[data 127 | batchdata(:,:,(tt-1)*10+kk)]; 128 | end 129 | 130 | %%%%%%%%%%%%%%% PERFORM CONJUGATE GRADIENT WITH 3 LINESEARCHES %%%%%%%%%%%%%%%%%%%%%%%%%%%%% 131 | max_iter=3; 132 | VV = [w1(:)' w2(:)' w3(:)' w4(:)' w5(:)' w6(:)' w7(:)' w8(:)']'; 133 | Dim = [l1; l2; l3; l4; l5; l6; l7; l8; l9]; 134 | 135 | [X, fX] = minimize(VV,'CG_MNIST',max_iter,Dim,data); 136 | 137 | w1 = reshape(X(1:(l1+1)*l2),l1+1,l2); 138 | xxx = (l1+1)*l2; 139 | w2 = reshape(X(xxx+1:xxx+(l2+1)*l3),l2+1,l3); 140 | xxx = xxx+(l2+1)*l3; 141 | w3 = reshape(X(xxx+1:xxx+(l3+1)*l4),l3+1,l4); 142 | xxx = xxx+(l3+1)*l4; 143 | w4 = reshape(X(xxx+1:xxx+(l4+1)*l5),l4+1,l5); 144 | xxx = xxx+(l4+1)*l5; 145 | w5 = reshape(X(xxx+1:xxx+(l5+1)*l6),l5+1,l6); 146 | xxx = xxx+(l5+1)*l6; 147 | w6 = reshape(X(xxx+1:xxx+(l6+1)*l7),l6+1,l7); 148 | xxx = xxx+(l6+1)*l7; 149 | w7 = reshape(X(xxx+1:xxx+(l7+1)*l8),l7+1,l8); 150 | xxx = xxx+(l7+1)*l8; 151 | w8 = reshape(X(xxx+1:xxx+(l8+1)*l9),l8+1,l9); 152 | 153 | %%%%%%%%%%%%%%% END OF CONJUGATE GRADIENT WITH 3 LINESEARCHES %%%%%%%%%%%%%%%%%%%%%%%%%%%%% 154 | 155 | end 156 | 157 | save mnist_weights w1 w2 w3 w4 w5 w6 w7 w8 158 | save mnist_error test_err train_err; 159 | 160 | end 161 | 162 | 163 | 164 | -------------------------------------------------------------------------------- /Deep Belief Networks/backpropclassify.m: -------------------------------------------------------------------------------- 1 | % Version 1.000 2 | % 3 | % Code provided by Ruslan Salakhutdinov and Geoff Hinton 4 | % 5 | % Permission is granted for anyone to copy, use, modify, or distribute this 6 | % program and accompanying programs and documents for any purpose, provided 7 | % this copyright notice is retained and prominently displayed, along with 8 | % a note saying that the original programs are available from our 9 | % web page. 10 | % The programs and documents are distributed without any warranty, express or 11 | % implied. As the programs were written for research purposes only, they have 12 | % not been tested to the degree that would be advisable in any important 13 | % application. All use of these programs is entirely at the user's own risk. 14 | 15 | % This program fine-tunes an autoencoder with backpropagation. 16 | % Weights of the autoencoder are going to be saved in mnist_weights.mat 17 | % and trainig and test reconstruction errors in mnist_error.mat 18 | % You can also set maxepoch, default value is 200 as in our paper. 19 | 20 | maxepoch=200; 21 | fprintf(1,'\nTraining discriminative model on MNIST by minimizing cross entropy error. \n'); 22 | fprintf(1,'60 batches of 1000 cases each. \n'); 23 | 24 | load mnistvhclassify 25 | load mnisthpclassify 26 | load mnisthp2classify 27 | 28 | makebatches; 29 | [numcases numdims numbatches]=size(batchdata); 30 | N=numcases; 31 | 32 | %%%% PREINITIALIZE WEIGHTS OF THE DISCRIMINATIVE MODEL%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 33 | 34 | w1=[vishid; hidrecbiases]; 35 | w2=[hidpen; penrecbiases]; 36 | w3=[hidpen2; penrecbiases2]; 37 | w_class = 0.1*randn(size(w3,2)+1,10); 38 | 39 | 40 | %%%%%%%%%% END OF PREINITIALIZATIO OF WEIGHTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 41 | 42 | l1=size(w1,1)-1; 43 | l2=size(w2,1)-1; 44 | l3=size(w3,1)-1; 45 | l4=size(w_class,1)-1; 46 | l5=10; 47 | test_err=[]; 48 | train_err=[]; 49 | 50 | 51 | for epoch = 1:maxepoch 52 | 53 | %%%%%%%%%%%%%%%%%%%% COMPUTE TRAINING MISCLASSIFICATION ERROR %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 54 | err=0; 55 | err_cr=0; 56 | counter=0; 57 | [numcases numdims numbatches]=size(batchdata); 58 | N=numcases; 59 | for batch = 1:numbatches 60 | data = [batchdata(:,:,batch)]; 61 | target = [batchtargets(:,:,batch)]; 62 | data = [data ones(N,1)]; 63 | w1probs = 1./(1 + exp(-data*w1)); w1probs = [w1probs ones(N,1)]; 64 | w2probs = 1./(1 + exp(-w1probs*w2)); w2probs = [w2probs ones(N,1)]; 65 | w3probs = 1./(1 + exp(-w2probs*w3)); w3probs = [w3probs ones(N,1)]; 66 | targetout = exp(w3probs*w_class); 67 | targetout = targetout./repmat(sum(targetout,2),1,10); 68 | 69 | [I J]=max(targetout,[],2); 70 | [I1 J1]=max(target,[],2); 71 | counter=counter+length(find(J==J1)); 72 | err_cr = err_cr- sum(sum( target(:,1:end).*log(targetout))) ; 73 | end 74 | train_err(epoch)=(numcases*numbatches-counter); 75 | train_crerr(epoch)=err_cr/numbatches; 76 | 77 | %%%%%%%%%%%%%% END OF COMPUTING TRAINING MISCLASSIFICATION ERROR %%%%%%%%%%%%%%%%%%%%%%%%%%%%% 78 | 79 | %%%%%%%%%%%%%%%%%%%% COMPUTE TEST MISCLASSIFICATION ERROR %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 80 | err=0; 81 | err_cr=0; 82 | counter=0; 83 | [testnumcases testnumdims testnumbatches]=size(testbatchdata); 84 | N=testnumcases; 85 | for batch = 1:testnumbatches 86 | data = [testbatchdata(:,:,batch)]; 87 | target = [testbatchtargets(:,:,batch)]; 88 | data = [data ones(N,1)]; 89 | w1probs = 1./(1 + exp(-data*w1)); w1probs = [w1probs ones(N,1)]; 90 | w2probs = 1./(1 + exp(-w1probs*w2)); w2probs = [w2probs ones(N,1)]; 91 | w3probs = 1./(1 + exp(-w2probs*w3)); w3probs = [w3probs ones(N,1)]; 92 | targetout = exp(w3probs*w_class); 93 | targetout = targetout./repmat(sum(targetout,2),1,10); 94 | 95 | [I J]=max(targetout,[],2); 96 | [I1 J1]=max(target,[],2); 97 | counter=counter+length(find(J==J1)); 98 | err_cr = err_cr- sum(sum( target(:,1:end).*log(targetout))) ; 99 | end 100 | test_err(epoch)=(testnumcases*testnumbatches-counter); 101 | test_crerr(epoch)=err_cr/testnumbatches; 102 | fprintf(1,'Before epoch %d Train # misclassified: %d (from %d). Test # misclassified: %d (from %d) \t \t \n',... 103 | epoch,train_err(epoch),numcases*numbatches,test_err(epoch),testnumcases*testnumbatches); 104 | 105 | %%%%%%%%%%%%%% END OF COMPUTING TEST MISCLASSIFICATION ERROR %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 106 | 107 | tt=0; 108 | for batch = 1:numbatches/10 109 | fprintf(1,'epoch %d batch %d\r',epoch,batch); 110 | 111 | %%%%%%%%%%% COMBINE 10 MINIBATCHES INTO 1 LARGER MINIBATCH %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 112 | tt=tt+1; 113 | data=[]; 114 | targets=[]; 115 | for kk=1:10 116 | data=[data 117 | batchdata(:,:,(tt-1)*10+kk)]; 118 | targets=[targets 119 | batchtargets(:,:,(tt-1)*10+kk)]; 120 | end 121 | 122 | %%%%%%%%%%%%%%% PERFORM CONJUGATE GRADIENT WITH 3 LINESEARCHES %%%%%%%%%%%%%%%%%%%%%%%%%%%%% 123 | max_iter=3; 124 | 125 | if epoch<6 % First update top-level weights holding other weights fixed. 126 | N = size(data,1); 127 | XX = [data ones(N,1)]; 128 | w1probs = 1./(1 + exp(-XX*w1)); w1probs = [w1probs ones(N,1)]; 129 | w2probs = 1./(1 + exp(-w1probs*w2)); w2probs = [w2probs ones(N,1)]; 130 | w3probs = 1./(1 + exp(-w2probs*w3)); %w3probs = [w3probs ones(N,1)]; 131 | 132 | VV = [w_class(:)']'; 133 | Dim = [l4; l5]; 134 | [X, fX] = minimize(VV,'CG_CLASSIFY_INIT',max_iter,Dim,w3probs,targets); 135 | w_class = reshape(X,l4+1,l5); 136 | 137 | else 138 | VV = [w1(:)' w2(:)' w3(:)' w_class(:)']'; 139 | Dim = [l1; l2; l3; l4; l5]; 140 | [X, fX] = minimize(VV,'CG_CLASSIFY',max_iter,Dim,data,targets); 141 | 142 | w1 = reshape(X(1:(l1+1)*l2),l1+1,l2); 143 | xxx = (l1+1)*l2; 144 | w2 = reshape(X(xxx+1:xxx+(l2+1)*l3),l2+1,l3); 145 | xxx = xxx+(l2+1)*l3; 146 | w3 = reshape(X(xxx+1:xxx+(l3+1)*l4),l3+1,l4); 147 | xxx = xxx+(l3+1)*l4; 148 | w_class = reshape(X(xxx+1:xxx+(l4+1)*l5),l4+1,l5); 149 | 150 | end 151 | %%%%%%%%%%%%%%% END OF CONJUGATE GRADIENT WITH 3 LINESEARCHES %%%%%%%%%%%%%%%%%%%%%%%%%%%%% 152 | 153 | end 154 | 155 | save mnistclassify_weights w1 w2 w3 w_class 156 | save mnistclassify_error test_err test_crerr train_err train_crerr; 157 | 158 | end 159 | 160 | 161 | 162 | -------------------------------------------------------------------------------- /Deep Belief Networks/backpropnew.asv: -------------------------------------------------------------------------------- 1 | % Version 1.000 2 | % 3 | % Code provided by Ruslan Salakhutdinov and Geoff Hinton 4 | % 5 | % Permission is granted for anyone to copy, use, modify, or distribute this 6 | % program and accompanying programs and documents for any purpose, provided 7 | % this copyright notice is retained and prominently displayed, along with 8 | % a note saying that the original programs are available from our 9 | % web page. 10 | % The programs and documents are distributed without any warranty, express or 11 | % implied. As the programs were written for research purposes only, they have 12 | % not been tested to the degree that would be advisable in any important 13 | % application. All use of these programs is entirely at the user's own risk. 14 | 15 | % This program fine-tunes an autoencoder with backpropagation. 16 | % Weights of the autoencoder are going to be saved in mnist_weights.mat 17 | % and trainig and test reconstruction errors in mnist_error.mat 18 | % You can also set maxepoch, default value is 200 as in our paper. 19 | 20 | maxepoch=50; 21 | fprintf(1,'\nFine-tuning deep autoencoder by minimizing cross entropy error. \n'); 22 | % fprintf(1,'60 batches of 1000 cases each. \n'); 23 | 24 | load mnistvh 25 | load mnisthp 26 | load mnisthp2 27 | load mnistpo 28 | 29 | % makebatches; 30 | [numcases numdims numbatches]=size(batchdata); 31 | N=numcases; 32 | 33 | %%%% PREINITIALIZE WEIGHTS OF THE AUTOENCODER %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 34 | w1=[vishid; hidrecbiases]; 35 | w2=[hidpen; penrecbiases]; 36 | % w3=[hidpen2; penrecbiases2]; 37 | w4=[hidtop; toprecbiases]; 38 | w5=[hidtop'; topgenbiases]; 39 | % w6=[hidpen2'; hidgenbiases2]; 40 | w7=[hidpen'; hidgenbiases]; 41 | w8=[vishid'; visbiases]; 42 | 43 | %%%%%%%%%% END OF PREINITIALIZATIO OF WEIGHTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 44 | 45 | l1=size(w1,1)-1; 46 | l2=size(w2,1)-1; 47 | % l3=size(w3,1)-1; 48 | l4=size(w4,1)-1; 49 | l5=size(w5,1)-1; 50 | % l6=size(w6,1)-1; 51 | l7=size(w7,1)-1; 52 | l8=size(w8,1)-1; 53 | l9=l1; 54 | test_err=[]; 55 | train_err=[]; 56 | 57 | 58 | for epoch = 1:maxepoch 59 | 60 | %%%%%%%%%%%%%%%%%%%% COMPUTE TRAINING RECONSTRUCTION ERROR %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 61 | err=0; 62 | [numcases numdims numbatches]=size(batchdata); 63 | N=numcases; 64 | for batch = 1:numbatches 65 | data = [batchdata(:,:,batch)]; 66 | data = [data ones(N,1)]; 67 | w1probs = 1./(1 + exp(-data*w1)); w1probs = [w1probs ones(N,1)]; 68 | w2probs = 1./(1 + exp(-w1probs*w2)); w2probs = [w2probs ones(N,1)]; 69 | % w3probs = 1./(1 + exp(-w2probs*w3)); w3probs = [w3probs ones(N,1)]; 70 | w4probs = w2probs*w4; w4probs = [w4probs ones(N,1)]; 71 | w5probs = 1./(1 + exp(-w4probs*w5)); w5probs = [w5probs ones(N,1)]; 72 | % w6probs = 1./(1 + exp(-w5probs*w6)); w6probs = [w6probs ones(N,1)]; 73 | w7probs = 1./(1 + exp(-w5probs*w7)); w7probs = [w7probs ones(N,1)]; 74 | dataout = 1./(1 + exp(-w7probs*w8)); 75 | err= err + 1/N*sum(sum( (data(:,1:end-1)-dataout).^2 )); 76 | end 77 | train_err(epoch)=err/numbatches; 78 | 79 | %%%%%%%%%%%%%% END OF COMPUTING TRAINING RECONSTRUCTION ERROR %%%%%%%%%%%%%%%%%%%%%%%%%%%%% 80 | 81 | %%%% DISPLAY FIGURE TOP ROW REAL DATA BOTTOM ROW RECONSTRUCTIONS %%%%%%%%%%%%%%%%%%%%%%%%% 82 | % fprintf(1,'Displaying in figure 1: Top row - real data, Bottom row -- reconstructions \n'); 83 | % output=[]; 84 | % for ii=1:15 85 | % output = [output data(ii,1:end-1)' dataout(ii,:)']; 86 | % end 87 | % if epoch==1 88 | % close all 89 | % figure('Position',[100,600,1000,200]); 90 | % else 91 | % figure(1) 92 | % end 93 | % mnistdisp(output); 94 | % drawnow; 95 | 96 | %%%%%%%%%%%%%%%%%%%% COMPUTE TEST RECONSTRUCTION ERROR %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 97 | [testnumcases testnumdims testnumbatches]=size(testbatchdata); 98 | N=testnumcases; 99 | err=0; 100 | for batch = 1:testnumbatches 101 | data = [testbatchdata(:,:,batch)]; 102 | data = [data ones(N,1)]; 103 | w1probs = 1./(1 + exp(-data*w1)); w1probs = [w1probs ones(N,1)]; 104 | w2probs = 1./(1 + exp(-w1probs*w2)); w2probs = [w2probs ones(N,1)]; 105 | % w3probs = 1./(1 + exp(-w2probs*w3)); w3probs = [w3probs ones(N,1)]; 106 | w4probs = w2probs*w4; w4probs = [w4probs ones(N,1)]; 107 | w5probs = 1./(1 + exp(-w4probs*w5)); w5probs = [w5probs ones(N,1)]; 108 | % w6probs = 1./(1 + exp(-w5probs*w6)); w6probs = [w6probs ones(N,1)]; 109 | w7probs = 1./(1 + exp(-w5probs*w7)); w7probs = [w7probs ones(N,1)]; 110 | dataout = 1./(1 + exp(-w7probs*w8)); 111 | err = err + 1/N*sum(sum( (data(:,1:end-1)-dataout).^2 )); 112 | end 113 | test_err(epoch)=err/testnumbatches; 114 | fprintf(1,'Before epoch %d Train squared error: %6.3f Test squared error: %6.3f \t \t \n',epoch,train_err(epoch),test_err(epoch)); 115 | 116 | %%%%%%%%%%%%%% END OF COMPUTING TEST RECONSTRUCTION ERROR %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 117 | 118 | tt=0; 119 | for batch = 1:numbatches/10 120 | fprintf(1,'epoch %d batch %d\r',epoch,batch); 121 | 122 | %%%%%%%%%%% COMBINE 10 MINIBATCHES INTO 1 LARGER MINIBATCH %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 123 | tt=tt+1; 124 | data=[]; 125 | for kk=1:10 126 | data=[data 127 | batchdata(:,:,(tt-1)*10+kk)]; 128 | end 129 | 130 | %%%%%%%%%%%%%%% PERFORM CONJUGATE GRADIENT WITH 3 LINESEARCHES %%%%%%%%%%%%%%%%%%%%%%%%%%%%% 131 | max_iter=3; 132 | % VV = [w1(:)' w2(:)' w3(:)' w4(:)' w5(:)' w6(:)' w7(:)' w8(:)']'; 133 | VV = [w1(:)' w2(:)' w4(:)' w5(:)' w7(:)' w8(:)']'; 134 | Dim = [l1; l2; l4; l5; l6; l7; l8; l9]; 135 | 136 | [X, fX] = minimize(VV,'CG_MNIST',max_iter,Dim,data); 137 | 138 | w1 = reshape(X(1:(l1+1)*l2),l1+1,l2); 139 | xxx = (l1+1)*l2; 140 | w2 = reshape(X(xxx+1:xxx+(l2+1)*l3),l2+1,l3); 141 | xxx = xxx+(l2+1)*l3; 142 | % w3 = reshape(X(xxx+1:xxx+(l3+1)*l4),l3+1,l4); 143 | % xxx = xxx+(l3+1)*l4; 144 | w4 = reshape(X(xxx+1:xxx+(l4+1)*l5),l4+1,l5); 145 | xxx = xxx+(l4+1)*l5; 146 | w5 = reshape(X(xxx+1:xxx+(l5+1)*l6),l5+1,l6); 147 | xxx = xxx+(l5+1)*l6; 148 | % w6 = reshape(X(xxx+1:xxx+(l6+1)*l7),l6+1,l7); 149 | % xxx = xxx+(l6+1)*l7; 150 | w7 = reshape(X(xxx+1:xxx+(l7+1)*l8),l7+1,l8); 151 | xxx = xxx+(l7+1)*l8; 152 | w8 = reshape(X(xxx+1:xxx+(l8+1)*l9),l8+1,l9); 153 | 154 | %%%%%%%%%%%%%%% END OF CONJUGATE GRADIENT WITH 3 LINESEARCHES %%%%%%%%%%%%%%%%%%%%%%%%%%%%% 155 | 156 | end 157 | 158 | save mnist_weights w1 w2 w3 w4 w5 w6 w7 w8 159 | save mnist_error test_err train_err; 160 | 161 | end 162 | 163 | 164 | 165 | -------------------------------------------------------------------------------- /Deep Belief Networks/backpropnew.m: -------------------------------------------------------------------------------- 1 | % Version 1.000 2 | % 3 | % Code provided by Ruslan Salakhutdinov and Geoff Hinton 4 | % 5 | % Permission is granted for anyone to copy, use, modify, or distribute this 6 | % program and accompanying programs and documents for any purpose, provided 7 | % this copyright notice is retained and prominently displayed, along with 8 | % a note saying that the original programs are available from our 9 | % web page. 10 | % The programs and documents are distributed without any warranty, express or 11 | % implied. As the programs were written for research purposes only, they have 12 | % not been tested to the degree that would be advisable in any important 13 | % application. All use of these programs is entirely at the user's own risk. 14 | 15 | % This program fine-tunes an autoencoder with backpropagation. 16 | % Weights of the autoencoder are going to be saved in mnist_weights.mat 17 | % and trainig and test reconstruction errors in mnist_error.mat 18 | % You can also set maxepoch, default value is 200 as in our paper. 19 | 20 | maxepoch=50; 21 | fprintf(1,'\nFine-tuning deep autoencoder by minimizing cross entropy error. \n'); 22 | % fprintf(1,'60 batches of 1000 cases each. \n'); 23 | 24 | load mnistvh 25 | load mnisthp 26 | load mnisthp2 27 | load mnistpo 28 | 29 | % makebatches; 30 | [numcases numdims numbatches]=size(batchdata); 31 | N=numcases; 32 | 33 | %%%% PREINITIALIZE WEIGHTS OF THE AUTOENCODER %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 34 | w1=[vishid; hidrecbiases]; 35 | w2=[hidpen; penrecbiases]; 36 | % w3=[hidpen2; penrecbiases2]; 37 | w4=[hidtop; toprecbiases]; 38 | w5=[hidtop'; topgenbiases]; 39 | % w6=[hidpen2'; hidgenbiases2]; 40 | w7=[hidpen'; hidgenbiases]; 41 | w8=[vishid'; visbiases]; 42 | 43 | %%%%%%%%%% END OF PREINITIALIZATIO OF WEIGHTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 44 | 45 | l1=size(w1,1)-1; 46 | l2=size(w2,1)-1; 47 | % l3=size(w3,1)-1; 48 | l4=size(w4,1)-1; 49 | l5=size(w5,1)-1; 50 | % l6=size(w6,1)-1; 51 | l7=size(w7,1)-1; 52 | l8=size(w8,1)-1; 53 | l9=l1; 54 | test_err=[]; 55 | train_err=[]; 56 | 57 | 58 | for epoch = 1:maxepoch 59 | 60 | %%%%%%%%%%%%%%%%%%%% COMPUTE TRAINING RECONSTRUCTION ERROR %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 61 | err=0; 62 | [numcases numdims numbatches]=size(batchdata); 63 | N=numcases; 64 | for batch = 1:numbatches 65 | data = [batchdata(:,:,batch)]; 66 | data = [data ones(N,1)]; 67 | w1probs = 1./(1 + exp(-data*w1)); w1probs = [w1probs ones(N,1)]; 68 | w2probs = 1./(1 + exp(-w1probs*w2)); w2probs = [w2probs ones(N,1)]; 69 | % w3probs = 1./(1 + exp(-w2probs*w3)); w3probs = [w3probs ones(N,1)]; 70 | w4probs = w2probs*w4; w4probs = [w4probs ones(N,1)]; 71 | w5probs = 1./(1 + exp(-w4probs*w5)); w5probs = [w5probs ones(N,1)]; 72 | % w6probs = 1./(1 + exp(-w5probs*w6)); w6probs = [w6probs ones(N,1)]; 73 | w7probs = 1./(1 + exp(-w5probs*w7)); w7probs = [w7probs ones(N,1)]; 74 | dataout = 1./(1 + exp(-w7probs*w8)); 75 | err= err + 1/N*sum(sum( (data(:,1:end-1)-dataout).^2 )); 76 | end 77 | train_err(epoch)=err/numbatches; 78 | 79 | %%%%%%%%%%%%%% END OF COMPUTING TRAINING RECONSTRUCTION ERROR %%%%%%%%%%%%%%%%%%%%%%%%%%%%% 80 | 81 | %%%% DISPLAY FIGURE TOP ROW REAL DATA BOTTOM ROW RECONSTRUCTIONS %%%%%%%%%%%%%%%%%%%%%%%%% 82 | % fprintf(1,'Displaying in figure 1: Top row - real data, Bottom row -- reconstructions \n'); 83 | % output=[]; 84 | % for ii=1:15 85 | % output = [output data(ii,1:end-1)' dataout(ii,:)']; 86 | % end 87 | % if epoch==1 88 | % close all 89 | % figure('Position',[100,600,1000,200]); 90 | % else 91 | % figure(1) 92 | % end 93 | % mnistdisp(output); 94 | % drawnow; 95 | 96 | %%%%%%%%%%%%%%%%%%%% COMPUTE TEST RECONSTRUCTION ERROR %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 97 | [testnumcases testnumdims testnumbatches]=size(testbatchdata); 98 | N=testnumcases; 99 | err=0; 100 | for batch = 1:testnumbatches 101 | data = [testbatchdata(:,:,batch)]; 102 | data = [data ones(N,1)]; 103 | w1probs = 1./(1 + exp(-data*w1)); w1probs = [w1probs ones(N,1)]; 104 | w2probs = 1./(1 + exp(-w1probs*w2)); w2probs = [w2probs ones(N,1)]; 105 | % w3probs = 1./(1 + exp(-w2probs*w3)); w3probs = [w3probs ones(N,1)]; 106 | w4probs = w2probs*w4; w4probs = [w4probs ones(N,1)]; 107 | w5probs = 1./(1 + exp(-w4probs*w5)); w5probs = [w5probs ones(N,1)]; 108 | % w6probs = 1./(1 + exp(-w5probs*w6)); w6probs = [w6probs ones(N,1)]; 109 | w7probs = 1./(1 + exp(-w5probs*w7)); w7probs = [w7probs ones(N,1)]; 110 | dataout = 1./(1 + exp(-w7probs*w8)); 111 | err = err + 1/N*sum(sum( (data(:,1:end-1)-dataout).^2 )); 112 | end 113 | test_err(epoch)=err/testnumbatches; 114 | fprintf(1,'Before epoch %d Train squared error: %6.3f Test squared error: %6.3f \t \t \n',epoch,train_err(epoch),test_err(epoch)); 115 | 116 | %%%%%%%%%%%%%% END OF COMPUTING TEST RECONSTRUCTION ERROR %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 117 | 118 | tt=0; 119 | for batch = 1:numbatches/10 120 | fprintf(1,'epoch %d batch %d\r',epoch,batch); 121 | 122 | %%%%%%%%%%% COMBINE 10 MINIBATCHES INTO 1 LARGER MINIBATCH %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 123 | tt=tt+1; 124 | data=[]; 125 | for kk=1:10 126 | data=[data 127 | batchdata(:,:,(tt-1)*10+kk)]; 128 | end 129 | 130 | %%%%%%%%%%%%%%% PERFORM CONJUGATE GRADIENT WITH 3 LINESEARCHES %%%%%%%%%%%%%%%%%%%%%%%%%%%%% 131 | max_iter=3; 132 | % VV = [w1(:)' w2(:)' w3(:)' w4(:)' w5(:)' w6(:)' w7(:)' w8(:)']'; 133 | VV = [w1(:)' w2(:)' w4(:)' w5(:)' w7(:)' w8(:)']'; 134 | Dim = [l1; l2; l4; l5; l7; l8; l9]; 135 | 136 | [X, fX] = minimize(VV,'CG_MNIST',max_iter,Dim,data); 137 | 138 | w1 = reshape(X(1:(l1+1)*l2),l1+1,l2); 139 | xxx = (l1+1)*l2; 140 | w2 = reshape(X(xxx+1:xxx+(l2+1)*l3),l2+1,l3); 141 | xxx = xxx+(l2+1)*l3; 142 | % w3 = reshape(X(xxx+1:xxx+(l3+1)*l4),l3+1,l4); 143 | % xxx = xxx+(l3+1)*l4; 144 | w4 = reshape(X(xxx+1:xxx+(l4+1)*l5),l4+1,l5); 145 | xxx = xxx+(l4+1)*l5; 146 | w5 = reshape(X(xxx+1:xxx+(l5+1)*l6),l5+1,l6); 147 | xxx = xxx+(l5+1)*l6; 148 | % w6 = reshape(X(xxx+1:xxx+(l6+1)*l7),l6+1,l7); 149 | % xxx = xxx+(l6+1)*l7; 150 | w7 = reshape(X(xxx+1:xxx+(l7+1)*l8),l7+1,l8); 151 | xxx = xxx+(l7+1)*l8; 152 | w8 = reshape(X(xxx+1:xxx+(l8+1)*l9),l8+1,l9); 153 | 154 | %%%%%%%%%%%%%%% END OF CONJUGATE GRADIENT WITH 3 LINESEARCHES %%%%%%%%%%%%%%%%%%%%%%%%%%%%% 155 | 156 | end 157 | 158 | save mnist_weights w1 w2 w4 w5 w7 w8 159 | save mnist_error test_err train_err; 160 | 161 | end 162 | 163 | 164 | 165 | -------------------------------------------------------------------------------- /Deep Belief Networks/converter.m: -------------------------------------------------------------------------------- 1 | % Version 1.000 2 | % 3 | % Code provided by Ruslan Salakhutdinov and Geoff Hinton 4 | % 5 | % Permission is granted for anyone to copy, use, modify, or distribute this 6 | % program and accompanying programs and documents for any purpose, provided 7 | % this copyright notice is retained and prominently displayed, along with 8 | % a note saying that the original programs are available from our 9 | % web page. 10 | % The programs and documents are distributed without any warranty, express or 11 | % implied. As the programs were written for research purposes only, they have 12 | % not been tested to the degree that would be advisable in any important 13 | % application. All use of these programs is entirely at the user's own risk. 14 | 15 | % This program reads raw MNIST files available at 16 | % http://yann.lecun.com/exdb/mnist/ 17 | % and converts them to files in matlab format 18 | % Before using this program you first need to download files: 19 | % train-images-idx3-ubyte.gz train-labels-idx1-ubyte.gz 20 | % t10k-images-idx3-ubyte.gz t10k-labels-idx1-ubyte.gz 21 | % and gunzip them. You need to allocate some space for this. 22 | 23 | % This program was originally written by Yee Whye Teh 24 | 25 | % Work with test files first 26 | fprintf(1,'You first need to download files:\n train-images-idx3-ubyte.gz\n train-labels-idx1-ubyte.gz\n t10k-images-idx3-ubyte.gz\n t10k-labels-idx1-ubyte.gz\n from http://yann.lecun.com/exdb/mnist/\n and gunzip them \n'); 27 | 28 | f = fopen('t10k-images.idx3-ubyte','r'); 29 | [a,count] = fread(f,4,'int32'); 30 | 31 | g = fopen('t10k-labels.idx1-ubyte','r'); 32 | [l,count] = fread(g,2,'int32'); 33 | 34 | fprintf(1,'Starting to convert Test MNIST images (prints 10 dots) \n'); 35 | n = 1000; 36 | 37 | Df = cell(1,10); 38 | for d=0:9, 39 | Df{d+1} = fopen(['test' num2str(d) '.ascii'],'w'); 40 | end; 41 | 42 | for i=1:10, 43 | fprintf('.'); 44 | rawimages = fread(f,28*28*n,'uchar'); 45 | rawlabels = fread(g,n,'uchar'); 46 | rawimages = reshape(rawimages,28*28,n); 47 | 48 | for j=1:n, 49 | fprintf(Df{rawlabels(j)+1},'%3d ',rawimages(:,j)); 50 | fprintf(Df{rawlabels(j)+1},'\n'); 51 | end; 52 | end; 53 | 54 | fprintf(1,'\n'); 55 | for d=0:9, 56 | fclose(Df{d+1}); 57 | D = load(['test' num2str(d) '.ascii'],'-ascii'); 58 | fprintf('%5d Digits of class %d\n',size(D,1),d); 59 | save(['test' num2str(d) '.mat'],'D','-mat'); 60 | end; 61 | 62 | 63 | % Work with trainig files second 64 | f = fopen('train-images.idx3-ubyte','r'); 65 | [a,count] = fread(f,4,'int32'); 66 | 67 | g = fopen('train-labels.idx1-ubyte','r'); 68 | [l,count] = fread(g,2,'int32'); 69 | 70 | fprintf(1,'Starting to convert Training MNIST images (prints 60 dots)\n'); 71 | n = 1000; 72 | 73 | Df = cell(1,10); 74 | for d=0:9, 75 | Df{d+1} = fopen(['digit' num2str(d) '.ascii'],'w'); 76 | end; 77 | 78 | for i=1:60, 79 | fprintf('.'); 80 | rawimages = fread(f,28*28*n,'uchar'); 81 | rawlabels = fread(g,n,'uchar'); 82 | rawimages = reshape(rawimages,28*28,n); 83 | 84 | for j=1:n, 85 | fprintf(Df{rawlabels(j)+1},'%3d ',rawimages(:,j)); 86 | fprintf(Df{rawlabels(j)+1},'\n'); 87 | end; 88 | end; 89 | 90 | fprintf(1,'\n'); 91 | for d=0:9, 92 | fclose(Df{d+1}); 93 | D = load(['digit' num2str(d) '.ascii'],'-ascii'); 94 | fprintf('%5d Digits of class %d\n',size(D,1),d); 95 | save(['digit' num2str(d) '.mat'],'D','-mat'); 96 | end; 97 | 98 | dos('rm *.ascii'); 99 | -------------------------------------------------------------------------------- /Deep Belief Networks/csi2batch.m: -------------------------------------------------------------------------------- 1 | load csidata; 2 | 3 | %10*30*50 4 | 5 | numcases=10; 6 | numdims=30; 7 | numbatches=numtot/numcases; 8 | batchdata = zeros(numcases, numdims, numbatches); 9 | for b=1:numbatches 10 | for c=1:numcases 11 | batchdata(c,:,b) = csi(1,:,(b-1)*numcases+c); 12 | end 13 | end 14 | test_numbatches=(600-numtot)/numcases; 15 | testbatchdata = zeros(numcases, numdims, test_numbatches); 16 | for b=1:test_numbatches 17 | for c=1:numcases 18 | testbatchdata(c,:,b) = test_csi(1,:,(b-1)*numcases+c); 19 | end 20 | end 21 | 22 | save batchdata batchdata testbatchdata; 23 | clear all -------------------------------------------------------------------------------- /Deep Belief Networks/csidata.asv: -------------------------------------------------------------------------------- 1 | clc 2 | clear 3 | csi_trace = read_bf_file('x13'); 4 | 5 | numtot=500; 6 | for i=1:numtot 7 | csi_entry = csi_trace{i}; 8 | csientry = get_scaled_csi(csi_entry); 9 | perm = csi_entry.perm; 10 | csi=zeros(3,30,numtot); 11 | time_csi=zeros(3,30,numtot); 12 | for k=1:3 13 | if perm(k)==1 14 | csi(1,:,i)=csientry(1,perm(k),:); 15 | time_csi(1,:,i)=ifft(csi(1,:,i)); 16 | elseif perm(k)==2 17 | csi(2,:,i)=csientry(1,perm(k),:); 18 | time_csi(2,:,i)=ifft(csi(2,:,i)); 19 | elseif perm(k)==3 20 | csi(3,:,i)=csientry(1,perm(k),:); 21 | time_csi(3,:,i)=ifft(csi(3,:,i)); 22 | end 23 | end 24 | end 25 | save csidata -------------------------------------------------------------------------------- /Deep Belief Networks/csidata.m: -------------------------------------------------------------------------------- 1 | csi_trace = read_bf_file('x13'); 2 | 3 | numtot=500; 4 | csi=zeros(3,30,numtot); 5 | time_csi=zeros(3,30,numtot); 6 | for i=1:numtot 7 | csi_entry = csi_trace{i}; 8 | csientry = get_scaled_csi(csi_entry); 9 | perm = csi_entry.perm; 10 | for k=1:3 11 | if perm(k)==1 12 | csi(1,:,i)=csientry(1,perm(k),:); 13 | time_csi(1,:,i)=ifft(csi(1,:,i)); 14 | elseif perm(k)==2 15 | csi(2,:,i)=csientry(1,perm(k),:); 16 | time_csi(2,:,i)=ifft(csi(2,:,i)); 17 | elseif perm(k)==3 18 | csi(3,:,i)=csientry(1,perm(k),:); 19 | time_csi(3,:,i)=ifft(csi(3,:,i)); 20 | end 21 | end 22 | end 23 | test_csi=zeros(3,30,600-numtot); 24 | test_time_csi=zeros(3,30,600-numtot); 25 | for i=numtot:600 26 | csi_entry = csi_trace{i}; 27 | csientry = get_scaled_csi(csi_entry); 28 | perm = csi_entry.perm; 29 | for k=1:3 30 | if perm(k)==1 31 | test_csi(1,:,i)=csientry(1,perm(k),:); 32 | test_time_csi(1,:,i)=ifft(test_csi(1,:,i)); 33 | elseif perm(k)==2 34 | test_csi(2,:,i)=csientry(1,perm(k),:); 35 | test_time_csi(2,:,i)=ifft(test_csi(2,:,i)); 36 | elseif perm(k)==3 37 | test_csi(3,:,i)=csientry(1,perm(k),:); 38 | test_time_csi(3,:,i)=ifft(test_csi(3,:,i)); 39 | end 40 | end 41 | end 42 | save csidata numtot csi time_csi test_csi test_time_csi; 43 | clear all -------------------------------------------------------------------------------- /Deep Belief Networks/dbinv.m: -------------------------------------------------------------------------------- 1 | %DBINV Convert from decibels. 2 | % 3 | % (c) 2008-2011 Daniel Halperin 4 | % 5 | function ret = dbinv(x) 6 | ret = 10.^(x/10); 7 | end 8 | -------------------------------------------------------------------------------- /Deep Belief Networks/get_scaled_csi.m: -------------------------------------------------------------------------------- 1 | %GET_SCALED_CSI Converts a CSI struct to a channel matrix H. 2 | % 3 | % (c) 2008-2011 Daniel Halperin 4 | % 5 | function ret = get_scaled_csi(csi_st) 6 | % Pull out CSI 7 | csi = csi_st.csi; 8 | 9 | % Calculate the scale factor between normalized CSI and RSSI (mW) 10 | csi_sq = csi .* conj(csi); 11 | csi_pwr = sum(csi_sq(:)); 12 | rssi_pwr = dbinv(get_total_rss(csi_st)); 13 | % Scale CSI -> Signal power : rssi_pwr / (mean of csi_pwr) 14 | scale = rssi_pwr / (csi_pwr / 30); 15 | 16 | % Thermal noise might be undefined if the trace was 17 | % captured in monitor mode. 18 | % ... If so, set it to -92 19 | if (csi_st.noise == -127) 20 | noise_db = -92; 21 | else 22 | noise_db = csi_st.noise; 23 | end 24 | thermal_noise_pwr = dbinv(noise_db); 25 | 26 | % Quantization error: the coefficients in the matrices are 27 | % 8-bit signed numbers, max 127/-128 to min 0/1. Given that Intel 28 | % only uses a 6-bit ADC, I expect every entry to be off by about 29 | % +/- 1 (total across real & complex parts) per entry. 30 | % 31 | % The total power is then 1^2 = 1 per entry, and there are 32 | % Nrx*Ntx entries per carrier. We only want one carrier's worth of 33 | % error, since we only computed one carrier's worth of signal above. 34 | quant_error_pwr = scale * (csi_st.Nrx * csi_st.Ntx); 35 | 36 | % Total noise and error power 37 | total_noise_pwr = thermal_noise_pwr + quant_error_pwr; 38 | 39 | % Ret now has units of sqrt(SNR) just like H in textbooks 40 | ret = csi * sqrt(scale / total_noise_pwr); 41 | if csi_st.Ntx == 2 42 | ret = ret * sqrt(2); 43 | elseif csi_st.Ntx == 3 44 | % Note: this should be sqrt(3)~ 4.77 dB. But, 4.5 dB is how 45 | % Intel (and some other chip makers) approximate a factor of 3 46 | % 47 | % You may need to change this if your card does the right thing. 48 | ret = ret * sqrt(dbinv(4.5)); 49 | end 50 | end 51 | -------------------------------------------------------------------------------- /Deep Belief Networks/get_total_rss.m: -------------------------------------------------------------------------------- 1 | %GET_TOTAL_RSS Calculates the Received Signal Strength (RSS) in dBm from 2 | % a CSI struct. 3 | % 4 | % (c) 2011 Daniel Halperin 5 | % 6 | function ret = get_total_rss(csi_st) 7 | error(nargchk(1,1,nargin)); 8 | 9 | % Careful here: rssis could be zero 10 | rssi_mag = 0; 11 | if csi_st.rssi_a ~= 0 12 | rssi_mag = rssi_mag + dbinv(csi_st.rssi_a); 13 | end 14 | if csi_st.rssi_b ~= 0 15 | rssi_mag = rssi_mag + dbinv(csi_st.rssi_b); 16 | end 17 | if csi_st.rssi_c ~= 0 18 | rssi_mag = rssi_mag + dbinv(csi_st.rssi_c); 19 | end 20 | 21 | ret = db(rssi_mag, 'pow') - 44 - csi_st.agc; 22 | end -------------------------------------------------------------------------------- /Deep Belief Networks/getbatchdata.m: -------------------------------------------------------------------------------- 1 | csi_trace = read_bf_file('x55'); 2 | 3 | numtot=400; 4 | csi=zeros(3,30,numtot); 5 | time_csi=zeros(3,30,numtot); 6 | for i=1:numtot 7 | csi_entry = csi_trace{i}; 8 | csientry = get_scaled_csi(csi_entry); 9 | perm = csi_entry.perm; 10 | for k=1:3 11 | if perm(k)==1 12 | csi(1,:,i)=csientry(1,perm(k),:); 13 | time_csi(1,:,i)=ifft(csi(1,:,i)); 14 | elseif perm(k)==2 15 | csi(2,:,i)=csientry(1,perm(k),:); 16 | time_csi(2,:,i)=ifft(csi(2,:,i)); 17 | elseif perm(k)==3 18 | csi(3,:,i)=csientry(1,perm(k),:); 19 | time_csi(3,:,i)=ifft(csi(3,:,i)); 20 | end 21 | end 22 | end 23 | 24 | numcases=5; 25 | numdims=30; 26 | numbatches=numtot/numcases; 27 | batchdata = zeros(numcases, numdims, numbatches); 28 | for b=1:numbatches 29 | for c=1:numcases 30 | % batchdata(c,:,b) = csi(1,:,(b-1)*numcases+c); 31 | batchdata(c,:,b) = (abs(squeeze(csi(1,:,(b-1)*numcases+c)))); 32 | end 33 | end 34 | batchdata=batchdata/max(max(max(batchdata))); 35 | save batchdata55 batchdata; 36 | clear all -------------------------------------------------------------------------------- /Deep Belief Networks/gettestbatchdata.asv: -------------------------------------------------------------------------------- 1 | csi_trace = read_bf_file('x55'); 2 | 3 | numtot=500; 4 | numall=600; 5 | test_csi=zeros(3,30,numall-numtot); 6 | test_time_csi=zeros(3,30,numall-numtot); 7 | for i=(numtot+1):numall 8 | csi_entry = csi_trace{i}; 9 | csientry = get_scaled_csi(csi_entry); 10 | perm = csi_entry.perm; 11 | for k=1:3 12 | if perm(k)==1 13 | test_csi(1,:,i-numtot)=csientry(1,perm(k),:); 14 | test_time_csi(1,:,i-numtot)=ifft(test_csi(1,:,i-numtot)); 15 | elseif perm(k)==2 16 | test_csi(2,:,i-numtot)=csientry(1,perm(k),:); 17 | test_time_csi(2,:,i-numtot)=ifft(test_csi(2,:,i-numtot)); 18 | elseif perm(k)==3 19 | test_csi(3,:,i-numtot)=csientry(1,perm(k),:); 20 | test_time_csi(3,:,i-numtot)=ifft(test_csi(3,:,i-numtot)); 21 | end 22 | end 23 | end 24 | 25 | numcases=1; 26 | numdims=30; 27 | test_numbatches=(numall-numtot)/numcases; 28 | testbatchdata = zeros(numcases, numdims, test_numbatches); 29 | for b=1:test_numbatches 30 | for c=1:numcases 31 | % testbatchdata(c,:,b) = test_csi(1,:,(b-1)*numcases+c); 32 | testbatchdata(c,:,b) = (abs(squeeze(test_csi(1,:,(b-1)*numcases+c)))); 33 | end 34 | end 35 | 36 | save testbatchdata55 testbatchdata; 37 | clear all -------------------------------------------------------------------------------- /Deep Belief Networks/gettestbatchdata.m: -------------------------------------------------------------------------------- 1 | csi_trace = read_bf_file('x56'); % 2 | 3 | numtot=300; % 4 | numall=400; % 5 | test_csi=zeros(3,30,numall-numtot); 6 | test_time_csi=zeros(3,30,numall-numtot); 7 | for i=(numtot+1):numall 8 | csi_entry = csi_trace{i}; 9 | csientry = get_scaled_csi(csi_entry); 10 | perm = csi_entry.perm; 11 | for k=1:3 12 | if perm(k)==1 13 | test_csi(1,:,i-numtot)=csientry(1,perm(k),:); 14 | test_time_csi(1,:,i-numtot)=ifft(test_csi(1,:,i-numtot)); 15 | elseif perm(k)==2 16 | test_csi(2,:,i-numtot)=csientry(1,perm(k),:); 17 | test_time_csi(2,:,i-numtot)=ifft(test_csi(2,:,i-numtot)); 18 | elseif perm(k)==3 19 | test_csi(3,:,i-numtot)=csientry(1,perm(k),:); 20 | test_time_csi(3,:,i-numtot)=ifft(test_csi(3,:,i-numtot)); 21 | end 22 | end 23 | end 24 | 25 | numcases=5; % 26 | numdims=30; 27 | test_numbatches=(numall-numtot)/numcases; 28 | testbatchdata = zeros(numcases, numdims, test_numbatches); 29 | for b=1:test_numbatches 30 | for c=1:numcases 31 | % testbatchdata(c,:,b) = test_csi(1,:,(b-1)*numcases+c); 32 | testbatchdata(c,:,b) = (abs(squeeze(test_csi(1,:,(b-1)*numcases+c)))); 33 | end 34 | end 35 | testbatchdata=testbatchdata/max(max(max(testbatchdata))); 36 | save testbatchdata56 testbatchdata; % 37 | clear all 38 | fprintf(1,'finished'); -------------------------------------------------------------------------------- /Deep Belief Networks/longdata: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mars920314/DeepFi/9e7f99c181616d9aa4db18973c08675bdb714e8c/Deep Belief Networks/longdata -------------------------------------------------------------------------------- /Deep Belief Networks/main.asv: -------------------------------------------------------------------------------- 1 | clear all 2 | close all 3 | clc 4 | 5 | % csidata; 6 | % csi2batch; 7 | getbatchdata; 8 | gettestbatchdata; 9 | fprintf(1,'finished'); 10 | %% 11 | load batchdatalong; 12 | batchdata=batchdata/max(max(max(batchdata))); 13 | [numcases numdims numbatches]=size(batchdata); 14 | maxepoch=4; %In the Science paper we use maxepoch=50, but it works just fine. 15 | numhid=1000; numpen=500; numpen2=250; numopen=30; 16 | 17 | fprintf(1,'Pretraining Layer 1 with RBM: %d-%d \n',numdims,numhid); 18 | restart=1; 19 | rbm; 20 | hidrecbiases=hidbiases; 21 | save mnistvh vishid hidrecbiases visbiases; 22 | %% 23 | 24 | fprintf(1,'\nPretraining Layer 2 with RBM: %d-%d \n',numhid,numpen); 25 | batchdata=batchposhidprobs; 26 | numhid=numpen; 27 | restart=1; 28 | rbm; 29 | hidpen=vishid; penrecbiases=hidbiases; hidgenbiases=visbiases; 30 | save mnisthp hidpen penrecbiases hidgenbiases; 31 | %% 32 | fprintf(1,'\nPretraining Layer 3 with RBM: %d-%d \n',numpen,numpen2); 33 | batchdata=batchposhidprobs; 34 | numhid=numpen2; 35 | restart=1; 36 | rbm; 37 | hidpen2=vishid; penrecbiases2=hidbiases; hidgenbiases2=visbiases; 38 | save mnisthp2 hidpen2 penrecbiases2 hidgenbiases2; 39 | %% 40 | fprintf(1,'\nPretraining Layer 4 with RBM: %d-%d \n',numpen2,numopen); 41 | batchdata=batchposhidprobs; 42 | numhid=numopen; 43 | restart=1; 44 | rbmhidlinear; 45 | hidtop=vishid; toprecbiases=hidbiases; topgenbiases=visbiases; 46 | save mnistpo hidtop toprecbiases topgenbiases; 47 | % clear all 48 | fprintf(1,'finished'); 49 | %% 50 | load batchdatalong; 51 | % load testbatchdatalong; 52 | load testbatchdata44; 53 | backprop; 54 | testerror(); -------------------------------------------------------------------------------- /Deep Belief Networks/main.m: -------------------------------------------------------------------------------- 1 | clear all 2 | close all 3 | clc 4 | 5 | % csidata; 6 | % csi2batch; 7 | getbatchdata; 8 | gettestbatchdata; 9 | fprintf(1,'finished'); 10 | %% 11 | load batchdatalong; 12 | [numcases numdims numbatches]=size(batchdata); 13 | maxepoch=4; %In the Science paper we use maxepoch=50, but it works just fine. 14 | numhid=100; numpen=50; numpen2=30; numopen=30; 15 | 16 | fprintf(1,'Pretraining Layer 1 with RBM: %d-%d \n',numdims,numhid); 17 | restart=1; 18 | rbm; 19 | hidrecbiases=hidbiases; 20 | save mnistvh vishid hidrecbiases visbiases; 21 | %% 22 | 23 | fprintf(1,'\nPretraining Layer 2 with RBM: %d-%d \n',numhid,numpen); 24 | batchdata=batchposhidprobs; 25 | numhid=numpen; 26 | restart=1; 27 | rbm; 28 | hidpen=vishid; penrecbiases=hidbiases; hidgenbiases=visbiases; 29 | save mnisthp hidpen penrecbiases hidgenbiases; 30 | %% 31 | fprintf(1,'\nPretraining Layer 3 with RBM: %d-%d \n',numpen,numpen2); 32 | batchdata=batchposhidprobs; 33 | numhid=numpen2; 34 | restart=1; 35 | rbm; 36 | hidpen2=vishid; penrecbiases2=hidbiases; hidgenbiases2=visbiases; 37 | save mnisthp2 hidpen2 penrecbiases2 hidgenbiases2; 38 | %% 39 | fprintf(1,'\nPretraining Layer 4 with RBM: %d-%d \n',numpen2,numopen); 40 | batchdata=batchposhidprobs; 41 | numhid=numopen; 42 | restart=1; 43 | rbmhidlinear; 44 | hidtop=vishid; toprecbiases=hidbiases; topgenbiases=visbiases; 45 | save mnistpo hidtop toprecbiases topgenbiases; 46 | % clear all 47 | fprintf(1,'finished'); 48 | %% 49 | load batchdata55; 50 | load testbatchdata55; 51 | % load testbatchdata44; 52 | backprop; 53 | % testerror('testbatchdata13'); -------------------------------------------------------------------------------- /Deep Belief Networks/main_test.m: -------------------------------------------------------------------------------- 1 | clear all 2 | close all 3 | clc 4 | 5 | % csidata; 6 | % csi2batch; 7 | getbatchdata; 8 | gettestbatchdata; 9 | fprintf(1,'finished'); 10 | %% 11 | load batchdatalong; 12 | batchdata=batchdata/max(max(max(batchdata))); 13 | [numcases numdims numbatches]=size(batchdata); 14 | maxepoch=10; %In the Science paper we use maxepoch=50, but it works just fine. 15 | numhid=100; numpen=50; numpen2=30; numopen=40; 16 | 17 | fprintf(1,'Pretraining Layer 1 with RBM: %d-%d \n',numdims,numhid); 18 | restart=1; 19 | rbm; 20 | hidrecbiases=hidbiases; 21 | save mnistvh vishid hidrecbiases visbiases; 22 | 23 | fprintf(1,'\nPretraining Layer 2 with RBM: %d-%d \n',numhid,numpen); 24 | batchdata=batchposhidprobs; 25 | numhid=numpen; 26 | restart=1; 27 | rbm; 28 | hidpen=vishid; penrecbiases=hidbiases; hidgenbiases=visbiases; 29 | save mnisthp hidpen penrecbiases hidgenbiases; 30 | 31 | % fprintf(1,'\nPretraining Layer 3 with RBM: %d-%d \n',numpen,numpen2); 32 | % batchdata=batchposhidprobs; 33 | % numhid=numpen2; 34 | % restart=1; 35 | % rbm; 36 | % hidpen2=vishid; penrecbiases2=hidbiases; hidgenbiases2=visbiases; 37 | % save mnisthp2 hidpen2 penrecbiases2 hidgenbiases2; 38 | 39 | fprintf(1,'\nPretraining Layer 4 with RBM: %d-%d \n',numpen2,numopen); 40 | batchdata=batchposhidprobs; 41 | numhid=numopen; 42 | restart=1; 43 | rbmhidlinear; 44 | hidtop=vishid; toprecbiases=hidbiases; topgenbiases=visbiases; 45 | save mnistpo hidtop toprecbiases topgenbiases; 46 | % clear all 47 | fprintf(1,'finished'); 48 | %% 49 | load batchdatalong; 50 | load testbatchdatalong; 51 | backpropnew; 52 | -------------------------------------------------------------------------------- /Deep Belief Networks/makebatches.m: -------------------------------------------------------------------------------- 1 | % Version 1.000 2 | % 3 | % Code provided by Ruslan Salakhutdinov and Geoff Hinton 4 | % 5 | % Permission is granted for anyone to copy, use, modify, or distribute this 6 | % program and accompanying programs and documents for any purpose, provided 7 | % this copyright notice is retained and prominently displayed, along with 8 | % a note saying that the original programs are available from our 9 | % web page. 10 | % The programs and documents are distributed without any warranty, express or 11 | % implied. As the programs were written for research purposes only, they have 12 | % not been tested to the degree that would be advisable in any important 13 | % application. All use of these programs is entirely at the user's own risk. 14 | 15 | digitdata=[]; 16 | targets=[]; 17 | load digit0; digitdata = [digitdata; D]; targets = [targets; repmat([1 0 0 0 0 0 0 0 0 0], size(D,1), 1)]; 18 | load digit1; digitdata = [digitdata; D]; targets = [targets; repmat([0 1 0 0 0 0 0 0 0 0], size(D,1), 1)]; 19 | load digit2; digitdata = [digitdata; D]; targets = [targets; repmat([0 0 1 0 0 0 0 0 0 0], size(D,1), 1)]; 20 | load digit3; digitdata = [digitdata; D]; targets = [targets; repmat([0 0 0 1 0 0 0 0 0 0], size(D,1), 1)]; 21 | load digit4; digitdata = [digitdata; D]; targets = [targets; repmat([0 0 0 0 1 0 0 0 0 0], size(D,1), 1)]; 22 | load digit5; digitdata = [digitdata; D]; targets = [targets; repmat([0 0 0 0 0 1 0 0 0 0], size(D,1), 1)]; 23 | load digit6; digitdata = [digitdata; D]; targets = [targets; repmat([0 0 0 0 0 0 1 0 0 0], size(D,1), 1)]; 24 | load digit7; digitdata = [digitdata; D]; targets = [targets; repmat([0 0 0 0 0 0 0 1 0 0], size(D,1), 1)]; 25 | load digit8; digitdata = [digitdata; D]; targets = [targets; repmat([0 0 0 0 0 0 0 0 1 0], size(D,1), 1)]; 26 | load digit9; digitdata = [digitdata; D]; targets = [targets; repmat([0 0 0 0 0 0 0 0 0 1], size(D,1), 1)]; 27 | digitdata = digitdata/255; 28 | 29 | totnum=size(digitdata,1); 30 | fprintf(1, 'Size of the training dataset= %5d \n', totnum); 31 | 32 | rand('state',0); %so we know the permutation of the training data 33 | randomorder=randperm(totnum); 34 | 35 | numbatches=totnum/100; 36 | numdims = size(digitdata,2); 37 | batchsize = 100; 38 | batchdata = zeros(batchsize, numdims, numbatches); 39 | batchtargets = zeros(batchsize, 10, numbatches); 40 | 41 | for b=1:numbatches 42 | batchdata(:,:,b) = digitdata(randomorder(1+(b-1)*batchsize:b*batchsize), :); 43 | batchtargets(:,:,b) = targets(randomorder(1+(b-1)*batchsize:b*batchsize), :); 44 | end; 45 | clear digitdata targets; 46 | 47 | digitdata=[]; 48 | targets=[]; 49 | load test0; digitdata = [digitdata; D]; targets = [targets; repmat([1 0 0 0 0 0 0 0 0 0], size(D,1), 1)]; 50 | load test1; digitdata = [digitdata; D]; targets = [targets; repmat([0 1 0 0 0 0 0 0 0 0], size(D,1), 1)]; 51 | load test2; digitdata = [digitdata; D]; targets = [targets; repmat([0 0 1 0 0 0 0 0 0 0], size(D,1), 1)]; 52 | load test3; digitdata = [digitdata; D]; targets = [targets; repmat([0 0 0 1 0 0 0 0 0 0], size(D,1), 1)]; 53 | load test4; digitdata = [digitdata; D]; targets = [targets; repmat([0 0 0 0 1 0 0 0 0 0], size(D,1), 1)]; 54 | load test5; digitdata = [digitdata; D]; targets = [targets; repmat([0 0 0 0 0 1 0 0 0 0], size(D,1), 1)]; 55 | load test6; digitdata = [digitdata; D]; targets = [targets; repmat([0 0 0 0 0 0 1 0 0 0], size(D,1), 1)]; 56 | load test7; digitdata = [digitdata; D]; targets = [targets; repmat([0 0 0 0 0 0 0 1 0 0], size(D,1), 1)]; 57 | load test8; digitdata = [digitdata; D]; targets = [targets; repmat([0 0 0 0 0 0 0 0 1 0], size(D,1), 1)]; 58 | load test9; digitdata = [digitdata; D]; targets = [targets; repmat([0 0 0 0 0 0 0 0 0 1], size(D,1), 1)]; 59 | digitdata = digitdata/255; 60 | 61 | totnum=size(digitdata,1); 62 | fprintf(1, 'Size of the test dataset= %5d \n', totnum); 63 | 64 | rand('state',0); %so we know the permutation of the training data 65 | randomorder=randperm(totnum); 66 | 67 | numbatches=totnum/100; 68 | numdims = size(digitdata,2); 69 | batchsize = 100; 70 | testbatchdata = zeros(batchsize, numdims, numbatches); 71 | testbatchtargets = zeros(batchsize, 10, numbatches); 72 | 73 | for b=1:numbatches 74 | testbatchdata(:,:,b) = digitdata(randomorder(1+(b-1)*batchsize:b*batchsize), :); 75 | testbatchtargets(:,:,b) = targets(randomorder(1+(b-1)*batchsize:b*batchsize), :); 76 | end; 77 | clear digitdata targets; 78 | 79 | 80 | %%% Reset random seeds 81 | rand('state',sum(100*clock)); 82 | randn('state',sum(100*clock)); 83 | 84 | save middata batchdata batchtargets testbatchdata testbatchtargets 85 | 86 | -------------------------------------------------------------------------------- /Deep Belief Networks/mnistclassify.m: -------------------------------------------------------------------------------- 1 | % Version 1.000 2 | % 3 | % Code provided by Ruslan Salakhutdinov and Geoff Hinton 4 | % 5 | % Permission is granted for anyone to copy, use, modify, or distribute this 6 | % program and accompanying programs and documents for any purpose, provided 7 | % this copyright notice is retained and prominently displayed, along with 8 | % a note saying that the original programs are available from our 9 | % web page. 10 | % The programs and documents are distributed without any warranty, express or 11 | % implied. As the programs were written for research purposes only, they have 12 | % not been tested to the degree that would be advisable in any important 13 | % application. All use of these programs is entirely at the user's own risk. 14 | 15 | 16 | % This program pretrains a deep autoencoder for MNIST dataset 17 | % You can set the maximum number of epochs for pretraining each layer 18 | % and you can set the architecture of the multilayer net. 19 | 20 | clear all 21 | close all 22 | 23 | maxepoch=50; 24 | numhid=500; numpen=500; numpen2=2000; 25 | 26 | fprintf(1,'Converting Raw files into Matlab format \n'); 27 | converter; 28 | 29 | fprintf(1,'Pretraining a deep autoencoder. \n'); 30 | fprintf(1,'The Science paper used 50 epochs. This uses %3i \n', maxepoch); 31 | 32 | makebatches; 33 | [numcases numdims numbatches]=size(batchdata); 34 | 35 | fprintf(1,'Pretraining Layer 1 with RBM: %d-%d \n',numdims,numhid); 36 | restart=1; 37 | rbm; 38 | hidrecbiases=hidbiases; 39 | save mnistvhclassify vishid hidrecbiases visbiases; 40 | 41 | fprintf(1,'\nPretraining Layer 2 with RBM: %d-%d \n',numhid,numpen); 42 | batchdata=batchposhidprobs; 43 | numhid=numpen; 44 | restart=1; 45 | rbm; 46 | hidpen=vishid; penrecbiases=hidbiases; hidgenbiases=visbiases; 47 | save mnisthpclassify hidpen penrecbiases hidgenbiases; 48 | 49 | fprintf(1,'\nPretraining Layer 3 with RBM: %d-%d \n',numpen,numpen2); 50 | batchdata=batchposhidprobs; 51 | numhid=numpen2; 52 | restart=1; 53 | rbm; 54 | hidpen2=vishid; penrecbiases2=hidbiases; hidgenbiases2=visbiases; 55 | save mnisthp2classify hidpen2 penrecbiases2 hidgenbiases2; 56 | 57 | backpropclassify; 58 | 59 | -------------------------------------------------------------------------------- /Deep Belief Networks/mnistdeepauto.asv: -------------------------------------------------------------------------------- 1 | % Version 1.000 2 | % 3 | % Code provided by Ruslan Salakhutdinov and Geoff Hinton 4 | % 5 | % Permission is granted for anyone to copy, use, modify, or distribute this 6 | % program and accompanying programs and documents for any purpose, provided 7 | % this copyright notice is retained and prominently displayed, along with 8 | % a note saying that the original programs are available from our 9 | % web page. 10 | % The programs and documents are distributed without any warranty, express or 11 | % implied. As the programs were written for research purposes only, they have 12 | % not been tested to the degree that would be advisable in any important 13 | % application. All use of these programs is entirely at the user's own risk. 14 | 15 | 16 | % This program pretrains a deep autoencoder for MNIST dataset 17 | % You can set the maximum number of epochs for pretraining each layer 18 | % and you can set the architecture of the multilayer net. 19 | 20 | clear all 21 | close all 22 | 23 | maxepoch=10; %In the Science paper we use maxepoch=50, but it works just fine. 24 | numhid=1000; numpen=500; numpen2=250; numopen=30; 25 | 26 | fprintf(1,'Converting Raw files into Matlab format \n'); 27 | converter; 28 | 29 | fprintf(1,'Pretraining a deep autoencoder. \n'); 30 | fprintf(1,'The Science paper used 50 epochs. This uses %3i \n', maxepoch); 31 | 32 | makebatches; 33 | clear all; load middata; 34 | [numcases numdims numbatches]=size(batchdata); 35 | %% 36 | fprintf(1,'Pretraining Layer 1 with RBM: %d-%d \n',numdims,numhid); 37 | restart=1; 38 | rbm; 39 | hidrecbiases=hidbiases; 40 | save mnistvh vishid hidrecbiases visbiases; 41 | %% 42 | fprintf(1,'\nPretraining Layer 2 with RBM: %d-%d \n',numhid,numpen); 43 | batchdata=batchposhidprobs; 44 | numhid=numpen; 45 | restart=1; 46 | rbm; 47 | hidpen=vishid; penrecbiases=hidbiases; hidgenbiases=visbiases; 48 | save mnisthp hidpen penrecbiases hidgenbiases; 49 | %% 50 | fprintf(1,'\nPretraining Layer 3 with RBM: %d-%d \n',numpen,numpen2); 51 | batchdata=batchposhidprobs; 52 | numhid=numpen2; 53 | restart=1; 54 | rbm; 55 | hidpen2=vishid; penrecbiases2=hidbiases; hidgenbiases2=visbiases; 56 | save mnisthp2 hidpen2 penrecbiases2 hidgenbiases2; 57 | %% 58 | fprintf(1,'\nPretraining Layer 4 with RBM: %d-%d \n',numpen2,numopen); 59 | batchdata=batchposhidprobs; 60 | numhid=numopen; 61 | restart=1; 62 | rbmhidlinear; 63 | hidtop=vishid; toprecbiases=hidbiases; topgenbiases=visbiases; 64 | save mnistpo hidtop toprecbiases topgenbiases; 65 | %% 66 | backprop; 67 | 68 | -------------------------------------------------------------------------------- /Deep Belief Networks/mnistdeepauto.m: -------------------------------------------------------------------------------- 1 | % Version 1.000 2 | % 3 | % Code provided by Ruslan Salakhutdinov and Geoff Hinton 4 | % 5 | % Permission is granted for anyone to copy, use, modify, or distribute this 6 | % program and accompanying programs and documents for any purpose, provided 7 | % this copyright notice is retained and prominently displayed, along with 8 | % a note saying that the original programs are available from our 9 | % web page. 10 | % The programs and documents are distributed without any warranty, express or 11 | % implied. As the programs were written for research purposes only, they have 12 | % not been tested to the degree that would be advisable in any important 13 | % application. All use of these programs is entirely at the user's own risk. 14 | 15 | 16 | % This program pretrains a deep autoencoder for MNIST dataset 17 | % You can set the maximum number of epochs for pretraining each layer 18 | % and you can set the architecture of the multilayer net. 19 | 20 | clear all 21 | close all 22 | 23 | fprintf(1,'Converting Raw files into Matlab format \n'); 24 | converter; 25 | 26 | fprintf(1,'Pretraining a deep autoencoder. \n'); 27 | fprintf(1,'The Science paper used 50 epochs. This uses %3i \n', maxepoch); 28 | 29 | makebatches; 30 | clear all; load middata; 31 | [numcases numdims numbatches]=size(batchdata); 32 | maxepoch=10; %In the Science paper we use maxepoch=50, but it works just fine. 33 | numhid=1000; numpen=500; numpen2=250; numopen=30; 34 | %% 35 | fprintf(1,'Pretraining Layer 1 with RBM: %d-%d \n',numdims,numhid); 36 | restart=1; 37 | rbm; 38 | hidrecbiases=hidbiases; 39 | save mnistvh vishid hidrecbiases visbiases; 40 | %% 41 | fprintf(1,'\nPretraining Layer 2 with RBM: %d-%d \n',numhid,numpen); 42 | batchdata=batchposhidprobs; 43 | numhid=numpen; 44 | restart=1; 45 | rbm; 46 | hidpen=vishid; penrecbiases=hidbiases; hidgenbiases=visbiases; 47 | save mnisthp hidpen penrecbiases hidgenbiases; 48 | %% 49 | fprintf(1,'\nPretraining Layer 3 with RBM: %d-%d \n',numpen,numpen2); 50 | batchdata=batchposhidprobs; 51 | numhid=numpen2; 52 | restart=1; 53 | rbm; 54 | hidpen2=vishid; penrecbiases2=hidbiases; hidgenbiases2=visbiases; 55 | save mnisthp2 hidpen2 penrecbiases2 hidgenbiases2; 56 | %% 57 | fprintf(1,'\nPretraining Layer 4 with RBM: %d-%d \n',numpen2,numopen); 58 | batchdata=batchposhidprobs; 59 | numhid=numopen; 60 | restart=1; 61 | rbmhidlinear; 62 | hidtop=vishid; toprecbiases=hidbiases; topgenbiases=visbiases; 63 | save mnistpo hidtop toprecbiases topgenbiases; 64 | %% 65 | backprop; 66 | 67 | -------------------------------------------------------------------------------- /Deep Belief Networks/mnistdisp.m: -------------------------------------------------------------------------------- 1 | % Version 1.000 2 | % 3 | % Code provided by Ruslan Salakhutdinov and Geoff Hinton 4 | % 5 | % Permission is granted for anyone to copy, use, modify, or distribute this 6 | % program and accompanying programs and documents for any purpose, provided 7 | % this copyright notice is retained and prominently displayed, along with 8 | % a note saying that the original programs are available from our 9 | % web page. 10 | % The programs and documents are distributed without any warranty, express or 11 | % implied. As the programs were written for research purposes only, they have 12 | % not been tested to the degree that would be advisable in any important 13 | % application. All use of these programs is entirely at the user's own risk. 14 | 15 | function [err] = mnistdisp(digits); 16 | % display a group of MNIST images 17 | col=28; 18 | row=28; 19 | 20 | [dd,N] = size(digits); 21 | imdisp=zeros(2*28,ceil(N/2)*28); 22 | 23 | for nn=1:N 24 | ii=rem(nn,2); if(ii==0) ii=2; end 25 | jj=ceil(nn/2); 26 | 27 | img1 = reshape(digits(:,nn),row,col); 28 | img2(((ii-1)*row+1):(ii*row),((jj-1)*col+1):(jj*col))=img1'; 29 | end 30 | 31 | imagesc(img2,[0 1]); colormap gray; axis equal; axis off; 32 | drawnow; 33 | err=0; 34 | 35 | -------------------------------------------------------------------------------- /Deep Belief Networks/rbm.m: -------------------------------------------------------------------------------- 1 | % Version 1.000 2 | % 3 | % Code provided by Geoff Hinton and Ruslan Salakhutdinov 4 | % 5 | % Permission is granted for anyone to copy, use, modify, or distribute this 6 | % program and accompanying programs and documents for any purpose, provided 7 | % this copyright notice is retained and prominently displayed, along with 8 | % a note saying that the original programs are available from our 9 | % web page. 10 | % The programs and documents are distributed without any warranty, express or 11 | % implied. As the programs were written for research purposes only, they have 12 | % not been tested to the degree that would be advisable in any important 13 | % application. All use of these programs is entirely at the user's own risk. 14 | 15 | % This program trains Restricted Boltzmann Machine in which 16 | % visible, binary, stochastic pixels are connected to 17 | % hidden, binary, stochastic feature detectors using symmetrically 18 | % weighted connections. Learning is done with 1-step Contrastive Divergence. 19 | % The program assumes that the following variables are set externally: 20 | % maxepoch -- maximum number of epochs 21 | % numhid -- number of hidden units 22 | % batchdata -- the data that is divided into batches (numcases numdims numbatches) 23 | % restart -- set to 1 if learning starts from beginning 24 | 25 | epsilonw = 0.1; % Learning rate for weights 26 | epsilonvb = 0.1; % Learning rate for biases of visible units 27 | epsilonhb = 0.1; % Learning rate for biases of hidden units 28 | weightcost = 0.0002; 29 | initialmomentum = 0.5; 30 | finalmomentum = 0.9; 31 | 32 | [numcases numdims numbatches]=size(batchdata); 33 | 34 | if restart ==1, 35 | restart=0; 36 | epoch=1; 37 | 38 | % Initializing symmetric weights and biases. 39 | vishid = 0.1*randn(numdims, numhid); 40 | hidbiases = zeros(1,numhid); 41 | visbiases = zeros(1,numdims); 42 | 43 | poshidprobs = zeros(numcases,numhid); 44 | neghidprobs = zeros(numcases,numhid); 45 | posprods = zeros(numdims,numhid); 46 | negprods = zeros(numdims,numhid); 47 | vishidinc = zeros(numdims,numhid); 48 | hidbiasinc = zeros(1,numhid); 49 | visbiasinc = zeros(1,numdims); 50 | batchposhidprobs=zeros(numcases,numhid,numbatches); 51 | end 52 | 53 | for epoch = epoch:maxepoch, 54 | fprintf(1,'epoch %d\r',epoch); 55 | errsum=0; 56 | for batch = 1:numbatches, 57 | % fprintf(1,'epoch %d batch %d\r',epoch,batch); 58 | 59 | %%%%%%%%% START POSITIVE PHASE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 60 | data = batchdata(:,:,batch); 61 | poshidprobs = 1./(1 + exp(-data*vishid - repmat(hidbiases,numcases,1))); 62 | batchposhidprobs(:,:,batch)=poshidprobs; 63 | posprods = data' * poshidprobs; 64 | poshidact = sum(poshidprobs); 65 | posvisact = sum(data); 66 | 67 | %%%%%%%%% END OF POSITIVE PHASE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 68 | poshidstates = poshidprobs > rand(numcases,numhid); 69 | 70 | %%%%%%%%% START NEGATIVE PHASE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 71 | negdata = 1./(1 + exp(-poshidstates*vishid' - repmat(visbiases,numcases,1))); 72 | neghidprobs = 1./(1 + exp(-negdata*vishid - repmat(hidbiases,numcases,1))); 73 | negprods = negdata'*neghidprobs; 74 | neghidact = sum(neghidprobs); 75 | negvisact = sum(negdata); 76 | 77 | %%%%%%%%% END OF NEGATIVE PHASE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 78 | err= sum(sum( (data-negdata).^2 )); 79 | errsum = err + errsum; 80 | 81 | if epoch>5, 82 | momentum=finalmomentum; 83 | else 84 | momentum=initialmomentum; 85 | end; 86 | 87 | %%%%%%%%% UPDATE WEIGHTS AND BIASES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 88 | vishidinc = momentum*vishidinc + ... 89 | epsilonw*( (posprods-negprods)/numcases - weightcost*vishid); 90 | visbiasinc = momentum*visbiasinc + (epsilonvb/numcases)*(posvisact-negvisact); 91 | hidbiasinc = momentum*hidbiasinc + (epsilonhb/numcases)*(poshidact-neghidact); 92 | 93 | vishid = vishid + vishidinc; 94 | visbiases = visbiases + visbiasinc; 95 | hidbiases = hidbiases + hidbiasinc; 96 | 97 | %%%%%%%%%%%%%%%% END OF UPDATES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 98 | 99 | end 100 | fprintf(1, 'epoch %4i error %6.1f \n', epoch, errsum); 101 | end; 102 | -------------------------------------------------------------------------------- /Deep Belief Networks/rbmhidlinear.m: -------------------------------------------------------------------------------- 1 | % Version 1.000 2 | % 3 | % Code provided by Ruslan Salakhutdinov and Geoff Hinton 4 | % 5 | % Permission is granted for anyone to copy, use, modify, or distribute this 6 | % program and accompanying programs and documents for any purpose, provided 7 | % this copyright notice is retained and prominently displayed, along with 8 | % a note saying that the original programs are available from our 9 | % web page. 10 | % The programs and documents are distributed without any warranty, express or 11 | % implied. As the programs were written for research purposes only, they have 12 | % not been tested to the degree that would be advisable in any important 13 | % application. All use of these programs is entirely at the user's own risk. 14 | 15 | % This program trains Restricted Boltzmann Machine in which 16 | % visible, binary, stochastic pixels are connected to 17 | % hidden, tochastic real-valued feature detectors drawn from a unit 18 | % variance Gaussian whose mean is determined by the input from 19 | % the logistic visible units. Learning is done with 1-step Contrastive Divergence. 20 | % The program assumes that the following variables are set externally: 21 | % maxepoch -- maximum number of epochs 22 | % numhid -- number of hidden units 23 | % batchdata -- the data that is divided into batches (numcases numdims numbatches) 24 | % restart -- set to 1 if learning starts from beginning 25 | 26 | epsilonw = 0.001; % Learning rate for weights 27 | epsilonvb = 0.001; % Learning rate for biases of visible units 28 | epsilonhb = 0.001; % Learning rate for biases of hidden units 29 | weightcost = 0.0002; 30 | initialmomentum = 0.5; 31 | finalmomentum = 0.9; 32 | 33 | 34 | [numcases numdims numbatches]=size(batchdata); 35 | 36 | if restart ==1, 37 | restart=0; 38 | epoch=1; 39 | 40 | % Initializing symmetric weights and biases. 41 | vishid = 0.1*randn(numdims, numhid); 42 | hidbiases = zeros(1,numhid); 43 | visbiases = zeros(1,numdims); 44 | 45 | 46 | poshidprobs = zeros(numcases,numhid); 47 | neghidprobs = zeros(numcases,numhid); 48 | posprods = zeros(numdims,numhid); 49 | negprods = zeros(numdims,numhid); 50 | vishidinc = zeros(numdims,numhid); 51 | hidbiasinc = zeros(1,numhid); 52 | visbiasinc = zeros(1,numdims); 53 | sigmainc = zeros(1,numhid); 54 | batchposhidprobs=zeros(numcases,numhid,numbatches); 55 | end 56 | 57 | for epoch = epoch:maxepoch, 58 | fprintf(1,'epoch %d\r',epoch); 59 | errsum=0; 60 | 61 | for batch = 1:numbatches, 62 | % fprintf(1,'epoch %d batch %d\r',epoch,batch); 63 | 64 | %%%%%%%%% START POSITIVE PHASE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 65 | data = batchdata(:,:,batch); 66 | poshidprobs = (data*vishid) + repmat(hidbiases,numcases,1); 67 | batchposhidprobs(:,:,batch)=poshidprobs; 68 | posprods = data' * poshidprobs; 69 | poshidact = sum(poshidprobs); 70 | posvisact = sum(data); 71 | 72 | %%%%%%%%% END OF POSITIVE PHASE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 73 | poshidstates = poshidprobs+randn(numcases,numhid); 74 | 75 | %%%%%%%%% START NEGATIVE PHASE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 76 | negdata = 1./(1 + exp(-poshidstates*vishid' - repmat(visbiases,numcases,1))); 77 | neghidprobs = (negdata*vishid) + repmat(hidbiases,numcases,1); 78 | negprods = negdata'*neghidprobs; 79 | neghidact = sum(neghidprobs); 80 | negvisact = sum(negdata); 81 | 82 | %%%%%%%%% END OF NEGATIVE PHASE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 83 | 84 | 85 | err= sum(sum( (data-negdata).^2 )); 86 | errsum = err + errsum; 87 | if epoch>5, 88 | momentum=finalmomentum; 89 | else 90 | momentum=initialmomentum; 91 | end; 92 | 93 | %%%%%%%%% UPDATE WEIGHTS AND BIASES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 94 | vishidinc = momentum*vishidinc + ... 95 | epsilonw*( (posprods-negprods)/numcases - weightcost*vishid); 96 | visbiasinc = momentum*visbiasinc + (epsilonvb/numcases)*(posvisact-negvisact); 97 | hidbiasinc = momentum*hidbiasinc + (epsilonhb/numcases)*(poshidact-neghidact); 98 | vishid = vishid + vishidinc; 99 | visbiases = visbiases + visbiasinc; 100 | hidbiases = hidbiases + hidbiasinc; 101 | 102 | %%%%%%%%%%%%%%%% END OF UPDATES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 103 | 104 | end 105 | fprintf(1, 'epoch %4i error %f \n', epoch, errsum); 106 | 107 | end 108 | -------------------------------------------------------------------------------- /Deep Belief Networks/read_bf_file.m: -------------------------------------------------------------------------------- 1 | %READ_BF_FILE Reads in a file of beamforming feedback logs. 2 | % This version uses the *C* version of read_bfee, compiled with 3 | % MATLAB's MEX utility. 4 | % 5 | % (c) 2008-2011 Daniel Halperin 6 | % 7 | function ret = read_bf_file(filename) 8 | %% Input check 9 | error(nargchk(1,1,nargin)); 10 | 11 | %% Open file 12 | f = fopen(filename, 'rb'); 13 | if (f < 0) 14 | error('Couldn''t open file %s', filename); 15 | return; 16 | end 17 | 18 | status = fseek(f, 0, 'eof'); 19 | if status ~= 0 20 | [msg, errno] = ferror(f); 21 | error('Error %d seeking: %s', errno, msg); 22 | fclose(f); 23 | return; 24 | end 25 | len = ftell(f); 26 | 27 | status = fseek(f, 0, 'bof'); 28 | if status ~= 0 29 | [msg, errno] = ferror(f); 30 | error('Error %d seeking: %s', errno, msg); 31 | fclose(f); 32 | return; 33 | end 34 | 35 | %% Initialize variables 36 | ret = cell(ceil(len/95),1); % Holds the return values - 1x1 CSI is 95 bytes big, so this should be upper bound 37 | cur = 0; % Current offset into file 38 | count = 0; % Number of records output 39 | broken_perm = 0; % Flag marking whether we've encountered a broken CSI yet 40 | triangle = [1 3 6]; % What perm should sum to for 1,2,3 antennas 41 | 42 | %% Process all entries in file 43 | % Need 3 bytes -- 2 byte size field and 1 byte code 44 | while cur < (len - 3) 45 | % Read size and code 46 | field_len = fread(f, 1, 'uint16', 0, 'ieee-be'); 47 | code = fread(f,1); 48 | cur = cur+3; 49 | 50 | % If unhandled code, skip (seek over) the record and continue 51 | if (code == 187) % get beamforming or phy data 52 | bytes = fread(f, field_len-1, 'uint8=>uint8'); 53 | cur = cur + field_len - 1; 54 | if (length(bytes) ~= field_len-1) 55 | fclose(f); 56 | return; 57 | end 58 | else % skip all other info 59 | fseek(f, field_len - 1, 'cof'); 60 | cur = cur + field_len - 1; 61 | continue; 62 | end 63 | 64 | if (code == 187) %hex2dec('bb')) Beamforming matrix -- output a record 65 | count = count + 1; 66 | ret{count} = read_bfee(bytes); 67 | 68 | perm = ret{count}.perm; 69 | Nrx = ret{count}.Nrx; 70 | if Nrx == 1 % No permuting needed for only 1 antenna 71 | continue; 72 | end 73 | if sum(perm) ~= triangle(Nrx) % matrix does not contain default values 74 | if broken_perm == 0 75 | broken_perm = 1; 76 | fprintf('WARN ONCE: Found CSI (%s) with Nrx=%d and invalid perm=[%s]\n', filename, Nrx, int2str(perm)); 77 | end 78 | else 79 | ret{count}.csi(:,perm(1:Nrx),:) = ret{count}.csi(:,1:Nrx,:); 80 | end 81 | end 82 | end 83 | ret = ret(1:count); 84 | 85 | %% Close file 86 | fclose(f); 87 | end 88 | -------------------------------------------------------------------------------- /Deep Belief Networks/read_bfee.c: -------------------------------------------------------------------------------- 1 | /* 2 | * (c) 2008-2011 Daniel Halperin 3 | */ 4 | #include "mex.h" 5 | 6 | /* The computational routine */ 7 | void read_bfee(unsigned char *inBytes, mxArray *outCell) 8 | { 9 | unsigned long timestamp_low = inBytes[0] + (inBytes[1] << 8) + 10 | (inBytes[2] << 16) + (inBytes[3] << 24); 11 | unsigned short bfee_count = inBytes[4] + (inBytes[5] << 8); 12 | unsigned int Nrx = inBytes[8]; 13 | unsigned int Ntx = inBytes[9]; 14 | unsigned int rssi_a = inBytes[10]; 15 | unsigned int rssi_b = inBytes[11]; 16 | unsigned int rssi_c = inBytes[12]; 17 | char noise = inBytes[13]; 18 | unsigned int agc = inBytes[14]; 19 | unsigned int antenna_sel = inBytes[15]; 20 | unsigned int len = inBytes[16] + (inBytes[17] << 8); 21 | unsigned int fake_rate_n_flags = inBytes[18] + (inBytes[19] << 8); 22 | unsigned int calc_len = (30 * (Nrx * Ntx * 8 * 2 + 3) + 7) / 8; 23 | unsigned int i, j; 24 | unsigned int index = 0, remainder; 25 | unsigned char *payload = &inBytes[20]; 26 | char tmp; 27 | int size[] = {Ntx, Nrx, 30}; 28 | mxArray *csi = mxCreateNumericArray(3, size, mxDOUBLE_CLASS, mxCOMPLEX); 29 | double* ptrR = (double *)mxGetPr(csi); 30 | double* ptrI = (double *)mxGetPi(csi); 31 | 32 | /* Check that length matches what it should */ 33 | if (len != calc_len) 34 | mexErrMsgIdAndTxt("MIMOToolbox:read_bfee_new:size","Wrong beamforming matrix size."); 35 | 36 | /* Compute CSI from all this crap :) */ 37 | for (i = 0; i < 30; ++i) 38 | { 39 | index += 3; 40 | remainder = index % 8; 41 | for (j = 0; j < Nrx * Ntx; ++j) 42 | { 43 | tmp = (payload[index / 8] >> remainder) | 44 | (payload[index/8+1] << (8-remainder)); 45 | //printf("%d\n", tmp); 46 | *ptrR = (double) tmp; 47 | ++ptrR; 48 | tmp = (payload[index / 8+1] >> remainder) | 49 | (payload[index/8+2] << (8-remainder)); 50 | *ptrI = (double) tmp; 51 | ++ptrI; 52 | index += 16; 53 | } 54 | } 55 | 56 | /* Compute the permutation array */ 57 | int perm_size[] = {1, 3}; 58 | mxArray *perm = mxCreateNumericArray(2, perm_size, mxDOUBLE_CLASS, mxREAL); 59 | ptrR = (double *)mxGetPr(perm); 60 | ptrR[0] = ((antenna_sel) & 0x3) + 1; 61 | ptrR[1] = ((antenna_sel >> 2) & 0x3) + 1; 62 | ptrR[2] = ((antenna_sel >> 4) & 0x3) + 1; 63 | 64 | mxDestroyArray(mxGetField(outCell, 0, "timestamp_low")); 65 | mxDestroyArray(mxGetField(outCell, 0, "bfee_count")); 66 | mxDestroyArray(mxGetField(outCell, 0, "Nrx")); 67 | mxDestroyArray(mxGetField(outCell, 0, "Ntx")); 68 | mxDestroyArray(mxGetField(outCell, 0, "rssi_a")); 69 | mxDestroyArray(mxGetField(outCell, 0, "rssi_b")); 70 | mxDestroyArray(mxGetField(outCell, 0, "rssi_c")); 71 | mxDestroyArray(mxGetField(outCell, 0, "noise")); 72 | mxDestroyArray(mxGetField(outCell, 0, "agc")); 73 | mxDestroyArray(mxGetField(outCell, 0, "perm")); 74 | mxDestroyArray(mxGetField(outCell, 0, "rate")); 75 | mxDestroyArray(mxGetField(outCell, 0, "csi")); 76 | mxSetField(outCell, 0, "timestamp_low", mxCreateDoubleScalar((double)timestamp_low)); 77 | mxSetField(outCell, 0, "bfee_count", mxCreateDoubleScalar((double)bfee_count)); 78 | mxSetField(outCell, 0, "Nrx", mxCreateDoubleScalar((double)Nrx)); 79 | mxSetField(outCell, 0, "Ntx", mxCreateDoubleScalar((double)Ntx)); 80 | mxSetField(outCell, 0, "rssi_a", mxCreateDoubleScalar((double)rssi_a)); 81 | mxSetField(outCell, 0, "rssi_b", mxCreateDoubleScalar((double)rssi_b)); 82 | mxSetField(outCell, 0, "rssi_c", mxCreateDoubleScalar((double)rssi_c)); 83 | mxSetField(outCell, 0, "noise", mxCreateDoubleScalar((double)noise)); 84 | mxSetField(outCell, 0, "agc", mxCreateDoubleScalar((double)agc)); 85 | mxSetField(outCell, 0, "perm", perm); 86 | mxSetField(outCell, 0, "rate", mxCreateDoubleScalar((double)fake_rate_n_flags)); 87 | mxSetField(outCell, 0, "csi", csi); 88 | 89 | //printf("Nrx: %u Ntx: %u len: %u calc_len: %u\n", Nrx, Ntx, len, calc_len); 90 | } 91 | 92 | /* The gateway function */ 93 | void mexFunction(int nlhs, mxArray *plhs[], 94 | int nrhs, const mxArray *prhs[]) 95 | { 96 | unsigned char *inBytes; /* A beamforming matrix */ 97 | mxArray *outCell; /* The cellular output */ 98 | 99 | /* check for proper number of arguments */ 100 | if(nrhs!=1) { 101 | mexErrMsgIdAndTxt("MIMOToolbox:read_bfee_new:nrhs","One input required."); 102 | } 103 | if(nlhs!=1) { 104 | mexErrMsgIdAndTxt("MIMOToolbox:read_bfee_new:nlhs","One output required."); 105 | } 106 | /* make sure the input argument is a char array */ 107 | if (!mxIsClass(prhs[0], "uint8")) { 108 | mexErrMsgIdAndTxt("MIMOToolbox:read_bfee_new:notBytes","Input must be a char array"); 109 | } 110 | 111 | /* create a pointer to the real data in the input matrix */ 112 | inBytes = mxGetData(prhs[0]); 113 | 114 | /* create the output matrix */ 115 | const char* fieldnames[] = {"timestamp_low", 116 | "bfee_count", 117 | "Nrx", "Ntx", 118 | "rssi_a", "rssi_b", "rssi_c", 119 | "noise", 120 | "agc", 121 | "perm", 122 | "rate", 123 | "csi"}; 124 | outCell = mxCreateStructMatrix(1, 1, 12, fieldnames); 125 | 126 | 127 | /* call the computational routine */ 128 | read_bfee(inBytes,outCell); 129 | 130 | /* */ 131 | plhs[0] = outCell; 132 | } 133 | -------------------------------------------------------------------------------- /Deep Belief Networks/read_bfee.mexa64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mars920314/DeepFi/9e7f99c181616d9aa4db18973c08675bdb714e8c/Deep Belief Networks/read_bfee.mexa64 -------------------------------------------------------------------------------- /Deep Belief Networks/read_bfee.mexmaci64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mars920314/DeepFi/9e7f99c181616d9aa4db18973c08675bdb714e8c/Deep Belief Networks/read_bfee.mexmaci64 -------------------------------------------------------------------------------- /Deep Belief Networks/read_bfee.mexw32: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mars920314/DeepFi/9e7f99c181616d9aa4db18973c08675bdb714e8c/Deep Belief Networks/read_bfee.mexw32 -------------------------------------------------------------------------------- /Deep Belief Networks/t10k-images-idx3-ubyte.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mars920314/DeepFi/9e7f99c181616d9aa4db18973c08675bdb714e8c/Deep Belief Networks/t10k-images-idx3-ubyte.gz -------------------------------------------------------------------------------- /Deep Belief Networks/t10k-labels-idx1-ubyte.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mars920314/DeepFi/9e7f99c181616d9aa4db18973c08675bdb714e8c/Deep Belief Networks/t10k-labels-idx1-ubyte.gz -------------------------------------------------------------------------------- /Deep Belief Networks/testerror.asv: -------------------------------------------------------------------------------- 1 | function testerror(filename) 2 | 3 | load(filename); 4 | load mnist_weights 5 | 6 | [testnumcases testnumdims testnumbatches]=size(testbatchdata); 7 | N=testnumcases; 8 | err=0; 9 | for batch = 1:testnumbatches 10 | data = [testbatchdata(:,:,batch)]; 11 | data = [data ones(N,1)]; 12 | w1probs = 1./(1 + exp(-data*w1)); w1probs = [w1probs ones(N,1)]; 13 | w2probs = 1./(1 + exp(-w1probs*w2)); w2probs = [w2probs ones(N,1)]; 14 | w3probs = 1./(1 + exp(-w2probs*w3)); w3probs = [w3probs ones(N,1)]; 15 | w4probs = w3probs*w4; w4probs = [w4probs ones(N,1)]; 16 | w5probs = 1./(1 + exp(-w4probs*w5)); w5probs = [w5probs ones(N,1)]; 17 | w6probs = 1./(1 + exp(-w5probs*w6)); w6probs = [w6probs ones(N,1)]; 18 | w7probs = 1./(1 + exp(-w6probs*w7)); w7probs = [w7probs ones(N,1)]; 19 | dataout = 1./(1 + exp(-w7probs*w8)); 20 | err = err + 1/N*sum(sum( (data(:,1:end-1)-dataout).^2 )); 21 | end 22 | test_err(epoch)=err/testnumbatches; 23 | fprintf(1,'Before epoch %d Train squared error: %6.3f Test squared error: %6.3f \t \t \n',epoch,train_err(epoch),test_err(epoch)); -------------------------------------------------------------------------------- /Deep Belief Networks/testerror.m: -------------------------------------------------------------------------------- 1 | function testerror(filename) 2 | 3 | load(filename); 4 | load mnist_weights; 5 | 6 | [testnumcases testnumdims testnumbatches]=size(testbatchdata); 7 | N=testnumcases; 8 | err=0; 9 | for batch = 1:testnumbatches 10 | data = [testbatchdata(:,:,batch)]; 11 | data = [data ones(N,1)]; 12 | w1probs = 1./(1 + exp(-data*w1)); w1probs = [w1probs ones(N,1)]; 13 | w2probs = 1./(1 + exp(-w1probs*w2)); w2probs = [w2probs ones(N,1)]; 14 | w3probs = 1./(1 + exp(-w2probs*w3)); w3probs = [w3probs ones(N,1)]; 15 | w4probs = w3probs*w4; w4probs = [w4probs ones(N,1)]; 16 | w5probs = 1./(1 + exp(-w4probs*w5)); w5probs = [w5probs ones(N,1)]; 17 | w6probs = 1./(1 + exp(-w5probs*w6)); w6probs = [w6probs ones(N,1)]; 18 | w7probs = 1./(1 + exp(-w6probs*w7)); w7probs = [w7probs ones(N,1)]; 19 | dataout = 1./(1 + exp(-w7probs*w8)); 20 | err = err + 1/N*sum(sum( (data(:,1:end-1)-dataout).^2 )); 21 | end 22 | test_err=err/testnumbatches; 23 | fprintf(1,'Test squared error: %6.3f \t \t \n',test_err); -------------------------------------------------------------------------------- /Deep Belief Networks/train-images-idx3-ubyte.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mars920314/DeepFi/9e7f99c181616d9aa4db18973c08675bdb714e8c/Deep Belief Networks/train-images-idx3-ubyte.gz -------------------------------------------------------------------------------- /Deep Belief Networks/train-labels-idx1-ubyte.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mars920314/DeepFi/9e7f99c181616d9aa4db18973c08675bdb714e8c/Deep Belief Networks/train-labels-idx1-ubyte.gz -------------------------------------------------------------------------------- /DeepFi/.PPP.m.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mars920314/DeepFi/9e7f99c181616d9aa4db18973c08675bdb714e8c/DeepFi/.PPP.m.swp -------------------------------------------------------------------------------- /DeepFi/CG_MNIST.m: -------------------------------------------------------------------------------- 1 | % Version 1.000 2 | % 3 | % Code provided by Ruslan Salakhutdinov and Geoff Hinton 4 | % 5 | % Permission is granted for anyone to copy, use, modify, or distribute this 6 | % program and accompanying programs and documents for any purpose, provided 7 | % this copyright notice is retained and prominently displayed, along with 8 | % a note saying that the original programs are available from our 9 | % web page. 10 | % The programs and documents are distributed without any warranty, express or 11 | % implied. As the programs were written for research purposes only, they have 12 | % not been tested to the degree that would be advisable in any important 13 | % application. All use of these programs is entirely at the user's own risk. 14 | 15 | function [f, df] = CG_MNIST(VV,Dim,XX); 16 | 17 | l1 = Dim(1); 18 | l2 = Dim(2); 19 | l3 = Dim(3); 20 | l4= Dim(4); 21 | l5= Dim(5); 22 | l6= Dim(6); 23 | l7= Dim(7); 24 | l8= Dim(8); 25 | l9= Dim(9); 26 | N = size(XX,1); 27 | 28 | % Do decomversion. 29 | w1 = reshape(VV(1:(l1+1)*l2),l1+1,l2); 30 | xxx = (l1+1)*l2; 31 | w2 = reshape(VV(xxx+1:xxx+(l2+1)*l3),l2+1,l3); 32 | xxx = xxx+(l2+1)*l3; 33 | w3 = reshape(VV(xxx+1:xxx+(l3+1)*l4),l3+1,l4); 34 | xxx = xxx+(l3+1)*l4; 35 | w4 = reshape(VV(xxx+1:xxx+(l4+1)*l5),l4+1,l5); 36 | xxx = xxx+(l4+1)*l5; 37 | w5 = reshape(VV(xxx+1:xxx+(l5+1)*l6),l5+1,l6); 38 | xxx = xxx+(l5+1)*l6; 39 | w6 = reshape(VV(xxx+1:xxx+(l6+1)*l7),l6+1,l7); 40 | xxx = xxx+(l6+1)*l7; 41 | w7 = reshape(VV(xxx+1:xxx+(l7+1)*l8),l7+1,l8); 42 | xxx = xxx+(l7+1)*l8; 43 | w8 = reshape(VV(xxx+1:xxx+(l8+1)*l9),l8+1,l9); 44 | 45 | 46 | XX = [XX ones(N,1)]; 47 | w1probs = 1./(1 + exp(-XX*w1)); w1probs = [w1probs ones(N,1)]; 48 | w2probs = 1./(1 + exp(-w1probs*w2)); w2probs = [w2probs ones(N,1)]; 49 | w3probs = 1./(1 + exp(-w2probs*w3)); w3probs = [w3probs ones(N,1)]; 50 | w4probs = w3probs*w4; w4probs = [w4probs ones(N,1)]; 51 | w5probs = 1./(1 + exp(-w4probs*w5)); w5probs = [w5probs ones(N,1)]; 52 | w6probs = 1./(1 + exp(-w5probs*w6)); w6probs = [w6probs ones(N,1)]; 53 | w7probs = 1./(1 + exp(-w6probs*w7)); w7probs = [w7probs ones(N,1)]; 54 | XXout = 1./(1 + exp(-w7probs*w8)); 55 | 56 | f = -1/N*sum(sum( XX(:,1:end-1).*log(XXout) + (1-XX(:,1:end-1)).*log(1-XXout))); 57 | IO = 1/N*(XXout-XX(:,1:end-1)); 58 | Ix8=IO; 59 | dw8 = w7probs'*Ix8; 60 | 61 | Ix7 = (Ix8*w8').*w7probs.*(1-w7probs); 62 | Ix7 = Ix7(:,1:end-1); 63 | dw7 = w6probs'*Ix7; 64 | 65 | Ix6 = (Ix7*w7').*w6probs.*(1-w6probs); 66 | Ix6 = Ix6(:,1:end-1); 67 | dw6 = w5probs'*Ix6; 68 | 69 | Ix5 = (Ix6*w6').*w5probs.*(1-w5probs); 70 | Ix5 = Ix5(:,1:end-1); 71 | dw5 = w4probs'*Ix5; 72 | 73 | Ix4 = (Ix5*w5'); 74 | Ix4 = Ix4(:,1:end-1); 75 | dw4 = w3probs'*Ix4; 76 | 77 | Ix3 = (Ix4*w4').*w3probs.*(1-w3probs); 78 | Ix3 = Ix3(:,1:end-1); 79 | dw3 = w2probs'*Ix3; 80 | 81 | Ix2 = (Ix3*w3').*w2probs.*(1-w2probs); 82 | Ix2 = Ix2(:,1:end-1); 83 | dw2 = w1probs'*Ix2; 84 | 85 | Ix1 = (Ix2*w2').*w1probs.*(1-w1probs); 86 | Ix1 = Ix1(:,1:end-1); 87 | dw1 = XX'*Ix1; 88 | 89 | df = [dw1(:)' dw2(:)' dw3(:)' dw4(:)' dw5(:)' dw6(:)' dw7(:)' dw8(:)' ]'; 90 | 91 | 92 | -------------------------------------------------------------------------------- /DeepFi/GaussianRBM.m: -------------------------------------------------------------------------------- 1 | %{ 2 | =========================================================================== 3 | Code provided by Yichuan (Charlie) Tang 4 | http://www.cs.toronto.edu/~tang 5 | 6 | Permission is granted for anyone to copy, use, modify, or distribute this 7 | program and accompanying programs and documents for any purpose, provided 8 | this copyright notice is retained and prominently displayed, along with 9 | a note saying that the original programs are available from our 10 | web page. 11 | The programs and documents are distributed without any warranty, express or 12 | implied. As the programs were written for research purposes only, they 13 | have not been tested to the degree that would be advisable in any important 14 | application. All use of these programs is entirely at the user's own risk. 15 | =========================================================================== 16 | 17 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 18 | GaussianRBM 19 | Based on code provided by Geoff Hinton and Ruslan Salakhutdinov. 20 | http://www.cs.toronto.edu/~hinton/MatlabForSciencePaper.html 21 | 22 | CT 3/2011 23 | PURPOSE: Training Gaussian RBM with CD, learning visible nodes' variances 24 | as well as sparsity penalty 25 | INPUT: 26 | OUTPUT: 27 | NOTES: 28 | TESTED: 29 | CHANGELOG: 30 | TODO: 31 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 32 | %} 33 | 34 | function [vhW, vb, hb, fvar, errs] = GaussianRBM(batchdata, params) 35 | 36 | [n d nBatches]=size(batchdata); 37 | 38 | assert(params.v_var > 0); 39 | fstd = ones(1,d)*sqrt(params.v_var); 40 | params.v_var=[]; 41 | 42 | r = params.epislonw_vng; 43 | 44 | std_rate = linspace(0, params.std_rate, params.maxepoch); 45 | std_rate(:) = params.std_rate; 46 | std_rate(1:min(30, params.maxepoch/2)) = 0; %learning schedule for variances 47 | 48 | 49 | assert( all(size(params.PreWts.vhW) == [d params.nHidNodes])); 50 | assert( all(size(params.PreWts.hb) == [1 params.nHidNodes])); 51 | assert( all(size(params.PreWts.vb) == [1 d])); 52 | 53 | vhW = params.PreWts.vhW; 54 | vb = params.PreWts.vb; 55 | hb = params.PreWts.hb; 56 | 57 | vhWInc = zeros( d, params.nHidNodes); 58 | hbInc = zeros( 1, params.nHidNodes); 59 | vbInc = zeros( 1, d); 60 | invfstdInc = zeros(1,d); 61 | 62 | Ones = ones(n,1); 63 | 64 | q=zeros(1, params.nHidNodes); %keep track of average activations 65 | errs = zeros(1, params.maxepoch); 66 | 67 | fprintf('\rTraining Learning v_var Gaussian-Binary RBM %d-%d epochs:%d r:%f',... 68 | d, params.nHidNodes, params.maxepoch, r); 69 | for epoch = 1:params.maxepoch 70 | 71 | if rem(epoch, int32(params.maxepoch/20)) == 0 || epoch < 30 72 | fprintf('\repoch %d',epoch); 73 | end 74 | 75 | errsum=0; 76 | ptot = 0; 77 | for batch = 1:nBatches 78 | 79 | Fstd = Ones*fstd; 80 | 81 | %%%%%%%%% START POSITIVE PHASE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 82 | data = batchdata(:,:,batch); %nxd 83 | pos_hidprobs = 1./(1 + exp(-(data./Fstd)*vhW - Ones*hb)); %p(h_j =1|data) 84 | pos_hidstates = pos_hidprobs > rand( size(pos_hidprobs) ); 85 | 86 | pos_prods = (data./Fstd)'* pos_hidprobs; 87 | pos_hid_act = sum(pos_hidprobs); 88 | pos_vis_act = sum(data)./(fstd.^2); %see notes on this derivation 89 | 90 | %%%%%%%%% END OF POSITIVE PHASE %%%%%%%%% 91 | for iterCD = 1:params.nCD 92 | 93 | %%%%%%%%% START NEGATIVE PHASE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 94 | negdataprobs = pos_hidstates*vhW'.*Fstd+Ones*vb; 95 | negdata = negdataprobs + randn(n, d).*Fstd; 96 | neg_hidprobs = 1./(1 + exp(-(negdata./Fstd)*vhW - Ones*hb )); %updating hidden nodes again 97 | pos_hidstates = neg_hidprobs > rand( size(neg_hidprobs) ); 98 | 99 | end %end CD iterations 100 | 101 | neg_prods = (negdata./Fstd)'*neg_hidprobs; 102 | neg_hid_act = sum(neg_hidprobs); 103 | neg_vis_act = sum(negdata)./(fstd.^2); %see notes for details 104 | 105 | %%%%%%%%% END OF NEGATIVE PHASE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 106 | 107 | errsum = errsum + sum(sum( (data-negdata).^2 )); 108 | 109 | if epoch > params.init_final_momen_iter, 110 | momentum=params.final_momen; 111 | else 112 | momentum=params.init_momen; 113 | end 114 | 115 | %%%%%%%%% UPDATE WEIGHTS AND BIASES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 116 | 117 | vhWInc = momentum*vhWInc + r/n*(pos_prods-neg_prods) - r*params.wtcost*vhW; 118 | vbInc = momentum*vbInc + (r/n)*(pos_vis_act-neg_vis_act); 119 | hbInc = momentum*hbInc + (r/n)*(pos_hid_act-neg_hid_act); 120 | 121 | invfstd_grad = sum(2*data.*(Ones*vb-data/2)./Fstd,1) + sum(data' .* (vhW*pos_hidprobs') ,2)'; 122 | invfstd_grad = invfstd_grad - ( sum(2*negdata.*(Ones*vb-negdata/2)./Fstd,1) + ... 123 | sum( negdata'.*(vhW*neg_hidprobs') ,2 )' ); 124 | 125 | invfstdInc = momentum*invfstdInc + std_rate(epoch)/n*invfstd_grad; 126 | 127 | if params.SPARSE == 1 %nair's paper on 3D object recognition 128 | %update q 129 | if batch==1 && epoch == 1 130 | q = mean(pos_hidprobs); 131 | else 132 | q_prev = q; 133 | q = 0.9*q_prev+0.1*mean(pos_hidprobs); 134 | end 135 | 136 | p = params.sparse_p; 137 | grad = 0.1*params.sparse_lambda/n*sum(pos_hidprobs.*(1-pos_hidprobs)).*(p-q)./(q.*(1-q)); 138 | gradW =0.1*params.sparse_lambda/n*(data'./Fstd'*(pos_hidprobs.*(1-pos_hidprobs))).*repmat((p-q)./(q.*(1-q)), d,1); 139 | 140 | hbInc = hbInc + r*grad; 141 | vhWInc = vhWInc + r*gradW; 142 | end 143 | 144 | ptot = ptot+mean(pos_hidprobs(:)); 145 | 146 | vhW = vhW + vhWInc; 147 | vb = vb + vbInc; 148 | hb = hb + hbInc; 149 | 150 | invfstd = 1./fstd; 151 | invfstd = invfstd + invfstdInc; 152 | fstd = 1./invfstd; 153 | 154 | fstd = max(fstd, 0.005); %have a lower bound! 155 | %%%%%%%%%%%%%%%% END OF UPDATES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 156 | end 157 | 158 | if rem(epoch, int32(params.maxepoch/20)) == 0 || epoch < 30 159 | fprintf(1, ' p%1.2f ', ptot/nBatches ); 160 | fprintf(1, ' error %6.2f stdr:%.5f fstd(x,y): [%2.3f %2.3f] mm:%.2f ', errsum, std_rate(epoch), fstd(1), fstd(2), momentum); 161 | fprintf(1, 'vh_W min %2.4f max %2.4f ', min(min(vhW)), max(max(vhW))); 162 | end 163 | errs(epoch) = errsum; 164 | end 165 | fvar = fstd.^2; 166 | 167 | -------------------------------------------------------------------------------- /DeepFi/backpropagation.m: -------------------------------------------------------------------------------- 1 | maxepoch=5; 2 | fprintf(1,'\nFine-tuning deep autoencoder by minimizing cross entropy error. \n'); 3 | 4 | load batchdata 5 | load mnistvh 6 | load mnisthp 7 | load mnisthp2 8 | load mnistpo 9 | 10 | [numcases numdims numbatches]=size(batchdata); 11 | w1=[vishid; hidrecbiases]; 12 | w2=[hidpen; penrecbiases]; 13 | w3=[hidpen2; penrecbiases2]; 14 | w4=[hidtop; toprecbiases]; 15 | w5=[hidtop'; topgenbiases]; 16 | w6=[hidpen2'; hidgenbiases2]; 17 | w7=[hidpen'; hidgenbiases]; 18 | w8=[vishid'; visbiases]; 19 | l1=size(w1,1)-1; 20 | l2=size(w2,1)-1; 21 | l3=size(w3,1)-1; 22 | l4=size(w4,1)-1; 23 | l5=size(w5,1)-1; 24 | l6=size(w6,1)-1; 25 | l7=size(w7,1)-1; 26 | l8=size(w8,1)-1; 27 | l9=l1; 28 | 29 | for epoch = 1:maxepoch 30 | tt=0; 31 | for batch = 1:numbatches/10 32 | fprintf(1,'epoch %d batch %d\r',epoch,batch); 33 | tt=tt+1; 34 | data=[]; 35 | for kk=1:10 36 | data=[data 37 | batchdata(:,:,(tt-1)*10+kk)]; 38 | end 39 | max_iter=3; 40 | VV = [w1(:)' w2(:)' w3(:)' w4(:)' w5(:)' w6(:)' w7(:)' w8(:)']'; 41 | Dim = [l1; l2; l3; l4; l5; l6; l7; l8; l9]; 42 | 43 | [X, fX] = minimize(VV,'CG_MNIST',max_iter,Dim,data); 44 | 45 | w1 = reshape(X(1:(l1+1)*l2),l1+1,l2); 46 | xxx = (l1+1)*l2; 47 | w2 = reshape(X(xxx+1:xxx+(l2+1)*l3),l2+1,l3); 48 | xxx = xxx+(l2+1)*l3; 49 | w3 = reshape(X(xxx+1:xxx+(l3+1)*l4),l3+1,l4); 50 | xxx = xxx+(l3+1)*l4; 51 | w4 = reshape(X(xxx+1:xxx+(l4+1)*l5),l4+1,l5); 52 | xxx = xxx+(l4+1)*l5; 53 | w5 = reshape(X(xxx+1:xxx+(l5+1)*l6),l5+1,l6); 54 | xxx = xxx+(l5+1)*l6; 55 | w6 = reshape(X(xxx+1:xxx+(l6+1)*l7),l6+1,l7); 56 | xxx = xxx+(l6+1)*l7; 57 | w7 = reshape(X(xxx+1:xxx+(l7+1)*l8),l7+1,l8); 58 | xxx = xxx+(l7+1)*l8; 59 | w8 = reshape(X(xxx+1:xxx+(l8+1)*l9),l8+1,l9); 60 | end 61 | savepath=['.\data\mnist_weights' num2str(j) num2str(i)]; 62 | save(savepath, 'w1', 'w2', 'w3', 'w4', 'w5', 'w6', 'w7', 'w8', 'vishid', 'hidrecbiases', 'visbiases', 'hidpen', 'penrecbiases', 'hidgenbiases', 'hidpen2', 'penrecbiases2', 'hidgenbiases2', 'hidtop', 'toprecbiases', 'topgenbiases'); 63 | end 64 | % -------------------------------------------------------------------------------- /DeepFi/backpropagationtest.m: -------------------------------------------------------------------------------- 1 | % Version 1.000 2 | % 3 | % Code provided by Ruslan Salakhutdinov and Geoff Hinton 4 | % 5 | % Permission is granted for anyone to copy, use, modify, or distribute this 6 | % program and accompanying programs and documents for any purpose, provided 7 | % this copyright notice is retained and prominently displayed, along with 8 | % a note saying that the original programs are available from our 9 | % web page. 10 | % The programs and documents are distributed without any warranty, express or 11 | % implied. As the programs were written for research purposes only, they have 12 | % not been tested to the degree that would be advisable in any important 13 | % application. All use of these programs is entirely at the user's own risk. 14 | 15 | % This program fine-tunes an autoencoder with backpropagation. 16 | % Weights of the autoencoder are going to be saved in mnist_weights.mat 17 | % and trainig and test reconstruction errors in mnist_error.mat 18 | % You can also set maxepoch, default value is 200 as in our paper. 19 | 20 | maxepoch=10; 21 | fprintf(1,'\nFine-tuning deep autoencoder by minimizing cross entropy error. \n'); 22 | % fprintf(1,'60 batches of 1000 cases each. \n'); 23 | load batchdata 24 | load testbatchdatanew 25 | load mnistvh 26 | load mnisthp 27 | load mnisthp2 28 | load mnistpo 29 | 30 | % makebatches; 31 | [numcases numdims numbatches]=size(batchdata); 32 | N=numcases; 33 | 34 | %%%% PREINITIALIZE WEIGHTS OF THE AUTOENCODER %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 35 | w1=[vishid; hidrecbiases]; 36 | w2=[hidpen; penrecbiases]; 37 | w3=[hidpen2; penrecbiases2]; 38 | w4=[hidtop; toprecbiases]; 39 | w5=[hidtop'; topgenbiases]; 40 | w6=[hidpen2'; hidgenbiases2]; 41 | w7=[hidpen'; hidgenbiases]; 42 | w8=[vishid'; visbiases]; 43 | 44 | %%%%%%%%%% END OF PREINITIALIZATIO OF WEIGHTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 45 | 46 | l1=size(w1,1)-1; 47 | l2=size(w2,1)-1; 48 | l3=size(w3,1)-1; 49 | l4=size(w4,1)-1; 50 | l5=size(w5,1)-1; 51 | l6=size(w6,1)-1; 52 | l7=size(w7,1)-1; 53 | l8=size(w8,1)-1; 54 | l9=l1; 55 | test_err=[]; 56 | train_err=[]; 57 | 58 | 59 | for epoch = 1:maxepoch 60 | 61 | %%%%%%%%%%%%%%%%%%%% COMPUTE TRAINING RECONSTRUCTION ERROR %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 62 | err=0; 63 | [numcases numdims numbatches]=size(batchdata); 64 | N=numcases; 65 | for batch = 1:numbatches 66 | data = [batchdata(:,:,batch)]; 67 | data = [data ones(N,1)]; 68 | w1probs = 1./(1 + exp(-data*w1)); w1probs = [w1probs ones(N,1)]; 69 | w2probs = 1./(1 + exp(-w1probs*w2)); w2probs = [w2probs ones(N,1)]; 70 | w3probs = 1./(1 + exp(-w2probs*w3)); w3probs = [w3probs ones(N,1)]; 71 | w4probs = w3probs*w4; w4probs = [w4probs ones(N,1)]; 72 | w5probs = 1./(1 + exp(-w4probs*w5)); w5probs = [w5probs ones(N,1)]; 73 | w6probs = 1./(1 + exp(-w5probs*w6)); w6probs = [w6probs ones(N,1)]; 74 | w7probs = 1./(1 + exp(-w6probs*w7)); w7probs = [w7probs ones(N,1)]; 75 | dataout = 1./(1 + exp(-w7probs*w8)); 76 | err= err + 1/N*sum(sum( (data(:,1:end-1)-dataout).^2 )); 77 | end 78 | train_err(epoch)=err/numbatches; 79 | 80 | %%%%%%%%%%%%%% END OF COMPUTING TRAINING RECONSTRUCTION ERROR %%%%%%%%%%%%%%%%%%%%%%%%%%%%% 81 | 82 | %%%% DISPLAY FIGURE TOP ROW REAL DATA BOTTOM ROW RECONSTRUCTIONS %%%%%%%%%%%%%%%%%%%%%%%%% 83 | % fprintf(1,'Displaying in figure 1: Top row - real data, Bottom row -- reconstructions \n'); 84 | % output=[]; 85 | % for ii=1:15 86 | % output = [output data(ii,1:end-1)' dataout(ii,:)']; 87 | % end 88 | % if epoch==1 89 | % close all 90 | % figure('Position',[100,600,1000,200]); 91 | % else 92 | % figure(1) 93 | % end 94 | % mnistdisp(output); 95 | % drawnow; 96 | 97 | %%%%%%%%%%%%%%%%%%%% COMPUTE TEST RECONSTRUCTION ERROR %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 98 | [testnumcases testnumdims testnumbatches]=size(testbatchdata); 99 | N=testnumcases; 100 | err=0; 101 | for batch = 1:testnumbatches 102 | data = [testbatchdata(:,:,batch)]; 103 | data = [data ones(N,1)]; 104 | w1probs = 1./(1 + exp(-data*w1)); w1probs = [w1probs ones(N,1)]; 105 | w2probs = 1./(1 + exp(-w1probs*w2)); w2probs = [w2probs ones(N,1)]; 106 | w3probs = 1./(1 + exp(-w2probs*w3)); w3probs = [w3probs ones(N,1)]; 107 | w4probs = w3probs*w4; w4probs = [w4probs ones(N,1)]; 108 | w5probs = 1./(1 + exp(-w4probs*w5)); w5probs = [w5probs ones(N,1)]; 109 | w6probs = 1./(1 + exp(-w5probs*w6)); w6probs = [w6probs ones(N,1)]; 110 | w7probs = 1./(1 + exp(-w6probs*w7)); w7probs = [w7probs ones(N,1)]; 111 | dataout = 1./(1 + exp(-w7probs*w8)); 112 | err = err + 1/N*sum(sum( (data(:,1:end-1)-dataout).^2 )); 113 | end 114 | test_err(epoch)=err/testnumbatches; 115 | fprintf(1,'Before epoch %d Train squared error: %6.3f Test squared error: %6.3f \t \t \n',epoch,train_err(epoch),test_err(epoch)); 116 | 117 | %%%%%%%%%%%%%% END OF COMPUTING TEST RECONSTRUCTION ERROR %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 118 | 119 | tt=0; 120 | for batch = 1:numbatches/10 121 | fprintf(1,'epoch %d batch %d\r',epoch,batch); 122 | 123 | %%%%%%%%%%% COMBINE 10 MINIBATCHES INTO 1 LARGER MINIBATCH %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 124 | tt=tt+1; 125 | data=[]; 126 | for kk=1:10 127 | data=[data 128 | batchdata(:,:,(tt-1)*10+kk)]; 129 | end 130 | 131 | %%%%%%%%%%%%%%% PERFORM CONJUGATE GRADIENT WITH 3 LINESEARCHES %%%%%%%%%%%%%%%%%%%%%%%%%%%%% 132 | max_iter=3; 133 | VV = [w1(:)' w2(:)' w3(:)' w4(:)' w5(:)' w6(:)' w7(:)' w8(:)']'; 134 | Dim = [l1; l2; l3; l4; l5; l6; l7; l8; l9]; 135 | 136 | [X, fX] = minimize(VV,'CG_MNIST',max_iter,Dim,data); 137 | 138 | w1 = reshape(X(1:(l1+1)*l2),l1+1,l2); 139 | xxx = (l1+1)*l2; 140 | w2 = reshape(X(xxx+1:xxx+(l2+1)*l3),l2+1,l3); 141 | xxx = xxx+(l2+1)*l3; 142 | w3 = reshape(X(xxx+1:xxx+(l3+1)*l4),l3+1,l4); 143 | xxx = xxx+(l3+1)*l4; 144 | w4 = reshape(X(xxx+1:xxx+(l4+1)*l5),l4+1,l5); 145 | xxx = xxx+(l4+1)*l5; 146 | w5 = reshape(X(xxx+1:xxx+(l5+1)*l6),l5+1,l6); 147 | xxx = xxx+(l5+1)*l6; 148 | w6 = reshape(X(xxx+1:xxx+(l6+1)*l7),l6+1,l7); 149 | xxx = xxx+(l6+1)*l7; 150 | w7 = reshape(X(xxx+1:xxx+(l7+1)*l8),l7+1,l8); 151 | xxx = xxx+(l7+1)*l8; 152 | w8 = reshape(X(xxx+1:xxx+(l8+1)*l9),l8+1,l9); 153 | 154 | %%%%%%%%%%%%%%% END OF CONJUGATE GRADIENT WITH 3 LINESEARCHES %%%%%%%%%%%%%%%%%%%%%%%%%%%%% 155 | 156 | end 157 | 158 | savepath=['.\data\mnist_weights' num2str(i) num2str(j)]; 159 | save(savepath, 'w1', 'w2', 'w3', 'w4', 'w5', 'w6', 'w7', 'w8', 'vishid', 'hidrecbiases', 'visbiases', 'hidpen', 'penrecbiases', 'hidgenbiases', 'hidpen2', 'penrecbiases2', 'hidgenbiases2', 'hidtop', 'toprecbiases', 'topgenbiases'); 160 | 161 | end 162 | 163 | 164 | 165 | -------------------------------------------------------------------------------- /DeepFi/dbinv.m: -------------------------------------------------------------------------------- 1 | %DBINV Convert from decibels. 2 | % 3 | % (c) 2008-2011 Daniel Halperin 4 | % 5 | function ret = dbinv(x) 6 | ret = 10.^(x/10); 7 | end 8 | -------------------------------------------------------------------------------- /DeepFi/dis.m: -------------------------------------------------------------------------------- 1 | dis4=[0.8891 0.9118 0.9554 0.5253 0.8994 0.8097 -0.4427 0.8093 -0.5591 0.9187 0.9606 0.9731 0.7493 0.8895 0.8896 0.8941 0.8944]; 2 | dis3=[0.6918 0.5588 0.9352 0.8424 0.928 0.9528 -0.782 0.9019 -0.7341 0.8707 0.7107 0.9766 0.9879 0.3031 -0.401 0.822 0.9874 0.3375]; 3 | dis2=[0.389 0.9321 0.8665 0.5822 0.508 0.7786 -0.6853 -0.2439 -0.7245 0.9909 -0.6746 0.3935 0.9173 0.2485 0.7708 0.9198 0.9783 0.8963]; 4 | ecdf(dis4) 5 | hold on 6 | ecdf(dis3) 7 | hold on 8 | ecdf(dis2) 9 | hold on 10 | %% 11 | % dis1=[0.4476 0.8551 0.9103 0.7671 0.4249 -0.3435 0.7246 -0.5476 0.8272 0.4559 -0.1957 -0.2742 0.5807 -0.448 0.6945 0.1691 0.9568 -0.3598 0.4072 0.4901 0.7227 0.6713 0.061 -0.5377]; 12 | % dis2=[0.7381 0.8894 0.2339 0.1085 0.3151 0.0674 0.8126 -0.6498 0.5789 0.2754 -0.0868 0.4306 0.92 -0.7425 0.0071 -0.4421 0.7818 -0.3019 0.7129 -0.8015 0.4108 0.2067 0.8165 -0.1825]; 13 | % dis3=[0.3572 -0.1555 0.2298 -0.4467 0.1821 -0.1577 0.8691 -0.1586 0.5186 0.5137 0.9798 0.5354 0.7659 -0.7246 0.8572 0.6862 0.549 -0.3476 -0.0439 -0.2004 0.5011 0.5864 0.9726 -0.4086]; 14 | % dis4=[0.9598 -0.8593 0.5836 0.3978 -0.0263 -0.4364 0.2737 0.7362 0.6938 0.3933 0.8525 0.1858 0.4297 0.1514 0.3344 0.8378 0.5197 -0.3527 0.3036 0.1797 0.1398 0.7307 0.7609 0.9306]; 15 | 16 | % dis1=[0.4476 0.8551 0.9103 0.7671 0.4249 0.7246 0.8272 0.4559 -0.1957 -0.2742 0.5807 0.6945 0.1691 0.9568 0.4072 0.4901 0.7227 0.6713 0.061]; 17 | % dis2=[0.7381 0.8894 0.2339 0.1085 0.3151 0.0674 0.8126 0.5789 0.2754 -0.0868 0.4306 0.92 0.0071 0.7818 0.7129 0.4108 0.2067 0.8165 -0.1825]; 18 | % dis3=[0.3572 -0.1555 0.2298 0.1821 -0.1577 0.8691 -0.1586 0.5186 0.5137 0.9798 0.5354 0.7659 0.8572 0.6862 0.549 -0.0439 -0.2004 0.5011 0.5864 0.9726]; 19 | % dis4=[0.9598 0.5836 0.3978 -0.0263 0.2737 0.7362 0.6938 0.3933 0.8525 0.1858 0.4297 0.1514 0.3344 0.8378 0.5197 0.3036 0.1797 0.1398 0.7307 0.7609 0.9306]; 20 | 21 | dis1=[0.4476 0.8551 0.9103 0.7671 0.4249 0.4249 0.7246 0.7246 0.8272 0.4559 -0.1957 -0.2742 0.5807 0.5807 0.6945 0.1691 0.9568 0.9568 0.4072 0.4901 0.7227 0.6713 0.061 0.061]; 22 | dis2=[0.7381 0.8894 0.2339 0.1085 0.3151 0.0674 0.8126 0.8126 0.5789 0.5789 -0.0868 0.4306 0.92 0.92 0.0071 0.0071 0.7818 0.7818 0.7129 0.7129 0.4108 0.2067 0.8165 -0.1825]; 23 | dis3=[0.3572 -0.1555 0.2298 0.2298 0.1821 -0.1577 0.8691 -0.1586 0.5186 0.5137 0.9798 0.5354 0.7659 0.7659 0.8572 0.6862 0.549 -0.3476 -0.0439 -0.2004 0.5011 0.5864 0.9726 0.9726]; 24 | dis4=[0.9598 0.9598 0.5836 0.3978 -0.0263 -0.4364 0.2737 0.7362 0.6938 0.3933 0.8525 0.1858 0.4297 0.1514 0.3344 0.8378 0.5197 -0.3527 0.3036 0.1797 0.1398 0.7307 0.7609 0.9306]; 25 | 26 | -------------------------------------------------------------------------------- /DeepFi/errfunc1.m: -------------------------------------------------------------------------------- 1 | function [ err ] = errfunc1( dataname,traindataname ) 2 | load(dataname); 3 | load (traindataname); 4 | 5 | [numcases numdims numbatches]=size(batchdata); 6 | N=numcases; 7 | err=0; 8 | for batch = 1:numbatches 9 | data = [batchdata(:,:,batch)]; 10 | data = [data ones(N,1)]; 11 | w1probs = 1./(1 + exp(-data*w1)); w1probs = [w1probs ones(N,1)]; 12 | w2probs = 1./(1 + exp(-w1probs*w2)); w2probs = [w2probs ones(N,1)]; 13 | w3probs = 1./(1 + exp(-w2probs*w3)); w3probs = [w3probs ones(N,1)]; 14 | w4probs = w3probs*w4; w4probs = [w4probs ones(N,1)]; 15 | w5probs = 1./(1 + exp(-w4probs*w5)); w5probs = [w5probs ones(N,1)]; 16 | w6probs = 1./(1 + exp(-w5probs*w6)); w6probs = [w6probs ones(N,1)]; 17 | w7probs = 1./(1 + exp(-w6probs*w7)); w7probs = [w7probs ones(N,1)]; 18 | dataout = 1./(1 + exp(-w7probs*w8)); 19 | % err = err + 1/N*sum(sum( (data(:,1:end-1)-dataout).^2 )); 20 | 21 | % err=err+sum(exp(-sum(((data(:,1:end-1)-dataout).^2)')./(2*10*var(data(:,1:end-1)')))); 22 | Bhattacharyya=data(:,1:end-1)-dataout 23 | % for n=1:N 24 | % err=err+corr(data(n,1:end-1)',dataout(n,:)'); 25 | % err=err+xxxc(data(n,1:end-1)',dataout(n,:)'); 26 | % end 27 | end 28 | err=err/numbatches/numcases; 29 | % fprintf(1,'squared error: %6.3f ,%s of %s \t \t \n',err, dataname, traindataname); 30 | -------------------------------------------------------------------------------- /DeepFi/getE.asv: -------------------------------------------------------------------------------- 1 | function E=getE(dataname,traindataname) 2 | 3 | load(dataname); 4 | load (traindataname); 5 | % load mnistvh; 6 | % load mnisthp; 7 | % load mnisthp2; 8 | % load mnistpo; 9 | 10 | [numcases numdims numbatches]=size(batchdata); 11 | N=numcases; 12 | err=0; 13 | E=0; 14 | for batch = 1:numbatches 15 | dataori = [batchdata(:,:,batch)]; 16 | data = [dataori ones(N,1)]; 17 | w1probsori = 1./(1 + exp(-data*w1)); w1probs = [w1probsori ones(N,1)]; 18 | w2probsori = 1./(1 + exp(-w1probs*w2)); w2probs = [w2probsori ones(N,1)]; 19 | w3probsori = 1./(1 + exp(-w2probs*w3)); w3probs = [w3probsori ones(N,1)]; 20 | w4probsori = w3probs*w4; w4probs = [w4probsori ones(N,1)]; 21 | w5probsori = 1./(1 + exp(-w4probs*w5)); w5probs = [w5probsori ones(N,1)]; 22 | w6probsori = 1./(1 + exp(-w5probs*w6)); w6probs = [w6probsori ones(N,1)]; 23 | w7probsori = 1./(1 + exp(-w6probs*w7)); w7probs = [w7probsori ones(N,1)]; 24 | dataout = 1./(1 + exp(-w7probs*w8)); 25 | % err = err + 1/N*sum(sum( (data(:,1:end-1)-dataout).^2 )); 26 | E=E-sum(sum(data*w1*w1probsori'))-sum(sum(w1probsori*hidrecbiases'))-sum(sum(dataori*visbiases')); 27 | % E=E-sum(sum(-w1probs*w2*w3probsori'))-sum(sum(w2probsori*penrecbiases'))-sum(sum(w1probsori*hidgenbiases')); 28 | end 29 | err=err/numbatches/numcases; 30 | E=E/numbatches; 31 | fprintf(1,'squared error: %6.3f ,%s of %s \t \t \n',E, dataname, traindataname); 32 | -------------------------------------------------------------------------------- /DeepFi/getE.m: -------------------------------------------------------------------------------- 1 | function E=getE(dataname,traindataname) 2 | 3 | load(dataname); 4 | load (traindataname); 5 | % load mnistvh; 6 | % load mnisthp; 7 | % load mnisthp2; 8 | % load mnistpo; 9 | 10 | [numcases numdims numbatches]=size(batchdata); 11 | N=numcases; 12 | err=0; 13 | E=0; 14 | for batch = 1:numbatches 15 | dataori = [batchdata(:,:,batch)]; 16 | data = [dataori ones(N,1)]; 17 | w1probsori = 1./(1 + exp(-data*w1)); w1probs = [w1probsori ones(N,1)]; 18 | w2probsori = 1./(1 + exp(-w1probs*w2)); w2probs = [w2probsori ones(N,1)]; 19 | w3probsori = 1./(1 + exp(-w2probs*w3)); w3probs = [w3probsori ones(N,1)]; 20 | w4probsori = w3probs*w4; w4probs = [w4probsori ones(N,1)]; 21 | w5probsori = 1./(1 + exp(-w4probs*w5)); w5probs = [w5probsori ones(N,1)]; 22 | w6probsori = 1./(1 + exp(-w5probs*w6)); w6probs = [w6probsori ones(N,1)]; 23 | w7probsori = 1./(1 + exp(-w6probs*w7)); w7probs = [w7probsori ones(N,1)]; 24 | dataout = 1./(1 + exp(-w7probs*w8)); 25 | save all; 26 | % err = err + 1/N*sum(sum( (data(:,1:end-1)-dataout).^2 )); 27 | DE=sum(sum(data*w1*w1probsori'))-sum(sum(w1probsori*hidrecbiases'))-sum(sum(dataori*visbiases')); 28 | % if DE<0 29 | % fprintf(1,'DE<0'); 30 | % end 31 | E=E-DE; 32 | E=E-sum(sum(-w1probs*w2*w2probsori'))-sum(sum(w2probsori*penrecbiases'))-sum(sum(w1probsori*hidgenbiases')); 33 | end 34 | err=err/numbatches/numcases; 35 | E=E/numbatches; 36 | fprintf(1,'squared error: %6.3f ,%s of %s \t \t \n',E, dataname, traindataname); 37 | -------------------------------------------------------------------------------- /DeepFi/get_eff_SNRs.m: -------------------------------------------------------------------------------- 1 | %GET_EFF_SNRS Compute the effective SNR values from a CSI matrix 2 | % Note that the matrix is expected to have dimensions M x N x S, where 3 | % M = # TX antennas 4 | % N = # RX antennas 5 | % S = # subcarriers 6 | % 7 | % (c) 2008-2011 Daniel Halperin , 8 | % Wenjun Hu 9 | % 10 | function ret=get_eff_SNRs(csi) 11 | ret = zeros(7,4) + eps; % machine epsilon is smallest possible SNR 12 | 13 | % [M N S] = size(csi); % If next line doesn't compile 14 | [M N ~] = size(csi); 15 | k = min(M,N); 16 | 17 | % Do the various SIMO configurations (i.e., TX antenna selection) 18 | if k >= 1 19 | snrs = get_simo_SNRs(csi); 20 | 21 | bers = bpsk_ber(snrs); 22 | mean_ber = mean(mean(bers, 3), 2); 23 | ret((1:length(mean_ber)),1) = bpsk_berinv(mean_ber); 24 | 25 | bers = qpsk_ber(snrs); 26 | mean_ber = mean(mean(bers, 3), 2); 27 | ret((1:length(mean_ber)),2) = qpsk_berinv(mean_ber); 28 | 29 | bers = qam16_ber(snrs); 30 | mean_ber = mean(mean(bers, 3), 2); 31 | ret((1:length(mean_ber)),3) = qam16_berinv(mean_ber); 32 | 33 | bers = qam64_ber(snrs); 34 | mean_ber = mean(mean(bers, 3), 2); 35 | ret((1:length(mean_ber)),4) = qam64_berinv(mean_ber); 36 | end 37 | 38 | % Do the various MIMO2 configurations (i.e., TX antenna selection) 39 | if k >= 2 40 | snrs = get_mimo2_SNRs(csi); 41 | 42 | bers = bpsk_ber(snrs); 43 | mean_ber = mean(mean(bers, 3), 2); 44 | ret(3+(1:length(mean_ber)),1) = bpsk_berinv(mean_ber); 45 | 46 | bers = qpsk_ber(snrs); 47 | mean_ber = mean(mean(bers, 3), 2); 48 | ret(3+(1:length(mean_ber)),2) = qpsk_berinv(mean_ber); 49 | 50 | bers = qam16_ber(snrs); 51 | mean_ber = mean(mean(bers, 3), 2); 52 | ret(3+(1:length(mean_ber)),3) = qam16_berinv(mean_ber); 53 | 54 | bers = qam64_ber(snrs); 55 | mean_ber = mean(mean(bers, 3), 2); 56 | ret(3+(1:length(mean_ber)),4) = qam64_berinv(mean_ber); 57 | end 58 | 59 | % Do the MIMO3 configuration 60 | if k >= 3 61 | snrs = get_mimo3_SNRs(csi); 62 | 63 | bers = bpsk_ber(snrs); 64 | mean_ber = mean(mean(bers, 3), 2); 65 | ret(6+(1:length(mean_ber)),1) = bpsk_berinv(mean_ber); 66 | 67 | bers = qpsk_ber(snrs); 68 | mean_ber = mean(mean(bers, 3), 2); 69 | ret(6+(1:length(mean_ber)),2) = qpsk_berinv(mean_ber); 70 | 71 | bers = qam16_ber(snrs); 72 | mean_ber = mean(mean(bers, 3), 2); 73 | ret(6+(1:length(mean_ber)),3) = qam16_berinv(mean_ber); 74 | 75 | bers = qam64_ber(snrs); 76 | mean_ber = mean(mean(bers, 3), 2); 77 | ret(6+(1:length(mean_ber)),4) = qam64_berinv(mean_ber); 78 | end 79 | 80 | % Apparently, sometimes it can be infinite so cap it at 40 dB 81 | %ret(ret==Inf) = dbinv(40); 82 | end 83 | -------------------------------------------------------------------------------- /DeepFi/get_scaled_csi.m: -------------------------------------------------------------------------------- 1 | %GET_SCALED_CSI Converts a CSI struct to a channel matrix H. 2 | % 3 | % (c) 2008-2011 Daniel Halperin 4 | % 5 | function ret = get_scaled_csi(csi_st) 6 | % Pull out CSI 7 | csi = csi_st.csi; 8 | 9 | % Calculate the scale factor between normalized CSI and RSSI (mW) 10 | csi_sq = csi .* conj(csi); 11 | csi_pwr = sum(csi_sq(:)); 12 | rssi_pwr = dbinv(get_total_rss(csi_st)); 13 | % Scale CSI -> Signal power : rssi_pwr / (mean of csi_pwr) 14 | scale = rssi_pwr / (csi_pwr / 30); 15 | 16 | % Thermal noise might be undefined if the trace was 17 | % captured in monitor mode. 18 | % ... If so, set it to -92 19 | if (csi_st.noise == -127) 20 | noise_db = -92; 21 | else 22 | noise_db = csi_st.noise; 23 | end 24 | thermal_noise_pwr = dbinv(noise_db); 25 | 26 | % Quantization error: the coefficients in the matrices are 27 | % 8-bit signed numbers, max 127/-128 to min 0/1. Given that Intel 28 | % only uses a 6-bit ADC, I expect every entry to be off by about 29 | % +/- 1 (total across real & complex parts) per entry. 30 | % 31 | % The total power is then 1^2 = 1 per entry, and there are 32 | % Nrx*Ntx entries per carrier. We only want one carrier's worth of 33 | % error, since we only computed one carrier's worth of signal above. 34 | quant_error_pwr = scale * (csi_st.Nrx * csi_st.Ntx); 35 | 36 | % Total noise and error power 37 | total_noise_pwr = thermal_noise_pwr + quant_error_pwr; 38 | 39 | % Ret now has units of sqrt(SNR) just like H in textbooks 40 | ret = csi * sqrt(scale / total_noise_pwr); 41 | if csi_st.Ntx == 2 42 | ret = ret * sqrt(2); 43 | elseif csi_st.Ntx == 3 44 | % Note: this should be sqrt(3)~ 4.77 dB. But, 4.5 dB is how 45 | % Intel (and some other chip makers) approximate a factor of 3 46 | % 47 | % You may need to change this if your card does the right thing. 48 | ret = ret * sqrt(dbinv(4.5)); 49 | end 50 | end 51 | -------------------------------------------------------------------------------- /DeepFi/get_total_rss.m: -------------------------------------------------------------------------------- 1 | %GET_TOTAL_RSS Calculates the Received Signal Strength (RSS) in dBm from 2 | % a CSI struct. 3 | % 4 | % (c) 2011 Daniel Halperin 5 | % 6 | function ret = get_total_rss(csi_st) 7 | error(nargchk(1,1,nargin)); 8 | 9 | % Careful here: rssis could be zero 10 | rssi_mag = 0; 11 | if csi_st.rssi_a ~= 0 12 | rssi_mag = rssi_mag + dbinv(csi_st.rssi_a); 13 | end 14 | if csi_st.rssi_b ~= 0 15 | rssi_mag = rssi_mag + dbinv(csi_st.rssi_b); 16 | end 17 | if csi_st.rssi_c ~= 0 18 | rssi_mag = rssi_mag + dbinv(csi_st.rssi_c); 19 | end 20 | 21 | ret = db(rssi_mag, 'pow') - 44 - csi_st.agc; 22 | end -------------------------------------------------------------------------------- /DeepFi/getbatchdata.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mars920314/DeepFi/9e7f99c181616d9aa4db18973c08675bdb714e8c/DeepFi/getbatchdata.m -------------------------------------------------------------------------------- /DeepFi/geterror.m: -------------------------------------------------------------------------------- 1 | function err=geterror(dataname,traindataname) 2 | 3 | load(dataname); 4 | load (traindataname); 5 | 6 | [numcases numdims numbatches]=size(batchdata); 7 | N=numcases; 8 | err=0; 9 | for batch = 1:numbatches 10 | data = [batchdata(:,:,batch)]; 11 | data = [data ones(N,1)]; 12 | w1probs = 1./(1 + exp(-data*w1)); w1probs = [w1probs ones(N,1)]; 13 | w2probs = 1./(1 + exp(-w1probs*w2)); w2probs = [w2probs ones(N,1)]; 14 | w3probs = 1./(1 + exp(-w2probs*w3)); w3probs = [w3probs ones(N,1)]; 15 | w4probs = w3probs*w4; w4probs = [w4probs ones(N,1)]; 16 | w5probs = 1./(1 + exp(-w4probs*w5)); w5probs = [w5probs ones(N,1)]; 17 | w6probs = 1./(1 + exp(-w5probs*w6)); w6probs = [w6probs ones(N,1)]; 18 | w7probs = 1./(1 + exp(-w6probs*w7)); w7probs = [w7probs ones(N,1)]; 19 | dataout = 1./(1 + exp(-w7probs*w8)); 20 | % err=err+sum(sum(dataori.*dataout))/(sqrt(sum(sum(dataori.^2)))*sqrt(sum(sum(dataout.^2)))); 21 | err = err + 1/N*sum(sum( (data(:,1:end-1)-dataout).^2 )); 22 | end 23 | err=err/numbatches; 24 | fprintf(1,'squared error: %6.3f ,%s of %s \t \t \n',err, dataname, traindataname); -------------------------------------------------------------------------------- /DeepFi/getonebatchdata.m: -------------------------------------------------------------------------------- 1 | function batchdata=getonebatchdata(filename,N,M,an) 2 | % csi_trace = read_bf_file(filename); 3 | % csi=zeros(3,30,M-N); 4 | % time_csi=zeros(3,30,M-N); 5 | % % figure; 6 | % for i=(N+1):M 7 | % csi_entry = csi_trace{i}; 8 | % csientry = get_scaled_csi(csi_entry); 9 | % perm = csi_entry.perm; 10 | % for k=1:3 11 | % if perm(k)==1 12 | % csi(1,:,i-N)=csientry(1,perm(k),:); 13 | % time_csi(1,:,i-N)=ifft(csi(1,:,i-N)); 14 | % elseif perm(k)==2 15 | % csi(2,:,i-N)=csientry(1,perm(k),:); 16 | % time_csi(2,:,i-N)=ifft(csi(2,:,i-N)); 17 | % elseif perm(k)==3 18 | % csi(3,:,i-N)=csientry(1,perm(k),:); 19 | % time_csi(3,:,i-N)=ifft(csi(3,:,i-N)); 20 | % end 21 | % end 22 | % % hold on 23 | % % plot((abs(squeeze(csi(1,:,i-N)))),'r'); 24 | % end 25 | 26 | load(filename); 27 | csi_tmp=csi; 28 | csi=zeros(3,30,M-N); 29 | csi(:,:,:)=csi_tmp(:,:,[N:M-1]); 30 | 31 | numcases=5; %how to group data 32 | numdims=30; %visible nodes 33 | numbatches=(M-N)/numcases; 34 | batchdata = zeros(numcases, numdims, numbatches); 35 | for b=1:numbatches 36 | for c=1:numcases 37 | batchdata(c,:,b) = [(abs(squeeze(csi(an,:,(b-1)*numcases+c))))]; 38 | % batchdata(c,:,b) = [(sum(abs(squeeze(csi(1,[1:5],(b-1)*numcases+c))))) (sum(abs(squeeze(csi(1,[6:10],(b-1)*numcases+c))))) (sum(abs(squeeze(csi(1,[11:15],(b-1)*numcases+c))))) (sum(abs(squeeze(csi(1,[16:20],(b-1)*numcases+c))))) (sum(abs(squeeze(csi(1,[21:25],(b-1)*numcases+c))))) (sum(abs(squeeze(csi(1,[26:30],(b-1)*numcases+c))))) (sum(abs(squeeze(csi(2,[1:5],(b-1)*numcases+c))))) (sum(abs(squeeze(csi(2,[6:10],(b-1)*numcases+c))))) (sum(abs(squeeze(csi(2,[11:15],(b-1)*numcases+c))))) (sum(abs(squeeze(csi(2,[16:20],(b-1)*numcases+c))))) (sum(abs(squeeze(csi(2,[21:25],(b-1)*numcases+c))))) (sum(abs(squeeze(csi(3,[26:30],(b-1)*numcases+c))))) (sum(abs(squeeze(csi(3,[1:5],(b-1)*numcases+c))))) (sum(abs(squeeze(csi(3,[6:10],(b-1)*numcases+c))))) (sum(abs(squeeze(csi(3,[11:15],(b-1)*numcases+c))))) (sum(abs(squeeze(csi(3,[16:20],(b-1)*numcases+c))))) (sum(abs(squeeze(csi(3,[21:25],(b-1)*numcases+c))))) (sum(abs(squeeze(csi(3,[26:30],(b-1)*numcases+c)))))]; 39 | end 40 | end 41 | batchdata=batchdata/max(max(max(batchdata))); -------------------------------------------------------------------------------- /DeepFi/homeposition.asv: -------------------------------------------------------------------------------- 1 | function [ posi ] = homeposition( posi ) 2 | posi(1)=1335-145*posi(1); 3 | posi(2)=1925-145*posi(2); 4 | 5 | 6 | end 7 | 8 | -------------------------------------------------------------------------------- /DeepFi/homeposition.m: -------------------------------------------------------------------------------- 1 | function [ posi ] = homeposition( posi ) 2 | posi(1)=1325-145*posi(1); 3 | posi(2)=1930-145*posi(2); 4 | 5 | 6 | end 7 | 8 | -------------------------------------------------------------------------------- /DeepFi/labposition.m: -------------------------------------------------------------------------------- 1 | function [ posiout ] = labposition( posiin ) 2 | posiout(1)=236+158*posiin(2); 3 | posiout(2)=5444-158*posiin(1); 4 | 5 | end 6 | 7 | -------------------------------------------------------------------------------- /DeepFi/labvedio.asv: -------------------------------------------------------------------------------- 1 | clear all; 2 | clc; 3 | figure(1); 4 | home=imread('lab.jpg'); 5 | image(home); 6 | axis([500,8000,0,3500]); 7 | axis off; 8 | hold on 9 | text(5600,100,'black point is original location'); 10 | text(5600,200,'blue point is estimate location'); 11 | pause(3); 12 | 13 | errall=zeros(30,1); 14 | for fi=10:10 15 | errpackage=ones(9,9); 16 | errallpackage=zeros(9,9); 17 | for pack=300:300 18 | for k=2:2 19 | filename=['.\data\3lat' num2str(k) num2str(fi)]; 20 | batchdata=getbatchdata(filename,pack,pack+10); 21 | dataname=['testbatchdata']; 22 | save(dataname, 'batchdata'); 23 | err=ones(9,9); 24 | for i=1:9 25 | for j=1:9 26 | traindataname=['.\data\mnist_weights' num2str(k) num2str(i) num2str(j) '.mat']; 27 | if(exist(traindataname)==2) 28 | err(i,j)=errfunc1(dataname,traindataname); 29 | end 30 | end 31 | end 32 | errpackage=errpackage.*err; 33 | end 34 | 35 | sumerr=0; 36 | for i=1:9 37 | for j=1:9 38 | if errpackage(i,j)~=1 39 | sumerr=sumerr+errpackage(i,j); 40 | else 41 | errpackage(i,j)=0; 42 | end 43 | end 44 | end 45 | errpackage=errpackage/sumerr; 46 | errallpackage=errallpackage+errpackage; 47 | end 48 | errallpackage=errallpackage/1; % 49 | 50 | pocal=zeros(2,1); 51 | for i=1:9 52 | for j=1:9 53 | if errallpackage(i,j)~=0 54 | [xout yout]=position(i,j,'lab'); 55 | pocal(1)=pocal(1)+xout*errallpackage(i,j); 56 | pocal(2)=pocal(2)+yout*errallpackage(i,j); 57 | end 58 | end 59 | end 60 | [poori(1) poori(2)]=position(fi,0,'lat'); 61 | errall(fi)=sqrt((pocal(1)-poori(1))^2+(pocal(2)-poori(2))^2)*0.3; 62 | 63 | apoori=labposition(poori); 64 | apocal=labposition(pocal); 65 | setpoori=scatter(apoori(2),apoori(1),100,'k','filled'); 66 | hold on 67 | setpocal=scatter(apocal(2),apocal(1),100,'b','filled'); 68 | hold on 69 | settext=text(5444,400,['location error is' num2str(errall(fi))]); 70 | pause(5); 71 | % set(setpoori,'visible','off'); 72 | % set(setpocal,'visible','off'); 73 | % set(settext,'visible','off'); 74 | end 75 | text(150,1400,['average location error is' num2str(mean(errall))]); 76 | figure(2); 77 | ecdf(errall); -------------------------------------------------------------------------------- /DeepFi/main.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mars920314/DeepFi/9e7f99c181616d9aa4db18973c08675bdb714e8c/DeepFi/main.m -------------------------------------------------------------------------------- /DeepFi/minimize.m: -------------------------------------------------------------------------------- 1 | function [X, fX, i] = minimize(X, f, length, varargin) 2 | 3 | % Minimize a differentiable multivariate function. 4 | % 5 | % Usage: [X, fX, i] = minimize(X, f, length, P1, P2, P3, ... ) 6 | % 7 | % where the starting point is given by "X" (D by 1), and the function named in 8 | % the string "f", must return a function value and a vector of partial 9 | % derivatives of f wrt X, the "length" gives the length of the run: if it is 10 | % positive, it gives the maximum number of line searches, if negative its 11 | % absolute gives the maximum allowed number of function evaluations. You can 12 | % (optionally) give "length" a second component, which will indicate the 13 | % reduction in function value to be expected in the first line-search (defaults 14 | % to 1.0). The parameters P1, P2, P3, ... are passed on to the function f. 15 | % 16 | % The function returns when either its length is up, or if no further progress 17 | % can be made (ie, we are at a (local) minimum, or so close that due to 18 | % numerical problems, we cannot get any closer). NOTE: If the function 19 | % terminates within a few iterations, it could be an indication that the 20 | % function values and derivatives are not consistent (ie, there may be a bug in 21 | % the implementation of your "f" function). The function returns the found 22 | % solution "X", a vector of function values "fX" indicating the progress made 23 | % and "i" the number of iterations (line searches or function evaluations, 24 | % depending on the sign of "length") used. 25 | % 26 | % The Polack-Ribiere flavour of conjugate gradients is used to compute search 27 | % directions, and a line search using quadratic and cubic polynomial 28 | % approximations and the Wolfe-Powell stopping criteria is used together with 29 | % the slope ratio method for guessing initial step sizes. Additionally a bunch 30 | % of checks are made to make sure that exploration is taking place and that 31 | % extrapolation will not be unboundedly large. 32 | % 33 | % See also: checkgrad 34 | % 35 | % Copyright (C) 2001 - 2006 by Carl Edward Rasmussen (2006-09-08). 36 | 37 | INT = 0.1; % don't reevaluate within 0.1 of the limit of the current bracket 38 | EXT = 3.0; % extrapolate maximum 3 times the current step-size 39 | MAX = 20; % max 20 function evaluations per line search 40 | RATIO = 10; % maximum allowed slope ratio 41 | SIG = 0.1; RHO = SIG/2; % SIG and RHO are the constants controlling the Wolfe- 42 | % Powell conditions. SIG is the maximum allowed absolute ratio between 43 | % previous and new slopes (derivatives in the search direction), thus setting 44 | % SIG to low (positive) values forces higher precision in the line-searches. 45 | % RHO is the minimum allowed fraction of the expected (from the slope at the 46 | % initial point in the linesearch). Constants must satisfy 0 < RHO < SIG < 1. 47 | % Tuning of SIG (depending on the nature of the function to be optimized) may 48 | % speed up the minimization; it is probably not worth playing much with RHO. 49 | 50 | % The code falls naturally into 3 parts, after the initial line search is 51 | % started in the direction of steepest descent. 1) we first enter a while loop 52 | % which uses point 1 (p1) and (p2) to compute an extrapolation (p3), until we 53 | % have extrapolated far enough (Wolfe-Powell conditions). 2) if necessary, we 54 | % enter the second loop which takes p2, p3 and p4 chooses the subinterval 55 | % containing a (local) minimum, and interpolates it, unil an acceptable point 56 | % is found (Wolfe-Powell conditions). Note, that points are always maintained 57 | % in order p0 <= p1 <= p2 < p3 < p4. 3) compute a new search direction using 58 | % conjugate gradients (Polack-Ribiere flavour), or revert to steepest if there 59 | % was a problem in the previous line-search. Return the best value so far, if 60 | % two consecutive line-searches fail, or whenever we run out of function 61 | % evaluations or line-searches. During extrapolation, the "f" function may fail 62 | % either with an error or returning Nan or Inf, and minimize should handle this 63 | % gracefully. 64 | 65 | if max(size(length)) == 2, red=length(2); length=length(1); else red=1; end 66 | if length>0, S='Linesearch'; else S='Function evaluation'; end 67 | 68 | i = 0; % zero the run length counter 69 | ls_failed = 0; % no previous line search has failed 70 | [f0 df0] = feval(f, X, varargin{:}); % get function value and gradient 71 | fX = f0; 72 | i = i + (length<0); % count epochs?! 73 | s = -df0; d0 = -s'*s; % initial search direction (steepest) and slope 74 | x3 = red/(1-d0); % initial step is red/(|s|+1) 75 | 76 | while i < abs(length) % while not finished 77 | i = i + (length>0); % count iterations?! 78 | 79 | X0 = X; F0 = f0; dF0 = df0; % make a copy of current values 80 | if length>0, M = MAX; else M = min(MAX, -length-i); end 81 | 82 | while 1 % keep extrapolating as long as necessary 83 | x2 = 0; f2 = f0; d2 = d0; f3 = f0; df3 = df0; 84 | success = 0; 85 | while ~success && M > 0 86 | try 87 | M = M - 1; i = i + (length<0); % count epochs?! 88 | [f3 df3] = feval(f, X+x3*s, varargin{:}); 89 | if isnan(f3) || isinf(f3) || any(isnan(df3)+isinf(df3)), error(''), end 90 | success = 1; 91 | catch % catch any error which occured in f 92 | x3 = (x2+x3)/2; % bisect and try again 93 | end 94 | end 95 | if f3 < F0, X0 = X+x3*s; F0 = f3; dF0 = df3; end % keep best values 96 | d3 = df3'*s; % new slope 97 | if d3 > SIG*d0 || f3 > f0+x3*RHO*d0 || M == 0 % are we done extrapolating? 98 | break 99 | end 100 | x1 = x2; f1 = f2; d1 = d2; % move point 2 to point 1 101 | x2 = x3; f2 = f3; d2 = d3; % move point 3 to point 2 102 | A = 6*(f1-f2)+3*(d2+d1)*(x2-x1); % make cubic extrapolation 103 | B = 3*(f2-f1)-(2*d1+d2)*(x2-x1); 104 | x3 = x1-d1*(x2-x1)^2/(B+sqrt(B*B-A*d1*(x2-x1))); % num. error possible, ok! 105 | if ~isreal(x3) || isnan(x3) || isinf(x3) || x3 < 0 % num prob | wrong sign? 106 | x3 = x2*EXT; % extrapolate maximum amount 107 | elseif x3 > x2*EXT % new point beyond extrapolation limit? 108 | x3 = x2*EXT; % extrapolate maximum amount 109 | elseif x3 < x2+INT*(x2-x1) % new point too close to previous point? 110 | x3 = x2+INT*(x2-x1); 111 | end 112 | end % end extrapolation 113 | 114 | while (abs(d3) > -SIG*d0 || f3 > f0+x3*RHO*d0) && M > 0 % keep interpolating 115 | if d3 > 0 || f3 > f0+x3*RHO*d0 % choose subinterval 116 | x4 = x3; f4 = f3; d4 = d3; % move point 3 to point 4 117 | else 118 | x2 = x3; f2 = f3; d2 = d3; % move point 3 to point 2 119 | end 120 | if f4 > f0 121 | x3 = x2-(0.5*d2*(x4-x2)^2)/(f4-f2-d2*(x4-x2)); % quadratic interpolation 122 | else 123 | A = 6*(f2-f4)/(x4-x2)+3*(d4+d2); % cubic interpolation 124 | B = 3*(f4-f2)-(2*d2+d4)*(x4-x2); 125 | x3 = x2+(sqrt(B*B-A*d2*(x4-x2)^2)-B)/A; % num. error possible, ok! 126 | end 127 | if isnan(x3) || isinf(x3) 128 | x3 = (x2+x4)/2; % if we had a numerical problem then bisect 129 | end 130 | x3 = max(min(x3, x4-INT*(x4-x2)),x2+INT*(x4-x2)); % don't accept too close 131 | [f3 df3] = feval(f, X+x3*s, varargin{:}); 132 | if f3 < F0, X0 = X+x3*s; F0 = f3; dF0 = df3; end % keep best values 133 | M = M - 1; i = i + (length<0); % count epochs?! 134 | d3 = df3'*s; % new slope 135 | end % end interpolation 136 | 137 | if abs(d3) < -SIG*d0 && f3 < f0+x3*RHO*d0 % if line search succeeded 138 | X = X+x3*s; f0 = f3; fX = [fX' f0]'; % update variables 139 | fprintf('%s %6i; Value %4.6e\r', S, i, f0); 140 | s = (df3'*df3-df0'*df3)/(df0'*df0)*s - df3; % Polack-Ribiere CG direction 141 | df0 = df3; % swap derivatives 142 | d3 = d0; d0 = df0'*s; 143 | if d0 > 0 % new slope must be negative 144 | s = -df0; d0 = -s'*s; % otherwise use steepest direction 145 | end 146 | x3 = x3 * min(RATIO, d3/(d0-realmin)); % slope ratio but max RATIO 147 | ls_failed = 0; % this line search did not fail 148 | else 149 | X = X0; f0 = F0; df0 = dF0; % restore best point so far 150 | if ls_failed || i > abs(length) % line search failed twice in a row 151 | break; % or we ran out of time, so we give up 152 | end 153 | s = -df0; d0 = -s'*s; % try steepest 154 | x3 = 1/(1-d0); 155 | ls_failed = 1; % this line search failed 156 | end 157 | end 158 | fprintf('\n'); -------------------------------------------------------------------------------- /DeepFi/position.m: -------------------------------------------------------------------------------- 1 | function [xout,yout]=position(xin,yin,opts) 2 | ap1=[10,3]; 3 | ap2=[10,26]; 4 | ap3=[11,12]; 5 | if opts=='lat' 6 | a=ceil(xin/6); 7 | b=xin-6*(a-1); 8 | switch a 9 | case 1 10 | xout=3; 11 | case 2 12 | xout=5+3; 13 | case 3 14 | xout=5+5+3; 15 | case 4 16 | xout=5+5+5+3; 17 | case 5 18 | xout=5+5+5+5+3; 19 | end 20 | yout=2*(b-1)+3; 21 | 22 | elseif opts=='lab' 23 | if yin<8 24 | switch xin 25 | case 1 26 | xout=2; 27 | case 2 28 | xout=2+2; 29 | case 3 30 | xout=5+2+2; 31 | case 4 32 | xout=5+5+2+2; 33 | case 5 34 | xout=5+5+5+2+2; 35 | case 6 36 | xout=5+5+5+5+2+2; 37 | case 7 38 | xout=2+5+5+5+5+2+2; 39 | end 40 | yout=2*(yin-1)+2; 41 | elseif yin==8 42 | switch xin 43 | case 2 44 | xout=3+2+2; 45 | case 3 46 | xout=5+3+2+2; 47 | case 4 48 | xout=5+5+3+2+2; 49 | case 5 50 | xout=5+5+5+3+2+2; 51 | end 52 | yout=2*6+2; 53 | end 54 | end -------------------------------------------------------------------------------- /DeepFi/rbm.m: -------------------------------------------------------------------------------- 1 | % Version 1.000 2 | % 3 | % Code provided by Geoff Hinton and Ruslan Salakhutdinov 4 | % 5 | % Permission is granted for anyone to copy, use, modify, or distribute this 6 | % program and accompanying programs and documents for any purpose, provided 7 | % this copyright notice is retained and prominently displayed, along with 8 | % a note saying that the original programs are available from our 9 | % web page. 10 | % The programs and documents are distributed without any warranty, express or 11 | % implied. As the programs were written for research purposes only, they have 12 | % not been tested to the degree that would be advisable in any important 13 | % application. All use of these programs is entirely at the user's own risk. 14 | 15 | % This program trains Restricted Boltzmann Machine in which 16 | % visible, binary, stochastic pixels are connected to 17 | % hidden, binary, stochastic feature detectors using symmetrically 18 | % weighted connections. Learning is done with 1-step Contrastive Divergence. 19 | % The program assumes that the following variables are set externally: 20 | % maxepoch -- maximum number of epochs 21 | % numhid -- number of hidden units 22 | % batchdata -- the data that is divided into batches (numcases numdims numbatches) 23 | % restart -- set to 1 if learning starts from beginning 24 | 25 | epsilonw = 0.1; % Learning rate for weights 26 | epsilonvb = 0.1; % Learning rate for biases of visible units 27 | epsilonhb = 0.1; % Learning rate for biases of hidden units 28 | weightcost = 0.0002; 29 | initialmomentum = 0.5; 30 | finalmomentum = 0.9; 31 | 32 | [numcases numdims numbatches]=size(batchdata); 33 | 34 | if restart ==1, 35 | restart=0; 36 | epoch=1; 37 | 38 | % Initializing symmetric weights and biases. 39 | vishid = 0.1*randn(numdims, numhid); 40 | hidbiases = zeros(1,numhid); 41 | visbiases = zeros(1,numdims); 42 | 43 | poshidprobs = zeros(numcases,numhid); 44 | neghidprobs = zeros(numcases,numhid); 45 | posprods = zeros(numdims,numhid); 46 | negprods = zeros(numdims,numhid); 47 | vishidinc = zeros(numdims,numhid); 48 | hidbiasinc = zeros(1,numhid); 49 | visbiasinc = zeros(1,numdims); 50 | batchposhidprobs=zeros(numcases,numhid,numbatches); 51 | end 52 | 53 | for epoch = epoch:maxepoch, 54 | fprintf(1,'epoch %d\r',epoch); 55 | errsum=0; 56 | for batch = 1:numbatches, 57 | % fprintf(1,'epoch %d batch %d\r',epoch,batch); 58 | 59 | %%%%%%%%% START POSITIVE PHASE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 60 | data = batchdata(:,:,batch); 61 | poshidprobs = 1./(1 + exp(-data*vishid - repmat(hidbiases,numcases,1))); 62 | batchposhidprobs(:,:,batch)=poshidprobs; 63 | posprods = data' * poshidprobs; 64 | poshidact = sum(poshidprobs); 65 | posvisact = sum(data); 66 | 67 | %%%%%%%%% END OF POSITIVE PHASE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 68 | poshidstates = poshidprobs > rand(numcases,numhid); 69 | 70 | %%%%%%%%% START NEGATIVE PHASE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 71 | negdata = 1./(1 + exp(-poshidstates*vishid' - repmat(visbiases,numcases,1))); 72 | neghidprobs = 1./(1 + exp(-negdata*vishid - repmat(hidbiases,numcases,1))); 73 | negprods = negdata'*neghidprobs; 74 | neghidact = sum(neghidprobs); 75 | negvisact = sum(negdata); 76 | 77 | %%%%%%%%% END OF NEGATIVE PHASE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 78 | err= sum(sum( (data-negdata).^2 )); 79 | errsum = err + errsum; 80 | 81 | if epoch>5, 82 | momentum=finalmomentum; 83 | else 84 | momentum=initialmomentum; 85 | end; 86 | 87 | %%%%%%%%% UPDATE WEIGHTS AND BIASES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 88 | vishidinc = momentum*vishidinc + ... 89 | epsilonw*( (posprods-negprods)/numcases - weightcost*vishid); 90 | visbiasinc = momentum*visbiasinc + (epsilonvb/numcases)*(posvisact-negvisact); 91 | hidbiasinc = momentum*hidbiasinc + (epsilonhb/numcases)*(poshidact-neghidact); 92 | 93 | vishid = vishid + vishidinc; 94 | visbiases = visbiases + visbiasinc; 95 | hidbiases = hidbiases + hidbiasinc; 96 | 97 | %%%%%%%%%%%%%%%% END OF UPDATES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 98 | 99 | end 100 | fprintf(1, 'epoch %4i error %6.1f \n', epoch, errsum); 101 | end; 102 | -------------------------------------------------------------------------------- /DeepFi/rbmhidlinear.m: -------------------------------------------------------------------------------- 1 | % Version 1.000 2 | % 3 | % Code provided by Ruslan Salakhutdinov and Geoff Hinton 4 | % 5 | % Permission is granted for anyone to copy, use, modify, or distribute this 6 | % program and accompanying programs and documents for any purpose, provided 7 | % this copyright notice is retained and prominently displayed, along with 8 | % a note saying that the original programs are available from our 9 | % web page. 10 | % The programs and documents are distributed without any warranty, express or 11 | % implied. As the programs were written for research purposes only, they have 12 | % not been tested to the degree that would be advisable in any important 13 | % application. All use of these programs is entirely at the user's own risk. 14 | 15 | % This program trains Restricted Boltzmann Machine in which 16 | % visible, binary, stochastic pixels are connected to 17 | % hidden, tochastic real-valued feature detectors drawn from a unit 18 | % variance Gaussian whose mean is determined by the input from 19 | % the logistic visible units. Learning is done with 1-step Contrastive Divergence. 20 | % The program assumes that the following variables are set externally: 21 | % maxepoch -- maximum number of epochs 22 | % numhid -- number of hidden units 23 | % batchdata -- the data that is divided into batches (numcases numdims numbatches) 24 | % restart -- set to 1 if learning starts from beginning 25 | 26 | epsilonw = 0.001; % Learning rate for weights 27 | epsilonvb = 0.001; % Learning rate for biases of visible units 28 | epsilonhb = 0.001; % Learning rate for biases of hidden units 29 | weightcost = 0.0002; 30 | initialmomentum = 0.5; 31 | finalmomentum = 0.9; 32 | 33 | 34 | [numcases numdims numbatches]=size(batchdata); 35 | 36 | if restart ==1, 37 | restart=0; 38 | epoch=1; 39 | 40 | % Initializing symmetric weights and biases. 41 | vishid = 0.1*randn(numdims, numhid); 42 | hidbiases = zeros(1,numhid); 43 | visbiases = zeros(1,numdims); 44 | 45 | 46 | poshidprobs = zeros(numcases,numhid); 47 | neghidprobs = zeros(numcases,numhid); 48 | posprods = zeros(numdims,numhid); 49 | negprods = zeros(numdims,numhid); 50 | vishidinc = zeros(numdims,numhid); 51 | hidbiasinc = zeros(1,numhid); 52 | visbiasinc = zeros(1,numdims); 53 | sigmainc = zeros(1,numhid); 54 | batchposhidprobs=zeros(numcases,numhid,numbatches); 55 | end 56 | 57 | for epoch = epoch:maxepoch, 58 | fprintf(1,'epoch %d\r',epoch); 59 | errsum=0; 60 | 61 | for batch = 1:numbatches, 62 | % fprintf(1,'epoch %d batch %d\r',epoch,batch); 63 | 64 | %%%%%%%%% START POSITIVE PHASE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 65 | data = batchdata(:,:,batch); 66 | poshidprobs = (data*vishid) + repmat(hidbiases,numcases,1); 67 | batchposhidprobs(:,:,batch)=poshidprobs; 68 | posprods = data' * poshidprobs; 69 | poshidact = sum(poshidprobs); 70 | posvisact = sum(data); 71 | 72 | %%%%%%%%% END OF POSITIVE PHASE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 73 | poshidstates = poshidprobs+randn(numcases,numhid); 74 | 75 | %%%%%%%%% START NEGATIVE PHASE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 76 | negdata = 1./(1 + exp(-poshidstates*vishid' - repmat(visbiases,numcases,1))); 77 | neghidprobs = (negdata*vishid) + repmat(hidbiases,numcases,1); 78 | negprods = negdata'*neghidprobs; 79 | neghidact = sum(neghidprobs); 80 | negvisact = sum(negdata); 81 | 82 | %%%%%%%%% END OF NEGATIVE PHASE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 83 | 84 | 85 | err= sum(sum( (data-negdata).^2 )); 86 | errsum = err + errsum; 87 | if epoch>5, 88 | momentum=finalmomentum; 89 | else 90 | momentum=initialmomentum; 91 | end; 92 | 93 | %%%%%%%%% UPDATE WEIGHTS AND BIASES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 94 | vishidinc = momentum*vishidinc + ... 95 | epsilonw*( (posprods-negprods)/numcases - weightcost*vishid); 96 | visbiasinc = momentum*visbiasinc + (epsilonvb/numcases)*(posvisact-negvisact); 97 | hidbiasinc = momentum*hidbiasinc + (epsilonhb/numcases)*(poshidact-neghidact); 98 | vishid = vishid + vishidinc; 99 | visbiases = visbiases + visbiasinc; 100 | hidbiases = hidbiases + hidbiasinc; 101 | 102 | %%%%%%%%%%%%%%%% END OF UPDATES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 103 | 104 | end 105 | fprintf(1, 'epoch %4i error %f \n', epoch, errsum); 106 | 107 | end 108 | -------------------------------------------------------------------------------- /DeepFi/read_bf_file.m: -------------------------------------------------------------------------------- 1 | %READ_BF_FILE Reads in a file of beamforming feedback logs. 2 | % This version uses the *C* version of read_bfee, compiled with 3 | % MATLAB's MEX utility. 4 | % 5 | % (c) 2008-2011 Daniel Halperin 6 | % 7 | function ret = read_bf_file(filename) 8 | %% Input check 9 | error(nargchk(1,1,nargin)); 10 | 11 | %% Open file 12 | f = fopen(filename, 'rb'); 13 | if (f < 0) 14 | error('Couldn''t open file %s', filename); 15 | return; 16 | end 17 | 18 | status = fseek(f, 0, 'eof'); 19 | if status ~= 0 20 | [msg, errno] = ferror(f); 21 | error('Error %d seeking: %s', errno, msg); 22 | fclose(f); 23 | return; 24 | end 25 | len = ftell(f); 26 | 27 | status = fseek(f, 0, 'bof'); 28 | if status ~= 0 29 | [msg, errno] = ferror(f); 30 | error('Error %d seeking: %s', errno, msg); 31 | fclose(f); 32 | return; 33 | end 34 | 35 | %% Initialize variables 36 | ret = cell(ceil(len/95),1); % Holds the return values - 1x1 CSI is 95 bytes big, so this should be upper bound 37 | cur = 0; % Current offset into file 38 | count = 0; % Number of records output 39 | broken_perm = 0; % Flag marking whether we've encountered a broken CSI yet 40 | triangle = [1 3 6]; % What perm should sum to for 1,2,3 antennas 41 | 42 | %% Process all entries in file 43 | % Need 3 bytes -- 2 byte size field and 1 byte code 44 | while cur < (len - 3) 45 | % Read size and code 46 | field_len = fread(f, 1, 'uint16', 0, 'ieee-be'); 47 | code = fread(f,1); 48 | cur = cur+3; 49 | 50 | % If unhandled code, skip (seek over) the record and continue 51 | if (code == 187) % get beamforming or phy data 52 | bytes = fread(f, field_len-1, 'uint8=>uint8'); 53 | cur = cur + field_len - 1; 54 | if (length(bytes) ~= field_len-1) 55 | fclose(f); 56 | return; 57 | end 58 | else % skip all other info 59 | fseek(f, field_len - 1, 'cof'); 60 | cur = cur + field_len - 1; 61 | continue; 62 | end 63 | 64 | if (code == 187) %hex2dec('bb')) Beamforming matrix -- output a record 65 | count = count + 1; 66 | ret{count} = read_bfee(bytes); 67 | 68 | perm = ret{count}.perm; 69 | Nrx = ret{count}.Nrx; 70 | if Nrx == 1 % No permuting needed for only 1 antenna 71 | continue; 72 | end 73 | if sum(perm) ~= triangle(Nrx) % matrix does not contain default values 74 | if broken_perm == 0 75 | broken_perm = 1; 76 | fprintf('WARN ONCE: Found CSI (%s) with Nrx=%d and invalid perm=[%s]\n', filename, Nrx, int2str(perm)); 77 | end 78 | else 79 | ret{count}.csi(:,perm(1:Nrx),:) = ret{count}.csi(:,1:Nrx,:); 80 | end 81 | end 82 | end 83 | ret = ret(1:count); 84 | 85 | %% Close file 86 | fclose(f); 87 | end 88 | -------------------------------------------------------------------------------- /DeepFi/read_bfee.c: -------------------------------------------------------------------------------- 1 | /* 2 | * (c) 2008-2011 Daniel Halperin 3 | */ 4 | #include "mex.h" 5 | 6 | /* The computational routine */ 7 | void read_bfee(unsigned char *inBytes, mxArray *outCell) 8 | { 9 | unsigned long timestamp_low = inBytes[0] + (inBytes[1] << 8) + 10 | (inBytes[2] << 16) + (inBytes[3] << 24); 11 | unsigned short bfee_count = inBytes[4] + (inBytes[5] << 8); 12 | unsigned int Nrx = inBytes[8]; 13 | unsigned int Ntx = inBytes[9]; 14 | unsigned int rssi_a = inBytes[10]; 15 | unsigned int rssi_b = inBytes[11]; 16 | unsigned int rssi_c = inBytes[12]; 17 | char noise = inBytes[13]; 18 | unsigned int agc = inBytes[14]; 19 | unsigned int antenna_sel = inBytes[15]; 20 | unsigned int len = inBytes[16] + (inBytes[17] << 8); 21 | unsigned int fake_rate_n_flags = inBytes[18] + (inBytes[19] << 8); 22 | unsigned int calc_len = (30 * (Nrx * Ntx * 8 * 2 + 3) + 7) / 8; 23 | unsigned int i, j; 24 | unsigned int index = 0, remainder; 25 | unsigned char *payload = &inBytes[20]; 26 | char tmp; 27 | int size[] = {Ntx, Nrx, 30}; 28 | mxArray *csi = mxCreateNumericArray(3, size, mxDOUBLE_CLASS, mxCOMPLEX); 29 | double* ptrR = (double *)mxGetPr(csi); 30 | double* ptrI = (double *)mxGetPi(csi); 31 | 32 | /* Check that length matches what it should */ 33 | if (len != calc_len) 34 | mexErrMsgIdAndTxt("MIMOToolbox:read_bfee_new:size","Wrong beamforming matrix size."); 35 | 36 | /* Compute CSI from all this crap :) */ 37 | for (i = 0; i < 30; ++i) 38 | { 39 | index += 3; 40 | remainder = index % 8; 41 | for (j = 0; j < Nrx * Ntx; ++j) 42 | { 43 | tmp = (payload[index / 8] >> remainder) | 44 | (payload[index/8+1] << (8-remainder)); 45 | //printf("%d\n", tmp); 46 | *ptrR = (double) tmp; 47 | ++ptrR; 48 | tmp = (payload[index / 8+1] >> remainder) | 49 | (payload[index/8+2] << (8-remainder)); 50 | *ptrI = (double) tmp; 51 | ++ptrI; 52 | index += 16; 53 | } 54 | } 55 | 56 | /* Compute the permutation array */ 57 | int perm_size[] = {1, 3}; 58 | mxArray *perm = mxCreateNumericArray(2, perm_size, mxDOUBLE_CLASS, mxREAL); 59 | ptrR = (double *)mxGetPr(perm); 60 | ptrR[0] = ((antenna_sel) & 0x3) + 1; 61 | ptrR[1] = ((antenna_sel >> 2) & 0x3) + 1; 62 | ptrR[2] = ((antenna_sel >> 4) & 0x3) + 1; 63 | 64 | mxDestroyArray(mxGetField(outCell, 0, "timestamp_low")); 65 | mxDestroyArray(mxGetField(outCell, 0, "bfee_count")); 66 | mxDestroyArray(mxGetField(outCell, 0, "Nrx")); 67 | mxDestroyArray(mxGetField(outCell, 0, "Ntx")); 68 | mxDestroyArray(mxGetField(outCell, 0, "rssi_a")); 69 | mxDestroyArray(mxGetField(outCell, 0, "rssi_b")); 70 | mxDestroyArray(mxGetField(outCell, 0, "rssi_c")); 71 | mxDestroyArray(mxGetField(outCell, 0, "noise")); 72 | mxDestroyArray(mxGetField(outCell, 0, "agc")); 73 | mxDestroyArray(mxGetField(outCell, 0, "perm")); 74 | mxDestroyArray(mxGetField(outCell, 0, "rate")); 75 | mxDestroyArray(mxGetField(outCell, 0, "csi")); 76 | mxSetField(outCell, 0, "timestamp_low", mxCreateDoubleScalar((double)timestamp_low)); 77 | mxSetField(outCell, 0, "bfee_count", mxCreateDoubleScalar((double)bfee_count)); 78 | mxSetField(outCell, 0, "Nrx", mxCreateDoubleScalar((double)Nrx)); 79 | mxSetField(outCell, 0, "Ntx", mxCreateDoubleScalar((double)Ntx)); 80 | mxSetField(outCell, 0, "rssi_a", mxCreateDoubleScalar((double)rssi_a)); 81 | mxSetField(outCell, 0, "rssi_b", mxCreateDoubleScalar((double)rssi_b)); 82 | mxSetField(outCell, 0, "rssi_c", mxCreateDoubleScalar((double)rssi_c)); 83 | mxSetField(outCell, 0, "noise", mxCreateDoubleScalar((double)noise)); 84 | mxSetField(outCell, 0, "agc", mxCreateDoubleScalar((double)agc)); 85 | mxSetField(outCell, 0, "perm", perm); 86 | mxSetField(outCell, 0, "rate", mxCreateDoubleScalar((double)fake_rate_n_flags)); 87 | mxSetField(outCell, 0, "csi", csi); 88 | 89 | //printf("Nrx: %u Ntx: %u len: %u calc_len: %u\n", Nrx, Ntx, len, calc_len); 90 | } 91 | 92 | /* The gateway function */ 93 | void mexFunction(int nlhs, mxArray *plhs[], 94 | int nrhs, const mxArray *prhs[]) 95 | { 96 | unsigned char *inBytes; /* A beamforming matrix */ 97 | mxArray *outCell; /* The cellular output */ 98 | 99 | /* check for proper number of arguments */ 100 | if(nrhs!=1) { 101 | mexErrMsgIdAndTxt("MIMOToolbox:read_bfee_new:nrhs","One input required."); 102 | } 103 | if(nlhs!=1) { 104 | mexErrMsgIdAndTxt("MIMOToolbox:read_bfee_new:nlhs","One output required."); 105 | } 106 | /* make sure the input argument is a char array */ 107 | if (!mxIsClass(prhs[0], "uint8")) { 108 | mexErrMsgIdAndTxt("MIMOToolbox:read_bfee_new:notBytes","Input must be a char array"); 109 | } 110 | 111 | /* create a pointer to the real data in the input matrix */ 112 | inBytes = mxGetData(prhs[0]); 113 | 114 | /* create the output matrix */ 115 | const char* fieldnames[] = {"timestamp_low", 116 | "bfee_count", 117 | "Nrx", "Ntx", 118 | "rssi_a", "rssi_b", "rssi_c", 119 | "noise", 120 | "agc", 121 | "perm", 122 | "rate", 123 | "csi"}; 124 | outCell = mxCreateStructMatrix(1, 1, 12, fieldnames); 125 | 126 | 127 | /* call the computational routine */ 128 | read_bfee(inBytes,outCell); 129 | 130 | /* */ 131 | plhs[0] = outCell; 132 | } 133 | -------------------------------------------------------------------------------- /DeepFi/read_bfee.mexa64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mars920314/DeepFi/9e7f99c181616d9aa4db18973c08675bdb714e8c/DeepFi/read_bfee.mexa64 -------------------------------------------------------------------------------- /DeepFi/read_bfee.mexmaci64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mars920314/DeepFi/9e7f99c181616d9aa4db18973c08675bdb714e8c/DeepFi/read_bfee.mexmaci64 -------------------------------------------------------------------------------- /DeepFi/read_bfee.mexw32: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mars920314/DeepFi/9e7f99c181616d9aa4db18973c08675bdb714e8c/DeepFi/read_bfee.mexw32 -------------------------------------------------------------------------------- /DeepFi/savedata.m: -------------------------------------------------------------------------------- 1 | clc 2 | clear all 3 | for jj=2:2 4 | for j=73:75 5 | filename=['.\data\3lab' num2str(jj) num2str(j)]; 6 | % filename=['.\data\x' num2str(j)]; 7 | if(exist(filename)==2) 8 | M=1000; 9 | N=1; 10 | csi_trace = read_bf_file(filename); 11 | csi=zeros(3,30,M-N); 12 | for i=(N+1):M 13 | csi_entry = csi_trace{i}; 14 | csientry = get_scaled_csi(csi_entry); 15 | perm = csi_entry.perm; 16 | for k=1:3 17 | if perm(k)==1 18 | csi(1,:,i-N)=csientry(1,perm(k),:); 19 | % time_csi(1,:,i-N)=ifft(csi(1,:,i-N),point); 20 | % time_csi_abs(1,:,i-N)=abs(time_csi(1,:,i-N)); 21 | elseif perm(k)==2 22 | csi(2,:,i-N)=csientry(1,perm(k),:); 23 | % time_csi(2,:,i-N)=ifft(csi(2,:,i-N),point); 24 | % time_csi_abs(2,:,i-N)=abs(time_csi(2,:,i-N)); 25 | elseif perm(k)==3 26 | csi(3,:,i-N)=csientry(1,perm(k),:); 27 | % time_csi(3,:,i-N)=ifft(csi(3,:,i-N),point); 28 | % time_csi_abs(3,:,i-N)=abs(time_csi(3,:,i-N)); 29 | end 30 | end 31 | end 32 | csi=squeeze(csi); 33 | save(filename,'csi'); 34 | end 35 | end 36 | end -------------------------------------------------------------------------------- /DeepFi/test.m: -------------------------------------------------------------------------------- 1 | clear all 2 | clc 3 | 4 | errall=zeros(30,3); 5 | for k=2:2 6 | for fi=1:30 7 | filename=['.\data\3lat' num2str(k) num2str(fi) '.mat']; 8 | batchdata=getbatchdata(filename,300,400); 9 | % batchdata=getonebatchdata(filename,300,400,2); 10 | dataname=['testbatchdata']; 11 | save(dataname, 'batchdata'); 12 | err=zeros(9,9); 13 | tic 14 | for i=1:9 15 | for j=1:9 16 | traindataname=['K:\test\antenna\data3\mnist_weights' num2str(k) num2str(i) num2str(j) '.mat']; 17 | if(exist(traindataname)==2) 18 | err(i,j)=errfunc1(dataname,traindataname); 19 | end 20 | end 21 | end 22 | toc 23 | % [a1 b1]=max(err); 24 | % [a2 b2]=max(a1); 25 | % pocal=[b1(b2) b2]; 26 | % [pocal(1) pocal(2)]=position(pocal(1),pocal(2),'lab'); 27 | % [poori(1) poori(2)]=position(fi,0,'lat'); 28 | % errall(fi,k)=sqrt((pocal(1)-poori(1))^2+(pocal(2)-poori(2))^2); 29 | 30 | sumerr=0; 31 | for i=1:9 32 | for j=1:9 33 | if err(i,j)~=1 34 | sumerr=sumerr+err(i,j); 35 | else 36 | err(i,j)=0; 37 | end 38 | end 39 | end 40 | err=err/sumerr; 41 | err=err/1; % 42 | pocal=zeros(2,1); 43 | for i=1:9 44 | for j=1:9 45 | if err(i,j)~=0 46 | [xout yout]=position(i,j,'lab'); 47 | pocal(1)=pocal(1)+xout*err(i,j); 48 | pocal(2)=pocal(2)+yout*err(i,j); 49 | end 50 | end 51 | end 52 | [poori(1) poori(2)]=position(fi,0,'lat'); 53 | errall(fi,k)=sqrt((pocal(1)-poori(1))^2+(pocal(2)-poori(2))^2); 54 | fi 55 | errall(fi,k) 56 | 57 | end 58 | end 59 | mean(errall)*0.3 60 | median(errall)*0.3 61 | ecdf(errall(:,k)*0.3); -------------------------------------------------------------------------------- /DeepFi/testall.m: -------------------------------------------------------------------------------- 1 | %test 2 | clear all 3 | close all 4 | % clc 5 | for ii=3:3 6 | for jj=6:6 7 | filename=['.\data\x' num2str(ii) num2str(jj)]; 8 | if(exist(filename)==2) 9 | end 10 | batchdata=getbatchdata(filename,100,200); 11 | dataname=['testbatchdata']; 12 | save(dataname, 'batchdata'); 13 | 14 | err=zeros(6,9); 15 | for i=1:6 16 | for j=1:9 17 | traindataname=['.\data\mnist_weights' num2str(i) num2str(j) '.mat']; 18 | if(exist(traindataname)==2) 19 | err(i,j)=geterror(dataname,traindataname); 20 | end 21 | end 22 | end 23 | err 24 | % err