5.3 Hopfield Network
5.3 Hopfield Network
5.3.1 Recovery of Digits
5.3.1 Recovery of Digits
In[]:=
session=StartExternalSession["Python"]
Out[]=
ExternalSessionObject
Test
In[]:=
5+6
Out[]=
11
In[]:=
import numpy as np
from neupy import algorithms
from neupy import algorithms
In[]:=
zero=np.matrix([ 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0 ])
In[]:=
zero
Out[]=
NumericArray
In[]:=
Normal[%]
Out[]=
{{0,1,1,1,0,1,0,0,0,1,1,0,0,0,1,1,0,0,0,1,1,0,0,0,1,0,1,1,1,0}}
In[]:=
zero=%;
In[]:=
Partition[zero//Flatten,5]//MatrixForm
Out[]//MatrixForm=
0 | 1 | 1 | 1 | 0 |
1 | 0 | 0 | 0 | 1 |
1 | 0 | 0 | 0 | 1 |
1 | 0 | 0 | 0 | 1 |
1 | 0 | 0 | 0 | 1 |
0 | 1 | 1 | 1 | 0 |
In[]:=
MatrixPlot[%,MeshTrue]
Out[]=
In[]:=
one = np.matrix([ 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0 ])
In[]:=
one
Out[]=
NumericArray
In[]:=
Normal[%]
Out[]=
{{0,0,1,0,0,0,1,1,0,0,1,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0}}
In[]:=
one=%;
In[]:=
Partition[one//Flatten,5]//MatrixForm
Out[]//MatrixForm=
0 | 0 | 1 | 0 | 0 |
0 | 1 | 1 | 0 | 0 |
1 | 0 | 1 | 0 | 0 |
0 | 0 | 1 | 0 | 0 |
0 | 0 | 1 | 0 | 0 |
0 | 0 | 1 | 0 | 0 |
In[]:=
MatrixPlot[%,MeshTrue]
Out[]=
In[]:=
two = np.matrix([ 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, ])
In[]:=
two
Out[]=
NumericArray
In[]:=
Normal[%]
Out[]=
{{0,1,1,0,0,1,0,0,1,0,0,0,0,1,0,0,1,1,0,0,1,0,0,0,0,1,1,1,1,1}}
In[]:=
two=%;
In[]:=
Partition[two//Flatten,5]//MatrixForm
Out[]//MatrixForm=
0 | 1 | 1 | 0 | 0 |
1 | 0 | 0 | 1 | 0 |
0 | 0 | 0 | 1 | 0 |
0 | 1 | 1 | 0 | 0 |
1 | 0 | 0 | 0 | 0 |
1 | 1 | 1 | 1 | 1 |
In[]:=
MatrixPlot[%,MeshTrue]
In[]:=
data = np.concatenate([zero, one,two], axis=0)
In[]:=
dhnet = algorithms.DiscreteHopfieldNetwork(mode='sync')
dhnet.train(data)
dhnet.train(data)
In[]:=
half_zero
In[]:=
result = dhnet.predict(half_zero)
result
result
In[]:=
half_two
In[]:=
result = dhnet.predict(half_two)
result
result
In[]:=
half_what
In[]:=
result = dhnet.predict(half_what)
result
result
In[]:=
from neupy import environment
environment.reproducible()
environment.reproducible()
In[]:=
dhnet.mode='async'
dhnet.n_times=1000
dhnet.n_times=1000
In[]:=
result = dhnet.predict(half_what)
result
result
5.3.2 Reconstruction of Deteriorated Images
5.3.2 Reconstruction of Deteriorated Images
In[]:=
from numpy import array, matrix
from scipy.io import mmread, mmwrite
from numpy import array, matrix
from scipy.io import mmread, mmwrite
In[]:=
caki1=mmread('cuki1.mtx')
caki11=mmread('cuki11.mtx')
caki11=mmread('cuki11.mtx')
In[]:=
data = caki1
In[]:=
dhnet = algorithms.DiscreteHopfieldNetwork(mode='sync')
dhnet.train(data)
dhnet.train(data)
Reconstructed the deteriorated images, see Figs. 5.3.15 - 5.3.17
In[]:=
t=dhnet.predict(caki11)
mmwrite('cico.mtx',t)
mmwrite('cico.mtx',t)
In[]:=
hopD = HopfieldFit[{H1b}]