Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,59 @@
|
|
| 1 |
import os
|
| 2 |
os.system("pip install numpy matplotlib pandas")
|
| 3 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
os.system("pip install numpy matplotlib pandas")
|
| 3 |
+
import numpy as np
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import pandas as pd
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def flatten(img : np.array) -> list[int] :
|
| 10 |
+
new : list[int] = []
|
| 11 |
+
for row in img:
|
| 12 |
+
for item in row:
|
| 13 |
+
new.append(int(item))
|
| 14 |
+
return new
|
| 15 |
+
|
| 16 |
+
class Hopfield:
|
| 17 |
+
def __init__(self,patts):
|
| 18 |
+
self.E : list[int] = []
|
| 19 |
+
self.patts = patts
|
| 20 |
+
self.size = (4,4)
|
| 21 |
+
self.Px :int = len(patts)
|
| 22 |
+
self.Py :int = len(patts[0])
|
| 23 |
+
|
| 24 |
+
self.W : np.array = np.zeros((self.Py,self.Py),dtype=np.float16)
|
| 25 |
+
|
| 26 |
+
def train(self):
|
| 27 |
+
|
| 28 |
+
for i in range(self.Py):
|
| 29 |
+
for j in range(self.Py):
|
| 30 |
+
if i == j:
|
| 31 |
+
self.W[i][j] = 0
|
| 32 |
+
continue
|
| 33 |
+
|
| 34 |
+
self.W[i][j] = (1 / self.Px) * sum([patt[i] * patt[j] for patt in self.patts])
|
| 35 |
+
|
| 36 |
+
def Energy(self):
|
| 37 |
+
|
| 38 |
+
return self.E
|
| 39 |
+
|
| 40 |
+
def update(self,pattern):
|
| 41 |
+
|
| 42 |
+
pattern_flat = flatten(pattern)
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
H : list[int] = []
|
| 46 |
+
for i in self.W:
|
| 47 |
+
|
| 48 |
+
H.append((sum([w * s for w,s in zip(i, pattern_flat)])))
|
| 49 |
+
|
| 50 |
+
H = np.array(H)
|
| 51 |
+
H = np.sign(H)
|
| 52 |
+
|
| 53 |
+
E = 0
|
| 54 |
+
for i in range(self.Py):
|
| 55 |
+
for j in range(self.Py):
|
| 56 |
+
E += float(-0.5 * self.W[i][j] * H[i] * H[j])
|
| 57 |
+
self.E.append(E)
|
| 58 |
+
|
| 59 |
+
|