Hopfield Neural Network model

First we define patterns 0 to 9 as 10  x 10 grids

\(zero\  = \ {{0, 0, 0, 0, 0, 0, 0, 0, 0, 
          0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 0, 0, 1, 1, 1, 1, 
          0, 0, 0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 0, 1, 1, 1, 
          1, 1, 1, 0, 0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 0, \ 
          1, 1, 0, 0, 1, 1, 0, 
          0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 1, 1, 0, 0, 0, 0, 
          1, 1, 0}, 
	\ \ {0, 1, 1, 0, 0, 0, 0, 1, 1, 
          0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 1, 1, 1, 0, 0, 1, 
          1, 1, 0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 0, 1, 1, 1, 
          1, 1, 1, 0, 0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 0, \ 
          0, 1, 1, 1, 1, 0, 0, 
          0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 0, 0, 0, 0, 0, 0, 
          0, 0, 0}\ }\)

\(one\  = \ {{0, 0, 0, 0, 0, 0, 0, 0, 0, 
          0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 0, 0, 0, \ 0, 1, 
          1, 0, 0, 0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 0, 0, 0, 
          1, 1, 1, 0, 0, 0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 
          0, \ 0, 1, 1, 1, 1, 0, 0, 
          0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 0, 0, \ 0, 1, 1, 
          1, 0, 0, 0}, 
	\ \ {0, 0, 0, 0, 1, 1, 1, 0, 0, 
          0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 0, 0, 0, 1, 1, 1, 
          0, 0, 0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 0, 0, 0, 1, 
          1, 1, 0, 0, 0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 0, \ 
          0, 0, 1, 1, 1, 0, 0, 
          0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 0, 0, 0, 1, 1, 1, 
          0, 0, 0}\ }\)

\(two\  = \ {{0, 0, 0, 0, 0, 0, 0, 0, 0, 
          0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 0, 0, 1, 1, 1, 1, 
          0, 0, 0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 0, 1, 1, 1, 
          1, 1, 1, 0, 0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 1, \ 
          1, 0, 0, 0, 1, 1, 1, 
          0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 1, 1, 0, 0, 0, 0, 
          1, 1, 0}, 
	\ \ {0, 0, 0, 0, 0, 0, 0, 1, 1, 
          0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 0, 0, 0, 0, 0, 1, 
          1, 0, 0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 0, 0, 0, 1, 
          1, 1, 0, 0, 0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 1, \ 
          1, 1, 1, 1, 1, 1, 1, 
          1}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 1, 1, 1, 1, 1, 1, 
          1, 1, 1}\ }\)

\(three\  = \ {{0, 1, 1, 1, 1, 1, 1, 1, 1, 
          0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 1, 1, 1, 1, 1, 1, 
          1, 0, 0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 0, 0, 0, 1, 
          1, 1, 0, 0, 0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 0, \ 
          0, 0, 1, 1, 0, 0, 0, 
          0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 0, 0, 1, 1, 1, 0, 
          0, 0, 0}, 
	\ \ {0, 0, 0, 0, 1, 1, 1, 1, 1, 
          0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 0, 0, 0, 0, 0, 0, 
          1, 1, 1}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 0, 1, 0, 0, 
          0, 1, 1, 1, 0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 1, \ 
          1, 1, 1, 1, 1, 1, 0, 
          0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 0, 1, 1, 1, 1, 1, 
          0, 0, 0}\ }\)

\(four\  = \ {{0, 0, 0, 0, 0, 1, 1, 0, 0, 
          0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 0, 0, 0, 1, 1, 1, 
          0, 0, 0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 0, 0, 1, 1, 
          1, 1, 0, 0, 0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 0, \ 
          1, 1, 0, 1, 1, 0, 0, 
          0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 1, 1, 0, 0, 1, 1, 
          0, 0, 0}, 
	\ \ {1, 1, 0, 0, 0, 1, 1, 0, 0, 
          0}, 
\ \ \ \ \ \ \ \ \ \ \ {1, 1, 1, 1, 1, 1, 1, 
          1, 1, 1}, 
\ \ \ \ \ \ \ \ \ \ \ {1, 1, 1, 1, 1, 
          1, 1, 1, 1, 1}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 0, \ 
          0, 0, 0, 1, 1, 0, 0, 
          0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 0, 0, 0, 0, 1, 1, 
          0, 0, 0}\ }\)

\(five\  = \ {{1, 1, 1, 1, 1, 1, 1, 1, 1, 
          0}, 
\ \ \ \ \ \ \ \ \ \ \ {1, 1, 1, 1, 1, 1, 1, 
          1, 1, 0}, 
\ \ \ \ \ \ \ \ \ \ \ {1, 1, 0, 0, 0, 
          0, 0, 0, 0, 0}, 
\ \ \ \ \ \ \ \ \ \ \ {1, 1, \ 
          0, 0, 0, 0, 0, 0, 0, 
          0}, 
\ \ \ \ \ \ \ \ \ \ \ {1, 1, 1, 1, 1, 1, 1, 
          1, 0, 0}, 
	\ \ {0, 1, 1, 1, 1, 1, 1, 1, 1, 
          0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 0, 0, 0, 0, 0, 0, 
          1, 1, 1}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 1, 1, 0, 0, 
          0, 0, 1, 1, 0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 1, \ 
          1, 1, 1, 1, 1, 1, 0, 
          0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 0, 1, 1, 1, 1, 1, 
          0, 0, 0}\ }\)

\(six\  = \ {{0, 0, 0, 1, 1, 1, 1, 0, 0, 
          0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 0, 1, 1, 1, 1, 1, 
          1, 0, 0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 1, 1, 0, 0, 
          0, 1, 1, 0, 0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 1, \ 
          1, 0, 0, 0, 1, 1, 0, 
          0}, 
\ \ \ \ \ \ \ \ \ \ \ {1, 1, 0, 0, 0, 0, 0, 
          0, 0, 0}, 
	\ \ {1, 1, 1, 1, 1, 1, 1, 1, 0, 
          0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 1, 1, 1, 1, 1, 1, 
          1, 1, 0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 1, 1, 0, 0, 
          0, 1, 1, 1, 0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 0, 1, 
          1, 1, 1, 1, 1, 0, 0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 
          0, 0, 1, 1, 1, 1, 0, 0, 0}\ }\)

\(seven\  = \ {{0, 0, 0, 0, 0, 0, 0, 0, 0, 
          0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 1, 1, 1, 1, 1, 1, 
          1, 1, 0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 1, 1, 1, 1, 
          1, 1, 1, 1, 0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 0, \ 
          0, 0, 0, 0, 1, 1, 0, 
          0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 0, 0, 0, 0, 0, 1, 
          1, 0, 0}, 
	\ \ {0, 0, 0, 0, 0, 0, 1, 1, 0, 
          0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 0, 0, 0, 0, 0, 1, 
          1, 0, 0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 0, 0, 0, 0, 
          0, 1, 1, 0, 0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 0, \ 
          0, 0, 0, 0, 1, 1, 0, 
          0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 0, 0, 0, 0, 0, 1, 
          1, 0, 0}\ }\)

\(eight = {{0, 0, 0, 0, 1, 1, 0, 0, 0, 
          0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 0, 0, 1, 1, 1, 1, 
          0, 0, 0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 0, 1, 1, 1, 
          1, 1, 1, 0, 0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 1, \ 
          1, 0, 0, 0, 1, 1, 0, 
          0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 1, 1, 0, 0, 0, 0, 
          1, 1, 0}, 
	\ \ {0, 0, 1, 1, 1, 1, 1, 1, 0, 
          0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 1, 1, 0, 0, 0, 0, 
          1, 1, 0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 1, 1, 1, 0, 
          0, 1, 1, 1, 0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 0, \ 
          1, 1, 1, 1, 1, 1, 0, 
          0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 0, 0, 0, 1, 1, 0, 
          0, 0, 0}\ }\)

\(nine\  = \ {{0, 0, 0, 1, 1, 1, 0, 0, 0, 
          0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 0, 0, 1, 1, 1, 1, 
          0, 0, 0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 0, 1, 1, 0, 
          0, 1, 1, 0, 0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 0, \ 
          1, 1, 0, 0, 1, 1, 0, 
          0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 1, 1, 0, 0, 0, 0, 
          1, 1, 0}, 
	\ \ {0, 1, 1, 1, 0, 0, 0, 1, 1, 
          0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 1, 1, 1, 1, 1, 1, 
          1, 1, 0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 0, 1, 1, 1, 
          1, 1, 1, 0, 0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 0, \ 
          0, 0, 0, 0, 1, 1, 0, 
          0}, 
\ \ \ \ \ \ \ \ \ \ \ {0, 0, 0, 0, 0, 0, 1, 
          1, 0, 0}\ }\)

Define a ploting function

plot[s_]  :=  
      Module[{sflp  =  
            s}, 
L  =  
              Length[s];

          For[i  =  1,  
              i  ≤  
                L,  ++i, 
sflp[[i]]  =  
                1 -  s[[L - i  +  
                        1]]
];
ListDensityPlot[sflp]
]

plot[two]

We now construct the W matrix .   (Subscript[W, ij])  < - 
          (Subscript[W, ij])  +  (1/N) (Subscript[x, i]) (Subscript[x, j])

tosign[0]  :=  -1;  tosign[x_]  :=  1  /;  x > 0;

topattern[s_]  :=  
      Module[{}, 
Table[
          s[[i + 10*(j - 1)]],  {j, 10}, {i, 
            10}]
]

wmatrix[
        lett_] := Module[{a}, 
a  =  
          Flatten[lett]; 
a  =  
          Map[tosign,  a]; 
Outer[Times, a, 
          a]
]

W  =  (wmatrix[zero]  +  wmatrix[one]  +  wmatrix[two]  +  
              wmatrix[three]  +  wmatrix[four]  +  wmatrix[five]  +  
              wmatrix[six]  +  wmatrix[seven]  +  wmatrix[eight]  +  
              wmatrix[nine])/10.0;

Let first check if the equation  sgn[ W x ]  =  
      x can be satisfied.

toarray[s_]  :=  Map[tosign,  Flatten[s]]

Sign[ W .  toarray[zero]]  -  toarray[zero]

Wee see for letter three there is a single place which is wrong .   Similarly,  
    there will be few places that is wrong.

Now we implement Hopfield dynamics

hopfield[n_,  W_,  sin_]  :=  
      Module[{s  =  sin}, 
L  =  
          Length[W]; 
Do[
  i  =  
            Ceiling[L * 
                Random[]]; 
   s[[i]]  =  
            Sign[ W[[i]] . 
                s], 
{n}]; 
s
]

We start with input addition some percentage of noise.

sinput  =  
      two  +  Table[ 
          If[Random[]  <  0.1,  1,  0],  {10},  {10}]

sout  =  hopfield[1000,  W, toarray[sinput]]

plot[topattern[sout]]