Last active
March 9, 2018 13:32
-
-
Save HYChou0515/2f1ac03cfafb2045f65ecdafc3db987b to your computer and use it in GitHub Desktop.
Simple Hebbian Network
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% train neurons | |
nr_class=3; | |
A=im2bin(imread('A.bmp')); | |
B=im2bin(imread('B.bmp')); | |
C=im2bin(imread('C.bmp')); | |
trainIms=cat(3,A,B,C); | |
[nr_row,nr_col]=size(A); | |
neurons=zeros(nr_row*nr_col,nr_row*nr_col); | |
for i=1:nr_row*nr_col | |
for j=i+1:nr_row*nr_col | |
ir=int16(floor((i-1)/nr_col)+1); | |
ic=int16(mod((i-1),nr_col)+1); | |
jr=int16(floor((j-1)/nr_col)+1); | |
jc=int16(mod((j-1),nr_col)+1); | |
neurons(i,j)=sum(trainIms(ir,ic,:).*trainIms(jr,jc,:)); | |
neurons(j,i)=neurons(i,j); | |
end | |
end | |
% run test neuron | |
t1=im2bin(imread('test/1.bmp')); | |
showMatAsIm(t1); | |
oldt1=t1; | |
stopCondition=false; | |
while ~stopCondition | |
tmpt1=int8(zeros(size(t1))); | |
for i=1:nr_row*nr_col | |
for j=1:nr_row*nr_col | |
ir=int16(floor((i-1)/nr_col)+1); | |
ic=int16(mod((i-1),nr_col)+1); | |
jr=int16(floor((j-1)/nr_col)+1); | |
jc=int16(mod((j-1),nr_col)+1); | |
tmpt1(ir,ic)=tmpt1(ir,ic)+neurons(i,j)*t1(jr,jc); | |
end | |
end | |
t1=sign(tmpt1); | |
stopCondition=isequal(t1,oldt1); | |
oldt1=t1; | |
end | |
showMatAsIm(t1); | |
function binary = im2bin(im) | |
%change unit8 to +1/-1 | |
binary=double(im./255.*2)-1; | |
binary=binary(:,:,1); | |
end | |
function showMatAsIm(matrix) | |
matrix=uint8(matrix); | |
matrix(matrix==-1)=0; | |
matrix(matrix==1)=255; | |
imshow(matrix,'InitialMagnification',2000) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment