-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsonw_quantum_progressive_network.py
145 lines (126 loc) · 4.55 KB
/
sonw_quantum_progressive_network.py
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
class ProgressiveQuantumNetwork(nn.Module):
"""
Integrates progressive learning from mathematics to quantum mechanics
"""
def __init__(
self,
input_dim: int,
hidden_dim: int = 64,
quantum_dim: int = 32,
wavelength_dim: int = 32,
n_terms: int = 8,
constants: Optional[ProgressiveConstants] = None
):
super().__init__()
self.constants = constants or ProgressiveConstants()
# Progressive layers
self.summation_layer = InfiniteSummationLayer(
input_dim,
hidden_dim,
n_terms,
constants
)
self.gradient_layer = QuantumGradientLayer(
hidden_dim,
quantum_dim,
constants=constants
)
self.wavelength_layer = WavelengthMappingLayer(
quantum_dim,
wavelength_dim,
constants
)
# Integration layer
self.integration_layer = nn.Sequential(
nn.Linear(wavelength_dim, hidden_dim),
nn.LayerNorm(hidden_dim),
nn.GELU(),
nn.Linear(hidden_dim, input_dim)
)
def forward(
self,
x: torch.Tensor,
return_components: bool = False
) -> Dict[str, torch.Tensor]:
# Process through progressive layers
summation = self.summation_layer(x, return_components=True)
gradient = self.gradient_layer(
summation['integrated'],
return_components=True
)
wavelength = self.wavelength_layer(
gradient['quantum_state'],
return_components=True
)
# Final integration
output = self.integration_layer(
wavelength['quantum_wavelength']
)
if return_components:
return {
'output': output,
'summation': summation,
'gradient': gradient,
'wavelength': wavelength
}
return {'output': output}
def train_progressive_network(
network: ProgressiveQuantumNetwork,
data_loader: torch.utils.data.DataLoader,
n_epochs: int = 100,
learning_rate: float = 0.001
) -> Dict[str, List[float]]:
optimizer = torch.optim.Adam(network.parameters(), lr=learning_rate)
history = {
'loss': [],
'summation_coherence': [],
'quantum_coherence': [],
'wavelength_coherence': []
}
for epoch in range(n_epochs):
epoch_loss = 0.0
epoch_summation = 0.0
epoch_quantum = 0.0
epoch_wavelength = 0.0
for batch_x, batch_y in data_loader:
optimizer.zero_grad()
# Forward pass with all components
outputs = network(batch_x, return_components=True)
# Compute main task loss
task_loss = F.mse_loss(outputs['output'], batch_y)
# Compute coherence metrics
summation_coherence = torch.mean(
torch.abs(outputs['summation']['integrated'])
)
quantum_coherence = torch.mean(
torch.abs(outputs['gradient']['quantum_state'])
)
wavelength_coherence = torch.mean(
torch.abs(outputs['wavelength']['quantum_wavelength'])
)
# Combined loss
loss = (
task_loss +
0.1 * (1.0 - summation_coherence) +
0.1 * (1.0 - quantum_coherence) +
0.1 * (1.0 - wavelength_coherence)
)
loss.backward()
optimizer.step()
epoch_loss += loss.item()
epoch_summation += summation_coherence.item()
epoch_quantum += quantum_coherence.item()
epoch_wavelength += wavelength_coherence.item()
# Record metrics
history['loss'].append(epoch_loss)
history['summation_coherence'].append(epoch_summation)
history['quantum_coherence'].append(epoch_quantum)
history['wavelength_coherence'].append(epoch_wavelength)
if (epoch + 1) % 10 == 0:
print(f"Epoch {epoch + 1}/{n_epochs}")
print(f"Loss: {epoch_loss:.4f}")
print(f"Summation Coherence: {epoch_summation:.4f}")
print(f"Quantum Coherence: {epoch_quantum:.4f}")
print(f"Wavelength Coherence: {epoch_wavelength:.4f}")
print("-------------------------")
return history