def hopfield_update_sync(s, W, steps=20): states = [s.copy()] for t in range(steps): s_new = np.sign(W @ s) s_new[s_new == 0] = 1 s = s_new states.append(s.copy()) return states
# Wzorce 9x9
T = -np.ones((9,9)) T[0,:] = 1 T[:,4] = 1
L = -np.ones((9,9)) L[:,0] = 1 L[8,:] = 1
p1 = T.flatten() p2 = L.flatten()
# Wagi W = hebbian_weights([p1, p2])
# Zaburzona litera T lub L
np.random.seed(1) litera = 'L' if litera == 'T': noisy_T = p1.copy()+0.5*p2.copy() else: noisy_T = 0.5*p1.copy()+ p2.copy() flip_idx = np.random.choice(len(noisy_T), size=39, replace=False) noisy_T[flip_idx] *= -1
# Symulacja
states = hopfield_update_sync(noisy_T.copy(), W, steps=5)
# Animacja i zapis do GIF
fig, ax = plt.subplots(figsize=(5,5))
def update(frame): ax.clear() show_pattern_ax(states[frame], ax, frame_color="blue") ax.set_title(f"Step {frame}")
ani = FuncAnimation(fig, update, frames=len(states), interval=2000, repeat=False)
# Zamknięcie statycznej figury (żeby nie wyświetlała się w Colabie) plt.close(fig)
# Zapis do GIF from matplotlib.animation import PillowWriter ani.save("hopfield_animation_L.gif", writer=PillowWriter(fps=1))