Spaces:
Sleeping
Sleeping
File size: 7,823 Bytes
dab707a b002b8e c062d6d cee6e5e c062d6d cee6e5e c062d6d cee6e5e c062d6d cee6e5e c062d6d cee6e5e c062d6d cee6e5e c062d6d cee6e5e c062d6d cee6e5e c062d6d cee6e5e c062d6d cee6e5e c062d6d cee6e5e c062d6d cee6e5e c062d6d cee6e5e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | import os
os.system("pip install numpy matplotlib pandas")
import numpy as np
import matplotlib.pyplot as plt
import gradio as gr
import pandas as pd
# --- Your Helper Functions ---
def flatten(img : np.array) -> list[int] :
"""Converts a 2D numpy array into a 1D list."""
new : list[int] = []
for row in img:
for item in row:
new.append(int(item))
return new
def sgn(x):
"""Sign function."""
if x < 0:
return -1
if x == 0:
return 0
return 1
# --- Your Hopfield Class (with one bugfix) ---
class Hopfield:
def __init__(self,patts):
self.E : list[int] = []
self.patts = patts
self.size = (4,4) # Fixed size for reshaping
self.Px :int = len(patts)
self.Py :int = len(patts[0])
# Initialize weights
self.W : np.array = np.zeros((self.Py,self.Py),dtype=np.float16)
def train(self):
"""Trains the network on the patterns provided in __init__."""
for i in range(self.Py):
for j in range(self.Py):
if i == j:
self.W[i][j] = 0
continue
# Hebbian rule
self.W[i][j] = (1 / self.Px) * sum([patt[i] * patt[j] for patt in self.patts])
def Energy(self):
"""Returns the list of energy values recorded during updates."""
return self.E
def update(self,pattern):
"""
Performs one asynchronous update step on the entire pattern.
"""
# Flatten the 2D input pattern to 1D
pattern_flat = flatten(pattern)
# Calculate the new state vector H
H : list[int] = []
for i in self.W:
# H_i = sgn(sum(W_ij * S_j))
H.append(sgn(sum([w * s for w,s in zip(i, pattern_flat)])))
H = np.array(H)
# Calculate the energy of this new state H
E = 0
for i in range(self.Py):
for j in range(self.Py):
E += float(-0.5 * self.W[i][j] * H[i] * H[j])
self.E.append(E)
# --- FIX ---
# Use reshape, not resize. Resize can add/remove elements.
# Reshape will fail if H doesn't have 16 elements, which is safer.
return H.reshape(self.size)
# --- Default Patterns for the Gradio App ---
# Pattern 1: 'X'
patt_1_default = [[ 1, -1, -1, 1],
[-1, 1, 1, -1],
[-1, 1, 1, -1],
[ 1, -1, -1, 1]]
# Pattern 2: 'C'
patt_2_default = [[ 1, 1, 1, -1],
[ 1, -1, -1, -1],
[ 1, -1, -1, -1],
[ 1, 1, 1, -1]]
# Pattern 3: 'L'
patt_3_default = [[ 1, -1, -1, -1],
[ 1, -1, -1, -1],
[ 1, -1, -1, -1],
[ 1, 1, 1, 1]]
# Initial (corrupted) shape to test
initial_shape_default = [[ 1, 1, -1, -1],
[ 1, -1, -1, -1],
[ 1, -1, 1, -1],
[ 1, 1, 1, -1]]
# --- Gradio Core Logic ---
def clean_dataframe(df):
"""Helper to convert Gradio dataframe to a clean NumPy array."""
# Fill any empty cells (None) with -1 and convert to int
return df.fillna(-1).to_numpy(dtype=int)
def run_hopfield(patt1_df, patt2_df, patt3_df, initial_shape_df, steps):
"""
The main function for the Gradio interface.
"""
# 1. Clean inputs
p1 = clean_dataframe(patt1_df)
p2 = clean_dataframe(patt2_df)
p3 = clean_dataframe(patt3_df)
initial_shape = clean_dataframe(initial_shape_df)
# 2. Collect patterns to train (ignore empty/all -1 patterns)
patterns_to_train = []
if np.any(p1 == 1):
patterns_to_train.append(flatten(p1))
if np.any(p2 == 1):
patterns_to_train.append(flatten(p2))
if np.any(p3 == 1):
patterns_to_train.append(flatten(p3))
# 3. Check if any patterns were provided
if not patterns_to_train:
fig_shape = plt.figure()
plt.title("Error: No patterns provided to train.")
plt.axis('off')
fig_energy = plt.figure()
plt.title("Error: No patterns provided to train.")
return fig_shape, fig_energy
# 4. Create and train the model
patts = np.array(patterns_to_train)
model = Hopfield(patts)
model.train()
# 5. Run the evolution
current_shape = initial_shape
for _ in range(int(steps)):
next_shape = model.update(current_shape)
# Check for convergence
if np.array_equal(current_shape, next_shape):
break
current_shape = next_shape
# 6. Generate final shape plot
fig_shape = plt.figure()
plt.imshow(current_shape, cmap='gray', vmin=-1, vmax=1)
plt.title("Final Evolved Shape")
plt.axis('off')
# 7. Generate energy plot
fig_energy = plt.figure()
energy_data = model.Energy()
if energy_data:
plt.plot(list(range(len(energy_data))), energy_data, marker='o')
plt.title("Energy Evolution")
plt.xlabel("Update Step")
plt.ylabel("Energy")
plt.grid(True)
else:
plt.title("Energy (No Updates Run)")
return fig_shape, fig_energy
# --- Gradio Interface ---
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown("# 🧠 Hopfield Network Simulator")
gr.Markdown(
"Define up to 3 patterns (1 for 'on', -1 for 'off'). "
"The network will learn them. Then, draw an 'Initial Shape' "
"and see if the network can evolve it into one of the patterns it learned."
)
with gr.Row():
with gr.Column():
gr.Markdown("### 1. Define Patterns to Memorize")
# Set headers to be invisible, type to pandas for .fillna
patt1_in = gr.Dataframe(
value=patt_1_default,
label="Pattern 1",
headers=None,
datatype="number",
col_count=4,
row_count=4,
type="pandas"
)
patt2_in = gr.Dataframe(
value=patt_2_default,
label="Pattern 2",
headers=None,
datatype="number",
col_count=4,
row_count=4,
type="pandas"
)
patt3_in = gr.Dataframe(
value=patt_3_default,
label="Pattern 3",
headers=None,
datatype="number",
col_count=4,
row_count=4,
type="pandas"
)
with gr.Column():
gr.Markdown("### 2. Set Initial Shape & Run")
initial_in = gr.Dataframe(
value=initial_shape_default,
label="Initial Shape (Test Pattern)",
headers=None,
datatype="number",
col_count=4,
row_count=4,
type="pandas"
)
steps_in = gr.Slider(
minimum=1,
maximum=10,
value=5,
step=1,
label="Max Evolution Steps"
)
run_btn = gr.Button("Run Evolution", variant="primary")
with gr.Row():
with gr.Column():
gr.Markdown("### 3. Results")
shape_out = gr.Plot(label="Final Evolved Shape")
with gr.Column():
gr.Markdown("### 4. Diagnostics")
energy_out = gr.Plot(label="Energy Evolution")
# Connect the button to the function
run_btn.click(
fn=run_hopfield,
inputs=[patt1_in, patt2_in, patt3_in, initial_in, steps_in],
outputs=[shape_out, energy_out]
)
if __name__ == "__main__":
demo.launch() |