Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,63 @@
|
|
1 |
-
import
|
|
|
|
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
from scipy.integrate import solve_ivp
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
|
5 |
+
class SelfAwareNetwork:
|
6 |
+
def __init__(self, num_neurons, learning_rate):
|
7 |
+
self.num_neurons = num_neurons
|
8 |
+
self.learning_rate = learning_rate
|
9 |
+
self.weights = np.random.rand(num_neurons)
|
10 |
+
self.state = np.zeros(num_neurons)
|
11 |
+
|
12 |
+
def activation_function(self, x, t, n):
|
13 |
+
hbar = 1.0545718e-34 # Reduced Planck constant in J路s
|
14 |
+
omega = 1/np.sqrt(137) # Angular frequency related to fine-structure constant
|
15 |
+
term1 = hbar * omega * (n + 0.5)
|
16 |
+
term2 = np.sin(omega * x + np.pi/4) * np.exp(-t)
|
17 |
+
return term1 + term2
|
18 |
+
|
19 |
+
def neuron_dynamics(self, t, y):
|
20 |
+
n = np.arange(self.num_neurons)
|
21 |
+
dydt = -y + self.activation_function(y, t, n)
|
22 |
+
return dydt
|
23 |
+
|
24 |
+
def update_weights(self, state):
|
25 |
+
self.weights += self.learning_rate * state
|
26 |
+
|
27 |
+
def solve_dynamics(self, t_span, y0):
|
28 |
+
sol = solve_ivp(self.neuron_dynamics, t_span, y0, method='RK45', vectorized=False)
|
29 |
+
return sol.t, sol.y
|
30 |
+
|
31 |
+
def evaluate_performance(self, target_state):
|
32 |
+
error = np.linalg.norm(self.state - target_state)
|
33 |
+
return error
|
34 |
+
|
35 |
+
def adjust_learning_rate(self, performance_metric):
|
36 |
+
if performance_metric > 0.1:
|
37 |
+
self.learning_rate *= 0.9
|
38 |
+
else:
|
39 |
+
self.learning_rate *= 1.1
|
40 |
+
|
41 |
+
def self_optimize(self, target_state, t_span, y0):
|
42 |
+
t, y = self.solve_dynamics(t_span, y0)
|
43 |
+
self.state = y[:, -1]
|
44 |
+
performance = self.evaluate_performance(target_state)
|
45 |
+
self.adjust_learning_rate(performance)
|
46 |
+
self.update_weights(self.state)
|
47 |
+
|
48 |
+
def plot_state_evolution(self, t, y):
|
49 |
+
plt.plot(t, y.T)
|
50 |
+
plt.xlabel('Time')
|
51 |
+
plt.ylabel('Neuron States')
|
52 |
+
plt.title('State Evolution of Neurons')
|
53 |
+
plt.show()
|
54 |
+
|
55 |
+
# Example usage
|
56 |
+
network = SelfAwareNetwork(num_neurons=3, learning_rate=0.01) # Reduced the number of neurons
|
57 |
+
t_span = (0, 5) # Shortened the time span
|
58 |
+
y0 = np.random.rand(3) # Adjusted for the reduced number of neurons
|
59 |
+
target_state = np.ones(3)
|
60 |
+
|
61 |
+
network.self_optimize(target_state, t_span, y0)
|
62 |
+
t, y = network.solve_dynamics(t_span, y0)
|
63 |
+
network.plot_state_evolution(t, y)
|