📚 First, Let's Get Our Tools Ready¶
Just like how you need crayons and paper to draw, we need some special tools to create our network.
In [1]:
# Import our special tools
import numpy as np # This helps us work with numbers
import matplotlib.pyplot as plt # This helps us make pretty pictures
print("Yay! Our tools are ready! 🎉")
Yay! Our tools are ready! 🎉
🤔 What is a Hopfield Network?¶
Imagine you have two friends who like to match their moods:
- When one is happy, the other tends to be happy too
- When one is sad, the other tends to be sad too
Our network works the same way! We'll have two "neurons" (like tiny brain cells) that can be either:
- Happy (+1) 😊
- Sad (-1) 😢
🔧 Let's Create Our Helper Function¶
This function will help us measure how "comfortable" our network feels in different states.
In [2]:
def calculate_network_energy(weights, states):
"""
This is like a happiness meter for our network!
- Lower energy = more comfortable 😊
- Higher energy = less comfortable 😟
"""
energy = -0.5 * np.sum(np.dot(states, weights) * states)
return energy
🎨 Now Let's Create Our Main Function to Draw the Picture!¶
This function will show us all the possible ways our two neurons can feel and how comfortable the network is with each combination.
In [3]:
def create_energy_landscape():
"""
Let's make a colorful map of our network's favorite states!
Blue means the network is very comfortable, red means it's uncomfortable.
"""
# Step 1: Set up our network
# This tells us how strongly our neurons are connected
weights = np.array([
[0, 1], # First neuron's connections
[1, 0] # Second neuron's connections
])
# Step 2: Define possible moods
possible_states = np.array([-1, 1]) # Sad (-1) or Happy (+1)
# Step 3: Create all possible mood combinations
state1, state2 = np.meshgrid(possible_states, possible_states)
# Step 4: Calculate how comfortable each combination is
energy_map = np.zeros(state1.shape)
for i in range(state1.shape[0]):
for j in range(state1.shape[1]):
current_state = np.array([state1[i, j], state2[i, j]])
energy_map[i, j] = calculate_network_energy(weights, current_state)
# Step 5: Draw our colorful map!
plt.figure(figsize=(12, 8))
# Create the main plot with nice colors
contour = plt.contourf(state1, state2, energy_map, levels=50,
cmap='RdYlBu_r')
# Add a color guide
colorbar = plt.colorbar(contour)
colorbar.set_label('Comfort Level\n(Blue = Very Comfortable)',
rotation=270, labelpad=15)
# Make it pretty!
plt.title('Our Network\'s Favorite States! 🎯',
fontsize=16, pad=20)
plt.xlabel('First Neuron\'s Mood 😊', fontsize=14)
plt.ylabel('Second Neuron\'s Mood 😊', fontsize=14)
# Label the points clearly
plt.xticks(ticks=[-1, 1], labels=['Sad 😢', 'Happy 😊'])
plt.yticks(ticks=[-1, 1], labels=['Sad 😢', 'Happy 😊'])
# Add helpful grid lines
plt.grid(True, linestyle='--', alpha=0.7)
# Add a fun explanation box
explanation = """
📖 What are we looking at?
• BLUE areas = The network is super comfortable!
Both neurons agree with each other.
• RED areas = The network is uncomfortable!
The neurons disagree with each other.
• The network is happiest when both neurons
feel the same way (both happy or both sad)!
"""
plt.text(1.5, 0, explanation,
bbox=dict(facecolor='white', alpha=0.8, edgecolor='gray'),
fontsize=12)
plt.tight_layout()
plt.show()
🚀 Let's Run Our Code and See What Happens!¶
In [4]:
# Time to make our visualization!
create_energy_landscape()
C:\Users\ratchet\AppData\Local\Temp\ipykernel_17396\2194339820.py:69: UserWarning: Glyph 127919 (\N{DIRECT HIT}) missing from font(s) DejaVu Sans.
plt.tight_layout()
C:\Users\ratchet\AppData\Local\Temp\ipykernel_17396\2194339820.py:69: UserWarning: Glyph 128214 (\N{OPEN BOOK}) missing from font(s) DejaVu Sans.
plt.tight_layout()
C:\Users\ratchet\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\IPython\core\pylabtools.py:170: UserWarning: Glyph 127919 (\N{DIRECT HIT}) missing from font(s) DejaVu Sans.
fig.canvas.print_figure(bytes_io, **kw)
C:\Users\ratchet\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\IPython\core\pylabtools.py:170: UserWarning: Glyph 128214 (\N{OPEN BOOK}) missing from font(s) DejaVu Sans.
fig.canvas.print_figure(bytes_io, **kw)
🎯 Understanding What We See¶
Let's break down what our picture shows:
The Blue Areas 🔵
- These show when our network is most comfortable
- This happens when both neurons agree (both happy or both sad)
The Red Areas 🔴
- These show when our network is uncomfortable
- This happens when our neurons disagree (one happy, one sad)
What This Means 💡
- Our network likes harmony! Just like real friends, our neurons prefer to be in the same mood
- This is why the corners where both neurons match (both +1 or both -1) are blue
- The other corners where they disagree are red
🤓 Fun Facts!¶
- This is similar to how real brain cells work together
- Scientists use these networks to study memory and learning
- The blue areas are like memories that the network wants to remember
🎮 Try It Yourself!¶
Here are some fun things you can try:
- What happens if you change the weights in the network?
- Can you predict which corners will be blue before running the code?
- Try counting how many stable states (blue areas) our network has!
🌟 Congratulations!¶
You've just learned about:
- How simple brain-like networks work
- How neurons can work together
- How to visualize network states
Keep exploring and learning! 🚀