Spaces:
Sleeping
Sleeping
Upload 11 files
Browse files- .gitattributes +3 -0
- app.py +222 -0
- data/MNIST/raw/t10k-images-idx3-ubyte +3 -0
- data/MNIST/raw/t10k-images-idx3-ubyte.gz +3 -0
- data/MNIST/raw/t10k-labels-idx1-ubyte +0 -0
- data/MNIST/raw/t10k-labels-idx1-ubyte.gz +3 -0
- data/MNIST/raw/train-images-idx3-ubyte +3 -0
- data/MNIST/raw/train-images-idx3-ubyte.gz +3 -0
- data/MNIST/raw/train-labels-idx1-ubyte +0 -0
- data/MNIST/raw/train-labels-idx1-ubyte.gz +3 -0
- logo.png +3 -0
- requirements.txt +5 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,6 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
data/MNIST/raw/t10k-images-idx3-ubyte filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
data/MNIST/raw/train-images-idx3-ubyte filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
logo.png filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# SKA Tensor Net Explorer - Gradio App
|
| 2 |
+
import torch
|
| 3 |
+
import torch.nn as nn
|
| 4 |
+
import numpy as np
|
| 5 |
+
import matplotlib
|
| 6 |
+
matplotlib.use('Agg')
|
| 7 |
+
import matplotlib.pyplot as plt
|
| 8 |
+
from torchvision import datasets, transforms
|
| 9 |
+
import gradio as gr
|
| 10 |
+
|
| 11 |
+
# Load MNIST from local data
|
| 12 |
+
transform = transforms.Compose([transforms.ToTensor()])
|
| 13 |
+
mnist_dataset = datasets.MNIST(root='./data', train=True, download=False, transform=transform)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
class SKAModel(nn.Module):
|
| 17 |
+
def __init__(self, input_size=784, layer_sizes=[256, 128, 64, 10], K=50):
|
| 18 |
+
super(SKAModel, self).__init__()
|
| 19 |
+
self.input_size = input_size
|
| 20 |
+
self.layer_sizes = layer_sizes
|
| 21 |
+
self.K = K
|
| 22 |
+
|
| 23 |
+
self.weights = nn.ParameterList()
|
| 24 |
+
self.biases = nn.ParameterList()
|
| 25 |
+
prev_size = input_size
|
| 26 |
+
for size in layer_sizes:
|
| 27 |
+
self.weights.append(nn.Parameter(torch.randn(prev_size, size) * 0.01))
|
| 28 |
+
self.biases.append(nn.Parameter(torch.zeros(size)))
|
| 29 |
+
prev_size = size
|
| 30 |
+
|
| 31 |
+
self.Z = [None] * len(layer_sizes)
|
| 32 |
+
self.Z_prev = [None] * len(layer_sizes)
|
| 33 |
+
self.D = [None] * len(layer_sizes)
|
| 34 |
+
self.D_prev = [None] * len(layer_sizes)
|
| 35 |
+
self.delta_D = [None] * len(layer_sizes)
|
| 36 |
+
|
| 37 |
+
self.frobenius_history = [[] for _ in range(len(layer_sizes))]
|
| 38 |
+
self.tensor_net_history = [[] for _ in range(len(layer_sizes))]
|
| 39 |
+
|
| 40 |
+
def forward(self, x):
|
| 41 |
+
batch_size = x.shape[0]
|
| 42 |
+
x = x.view(batch_size, -1)
|
| 43 |
+
for l in range(len(self.layer_sizes)):
|
| 44 |
+
z = torch.mm(x, self.weights[l]) + self.biases[l]
|
| 45 |
+
self.frobenius_history[l].append(torch.norm(z, p='fro').item())
|
| 46 |
+
d = torch.sigmoid(z)
|
| 47 |
+
self.Z[l] = z
|
| 48 |
+
self.D[l] = d
|
| 49 |
+
x = d
|
| 50 |
+
return x
|
| 51 |
+
|
| 52 |
+
def calculate_tensor_net(self):
|
| 53 |
+
for l in range(len(self.layer_sizes)):
|
| 54 |
+
if self.Z[l] is not None and self.Z_prev[l] is not None:
|
| 55 |
+
delta_Z = self.Z[l] - self.Z_prev[l]
|
| 56 |
+
# Tensor Net: Σ (D − ∇_z H) · ΔZ
|
| 57 |
+
D_prime = self.D[l] * (1 - self.D[l])
|
| 58 |
+
nabla_z_H = (1 / np.log(2)) * self.Z[l] * D_prime
|
| 59 |
+
tensor_net_step = torch.sum(delta_Z * (self.D[l] - nabla_z_H))
|
| 60 |
+
self.tensor_net_history[l].append(tensor_net_step.item())
|
| 61 |
+
|
| 62 |
+
def ska_update(self, inputs, learning_rate=0.01):
|
| 63 |
+
for l in range(len(self.layer_sizes)):
|
| 64 |
+
if self.D_prev[l] is not None:
|
| 65 |
+
self.delta_D[l] = self.D[l] - self.D_prev[l]
|
| 66 |
+
prev_output = inputs.view(inputs.shape[0], -1) if l == 0 else self.D_prev[l-1]
|
| 67 |
+
d_prime = self.D[l] * (1 - self.D[l])
|
| 68 |
+
gradient = -1 / np.log(2) * (self.Z[l] * d_prime + self.delta_D[l])
|
| 69 |
+
dW = torch.matmul(prev_output.t(), gradient) / prev_output.shape[0]
|
| 70 |
+
self.weights[l] = self.weights[l] - learning_rate * dW
|
| 71 |
+
self.biases[l] = self.biases[l] - learning_rate * gradient.mean(dim=0)
|
| 72 |
+
|
| 73 |
+
def initialize_tensors(self):
|
| 74 |
+
for l in range(len(self.layer_sizes)):
|
| 75 |
+
self.Z[l] = None
|
| 76 |
+
self.Z_prev[l] = None
|
| 77 |
+
self.D[l] = None
|
| 78 |
+
self.D_prev[l] = None
|
| 79 |
+
self.delta_D[l] = None
|
| 80 |
+
self.frobenius_history[l] = []
|
| 81 |
+
self.tensor_net_history[l] = []
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
def get_mnist_subset(samples_per_class, data_seed=0):
|
| 85 |
+
targets = mnist_dataset.targets.numpy()
|
| 86 |
+
rng = np.random.RandomState(data_seed)
|
| 87 |
+
images_list = []
|
| 88 |
+
for digit in range(10):
|
| 89 |
+
all_indices = np.where(targets == digit)[0]
|
| 90 |
+
rng.shuffle(all_indices)
|
| 91 |
+
for idx in all_indices[:samples_per_class]:
|
| 92 |
+
img, _ = mnist_dataset[idx]
|
| 93 |
+
images_list.append(img)
|
| 94 |
+
return torch.stack(images_list)
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
def run_tensor_net(n1, n2, n3, n4, K, tau, samples_per_class, data_seed):
|
| 98 |
+
layer_sizes = [int(n1), int(n2), int(n3), int(n4)]
|
| 99 |
+
|
| 100 |
+
K = int(K)
|
| 101 |
+
samples_per_class = int(samples_per_class)
|
| 102 |
+
data_seed = int(data_seed)
|
| 103 |
+
learning_rate = tau / K
|
| 104 |
+
|
| 105 |
+
inputs = get_mnist_subset(samples_per_class, data_seed)
|
| 106 |
+
|
| 107 |
+
torch.manual_seed(42)
|
| 108 |
+
np.random.seed(42)
|
| 109 |
+
model = SKAModel(input_size=784, layer_sizes=layer_sizes, K=K)
|
| 110 |
+
model.initialize_tensors()
|
| 111 |
+
|
| 112 |
+
for k in range(K):
|
| 113 |
+
model.forward(inputs)
|
| 114 |
+
if k > 0:
|
| 115 |
+
model.calculate_tensor_net()
|
| 116 |
+
model.ska_update(inputs, learning_rate)
|
| 117 |
+
model.D_prev = [d.clone().detach() if d is not None else None for d in model.D]
|
| 118 |
+
model.Z_prev = [z.clone().detach() if z is not None else None for z in model.Z]
|
| 119 |
+
|
| 120 |
+
num_layers = len(layer_sizes)
|
| 121 |
+
|
| 122 |
+
# Plot: Tensor Net per layer with zero-crossing markers
|
| 123 |
+
fig, ax = plt.subplots(figsize=(8, 5))
|
| 124 |
+
for l in range(num_layers):
|
| 125 |
+
data = model.tensor_net_history[l]
|
| 126 |
+
line, = ax.plot(data, label=f"Layer {l+1}")
|
| 127 |
+
if len(data) > 1:
|
| 128 |
+
arr = np.array(data)
|
| 129 |
+
crossings = np.where(np.diff(np.sign(arr)))[0]
|
| 130 |
+
for c in crossings:
|
| 131 |
+
x_cross = c + arr[c] / (arr[c] - arr[c + 1])
|
| 132 |
+
ax.axvline(x=x_cross, color=line.get_color(), linestyle=':', linewidth=1.2, alpha=0.8)
|
| 133 |
+
ax.axhline(y=0, color='black', linewidth=0.8, linestyle='--')
|
| 134 |
+
ax.set_title("Tensor Net Evolution Across Layers")
|
| 135 |
+
ax.set_xlabel("Step Index K")
|
| 136 |
+
ax.set_ylabel("Tensor Net")
|
| 137 |
+
ax.legend()
|
| 138 |
+
ax.grid(True)
|
| 139 |
+
fig.tight_layout()
|
| 140 |
+
|
| 141 |
+
# Plot 2: Tensor Net vs ||Z||_F scatter per layer
|
| 142 |
+
fig2, axes2 = plt.subplots(2, (num_layers + 1) // 2, figsize=(12, 8))
|
| 143 |
+
axes2_flat = axes2.flatten() if num_layers > 1 else [axes2]
|
| 144 |
+
for l in range(num_layers):
|
| 145 |
+
ax = axes2_flat[l]
|
| 146 |
+
tn = model.tensor_net_history[l]
|
| 147 |
+
frob = model.frobenius_history[l][1:len(tn) + 1]
|
| 148 |
+
min_len = min(len(tn), len(frob))
|
| 149 |
+
if min_len < 2:
|
| 150 |
+
ax.set_title(f"Layer {l+1}: Not enough data")
|
| 151 |
+
continue
|
| 152 |
+
tn_plot = tn[:min_len]
|
| 153 |
+
frob_plot = frob[:min_len]
|
| 154 |
+
sc = ax.scatter(frob_plot, tn_plot, c=range(min_len), cmap='Blues_r', s=50, alpha=0.8)
|
| 155 |
+
ax.plot(frob_plot, tn_plot, 'k-', alpha=0.3)
|
| 156 |
+
plt.colorbar(sc, ax=ax, label='Step')
|
| 157 |
+
ax.axhline(y=0, color='black', linewidth=0.8, linestyle='--')
|
| 158 |
+
ax.set_xlabel('Frobenius Norm of Knowledge Tensor Z')
|
| 159 |
+
ax.set_ylabel('Tensor Net')
|
| 160 |
+
ax.set_title(f'Layer {l+1}: Tensor Net vs. Knowledge Magnitude')
|
| 161 |
+
ax.grid(True, alpha=0.3)
|
| 162 |
+
for l in range(num_layers, len(axes2_flat)):
|
| 163 |
+
axes2_flat[l].set_visible(False)
|
| 164 |
+
fig2.tight_layout()
|
| 165 |
+
|
| 166 |
+
return fig, fig2
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
with gr.Blocks(title="SKA Tensor Net Explorer") as demo:
|
| 170 |
+
gr.Image("logo.png", show_label=False, height=100, container=False)
|
| 171 |
+
gr.Markdown("# SKA Tensor Net Explorer")
|
| 172 |
+
gr.Markdown("Visualize the Tensor Net per layer. The zero-crossing marks the transition from unstructured to structured knowledge accumulation.")
|
| 173 |
+
|
| 174 |
+
with gr.Row():
|
| 175 |
+
with gr.Column(scale=1):
|
| 176 |
+
n1_input = gr.Slider(8, 512, value=256, step=8, label="Layer 1 \u2014 neurons")
|
| 177 |
+
n2_input = gr.Slider(8, 512, value=128, step=8, label="Layer 2 \u2014 neurons")
|
| 178 |
+
n3_input = gr.Slider(8, 256, value=64, step=8, label="Layer 3 \u2014 neurons")
|
| 179 |
+
n4_input = gr.Slider(2, 64, value=10, step=1, label="Layer 4 \u2014 neurons")
|
| 180 |
+
k_slider = gr.Slider(1, 200, value=50, step=1, label="K (forward steps)")
|
| 181 |
+
tau_slider = gr.Slider(0.1, 0.75, value=0.5, step=0.01, label="Learning budget \u03c4 (\u03c4 = \u03b7\u00b7K)")
|
| 182 |
+
samples_slider = gr.Slider(1, 100, value=100, step=1, label="Samples per class")
|
| 183 |
+
seed_slider = gr.Slider(0, 99, value=0, step=1, label="Data seed (shuffle samples)")
|
| 184 |
+
run_btn = gr.Button("Run Tensor Net", variant="primary")
|
| 185 |
+
|
| 186 |
+
gr.Markdown("---")
|
| 187 |
+
gr.Markdown("### Definitions")
|
| 188 |
+
gr.Markdown(
|
| 189 |
+
"| Quantity | Definition |\n|---|---|\n"
|
| 190 |
+
"| **Tensor Net** | \u03a3 (D \u2212 \u2207z H) \u00b7 \u0394Z |\n"
|
| 191 |
+
"| **\u2207z H** | \u2212(1/ln2) \u00b7 z \u00b7 D(1\u2212D) |\n"
|
| 192 |
+
"| **Zero-crossing** | phase transition |"
|
| 193 |
+
)
|
| 194 |
+
|
| 195 |
+
gr.Markdown("---")
|
| 196 |
+
gr.Markdown("### Reference Paper")
|
| 197 |
+
gr.HTML('<a href="https://arxiv.org/abs/2504.03214v1" target="_blank">arXiv:2504.03214v1</a>')
|
| 198 |
+
|
| 199 |
+
gr.Markdown("""
|
| 200 |
+
**Abstract**
|
| 201 |
+
|
| 202 |
+
This paper aims to extend the Structured Knowledge Accumulation (SKA) framework recently proposed by mahi. We introduce two core concepts: the Tensor Net function and the characteristic time property of neural learning. First, we reinterpret the learning rate as a time step in a continuous system. This transforms neural learning from discrete optimization into continuous-time evolution. We show that learning dynamics remain consistent when the product of learning rate and iteration steps stays constant. This reveals a time-invariant behavior and identifies an intrinsic timescale of the network. Second, we define the Tensor Net function as a measure that captures the relationship between decision probabilities, entropy gradients, and knowledge change. Additionally, we define its zero-crossing as the equilibrium state between decision probabilities and entropy gradients. We show that the convergence of entropy and knowledge flow provides a natural stopping condition, replacing arbitrary thresholds with an information-theoretic criterion. We also establish that SKA dynamics satisfy a variational principle based on the Euler-Lagrange equation. These findings extend SKA into a continuous and self-organizing learning model. The framework links computational learning with physical systems that evolve by natural laws. By understanding learning as a time-based process, we open new directions for building efficient, robust, and biologically-inspired AI systems.
|
| 203 |
+
""")
|
| 204 |
+
|
| 205 |
+
gr.Markdown("---")
|
| 206 |
+
gr.Markdown("### SKA Explorer Suite")
|
| 207 |
+
gr.HTML('<a href="https://huggingface.co/quant-iota" target="_blank">\u2b05 All Apps</a>')
|
| 208 |
+
gr.Markdown("---")
|
| 209 |
+
gr.Markdown("### About this App")
|
| 210 |
+
gr.Markdown("The Tensor Net captures the balance between decision probabilities D and entropy gradients \u2207z H, weighted by the knowledge change \u0394Z at each step. When positive, the network is accumulating knowledge in the direction of the entropy gradient. The zero-crossing — marked by dotted vertical lines — signals the onset of structured knowledge accumulation.")
|
| 211 |
+
|
| 212 |
+
with gr.Column(scale=2):
|
| 213 |
+
plot_tensor = gr.Plot(label="Tensor Net per Layer")
|
| 214 |
+
plot_scatter = gr.Plot(label="Tensor Net vs Frobenius Norm")
|
| 215 |
+
|
| 216 |
+
run_btn.click(
|
| 217 |
+
fn=run_tensor_net,
|
| 218 |
+
inputs=[n1_input, n2_input, n3_input, n4_input, k_slider, tau_slider, samples_slider, seed_slider],
|
| 219 |
+
outputs=[plot_tensor, plot_scatter],
|
| 220 |
+
)
|
| 221 |
+
|
| 222 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
|
data/MNIST/raw/t10k-images-idx3-ubyte
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0fa7898d509279e482958e8ce81c8e77db3f2f8254e26661ceb7762c4d494ce7
|
| 3 |
+
size 7840016
|
data/MNIST/raw/t10k-images-idx3-ubyte.gz
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:8d422c7b0a1c1c79245a5bcf07fe86e33eeafee792b84584aec276f5a2dbc4e6
|
| 3 |
+
size 1648877
|
data/MNIST/raw/t10k-labels-idx1-ubyte
ADDED
|
Binary file (10 kB). View file
|
|
|
data/MNIST/raw/t10k-labels-idx1-ubyte.gz
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f7ae60f92e00ec6debd23a6088c31dbd2371eca3ffa0defaefb259924204aec6
|
| 3 |
+
size 4542
|
data/MNIST/raw/train-images-idx3-ubyte
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ba891046e6505d7aadcbbe25680a0738ad16aec93bde7f9b65e87a2fc25776db
|
| 3 |
+
size 47040016
|
data/MNIST/raw/train-images-idx3-ubyte.gz
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:440fcabf73cc546fa21475e81ea370265605f56be210a4024d2ca8f203523609
|
| 3 |
+
size 9912422
|
data/MNIST/raw/train-labels-idx1-ubyte
ADDED
|
Binary file (60 kB). View file
|
|
|
data/MNIST/raw/train-labels-idx1-ubyte.gz
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3552534a0a558bbed6aed32b30c495cca23d567ec52cac8be1a0730e8010255c
|
| 3 |
+
size 28881
|
logo.png
ADDED
|
Git LFS Details
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
torchvision
|
| 3 |
+
matplotlib
|
| 4 |
+
seaborn
|
| 5 |
+
numpy
|