-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtempCodeRunnerFile.py
More file actions
75 lines (57 loc) · 2.15 KB
/
tempCodeRunnerFile.py
File metadata and controls
75 lines (57 loc) · 2.15 KB
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
# ...existing code...
import numpy as np
A1 = np.array([-1, 1, -1, 1])
A2 = np.array([ 1, 1, 1, -1])
A3 = np.array([-1, -1, -1, 1])
stored_patterns = [A1, A2, A3]
pattern_names = ["A1", "A2", "A3"]
W = np.zeros((4, 4))
for A in stored_patterns:
W += np.outer(A, A)
np.fill_diagonal(W, 0)
print("Weight Matrix (W):")
print(W)
def activation(x):
"""Bipolar step activation function."""
return np.where(x >= 0, 1, -1)
def recall(pattern, W, activation_fn, max_steps=10):
"""
Synchronous iterative recall: apply activation(np.dot(state, W)) until convergence
or max_steps reached. Returns (final_state, steps_taken, converged_bool).
"""
state = pattern.copy()
for step in range(1, max_steps + 1):
new_state = activation_fn(np.dot(state, W))
if np.array_equal(new_state, state):
return new_state, step, True
state = new_state
return state, max_steps, False
def match_stored(state, stored, names=None):
"""Return name/index of matching stored pattern or (None, -1) if no match."""
for i, p in enumerate(stored):
if np.array_equal(state, p):
return (names[i] if names else i, i)
return (None, -1)
# test patterns
Ax = np.array([-1, 1, -1, 1])
Ay = np.array([ 1, 1, 1, 1])
Az = np.array([-1, -1, -1, -1])
test_patterns = [Ax, Ay, Az]
test_names = ["Ax", "Ay", "Az"]
if __name__ == "__main__":
print("\n--- Testing Network Recall (iterative) ---")
for name, pattern in zip(test_names, test_patterns):
print(f"\nTesting with pattern: {name}")
print("Input:")
print(pattern)
final, steps, converged = recall(pattern, W, activation, max_steps=20)
print(f"Output after {steps} step(s):")
print(final)
if converged:
match_name, idx = match_stored(final, stored_patterns, pattern_names)
if match_name is not None:
print(f"Result: Converged to stored pattern {match_name} (index {idx})")
else:
print("Result: Converged to a pattern but not one of the stored patterns")
else:
print("Result: Did not converge within max steps")