-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbasic_usage.py
More file actions
117 lines (96 loc) · 3.57 KB
/
basic_usage.py
File metadata and controls
117 lines (96 loc) · 3.57 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
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
#!/usr/bin/env python3
"""Basic usage example for Living Memory Dynamics (LMD).
This example demonstrates:
1. Creating living memories
2. Setting up the dynamics engine
3. Evolving memories over time
4. Observing emergent behavior
"""
import torch
from lmd import (
LivingMemory,
ValenceTrajectory,
NarrativePhase,
LMDDynamics,
LMDConfig,
)
def main():
# Configuration
content_dim = 256
n_memories = 20
n_steps = 100
print("Living Memory Dynamics - Basic Usage Example")
print("=" * 50)
# 1. Create living memories
print("\n1. Creating living memories...")
memories = []
for i in range(n_memories):
# Random embedding
embedding = torch.randn(content_dim)
embedding = embedding / embedding.norm() # Normalize
# Create valence trajectory (emotional arc)
onset = torch.rand(1).item() * 0.5 # Start emotion
peak = 0.5 + torch.rand(1).item() * 0.5 # Peak emotion
resolution = onset + torch.rand(1).item() * (peak - onset) # End emotion
memory = LivingMemory(
id=i,
content=embedding,
energy=0.5 + torch.rand(1).item(), # Random energy 0.5-1.5
valence=ValenceTrajectory(
points=torch.tensor([onset, peak, resolution])
),
phase=0.0, # Starts at phase 0 (SETUP). Phase is a float radian value.
label=f"memory_{i:03d}",
)
memories.append(memory)
print(f" Created {len(memories)} living memories")
# 2. Setup dynamics engine
print("\n2. Setting up dynamics engine...")
config = LMDConfig(
content_dim=content_dim,
coupling_content_weight=0.5,
narrative_potential_strength=0.4,
noise_scale=0.01,
)
dynamics = LMDDynamics(config)
print(f" Config: coupling_content_weight={config.coupling_content_weight}, noise={config.noise_scale}")
# 3. Evolve memories
print("\n3. Evolving memories...")
dt = 0.01
for step in range(n_steps):
# Step the dynamics
dynamics.step(memories, dt=dt)
# Log every 25 steps
if (step + 1) % 25 == 0:
# Calculate statistics
energies = [m.energy for m in memories]
avg_energy = sum(energies) / len(energies)
phases = {}
for m in memories:
phase_name = m.narrative_phase.name
phases[phase_name] = phases.get(phase_name, 0) + 1
metabolic_states = {}
for m in memories:
state = m.metabolic_state.value
metabolic_states[state] = metabolic_states.get(state, 0) + 1
print(f" Step {step + 1}/{n_steps}:")
print(f" Avg Energy: {avg_energy:.3f}")
print(f" Phases: {phases}")
print(f" Metabolic: {metabolic_states}")
# 4. Final state
print("\n4. Final memory states:")
for m in memories[:5]: # Show first 5
print(f" {m.id}: energy={m.energy:.2f}, phase={m.narrative_phase.name}, "
f"state={m.metabolic_state.value}")
print(f" ... and {len(memories) - 5} more")
# 5. Find coupled memories
print("\n5. Finding strongly coupled memories...")
coupling_field = dynamics.coupling
for i, m1 in enumerate(memories[:5]):
for j, m2 in enumerate(memories[i+1:i+6], start=i+1):
coupling = coupling_field.get_coupling(m1, m2)
if abs(coupling) > 0.1:
print(f" {m1.id} <-> {m2.id}: coupling={coupling:.3f}")
print("\nDone!")
if __name__ == "__main__":
main()