Upload 11 files
Browse files- .gitattributes +3 -0
- app.py +225 -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,225 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# SKA Per-Class Entropy 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 |
+
self.entropy = [None] * len(layer_sizes)
|
| 37 |
+
|
| 38 |
+
self.entropy_history = [[] for _ in range(len(layer_sizes))]
|
| 39 |
+
self.cosine_history = [[] for _ in range(len(layer_sizes))]
|
| 40 |
+
self.output_history = []
|
| 41 |
+
|
| 42 |
+
self.frobenius_history = [[] for _ in range(len(layer_sizes))]
|
| 43 |
+
self.weight_frobenius_history = [[] for _ in range(len(layer_sizes))]
|
| 44 |
+
self.net_history = [[] for _ in range(len(layer_sizes))]
|
| 45 |
+
self.tensor_net_total = [0.0] * len(layer_sizes)
|
| 46 |
+
|
| 47 |
+
def forward(self, x):
|
| 48 |
+
batch_size = x.shape[0]
|
| 49 |
+
x = x.view(batch_size, -1)
|
| 50 |
+
for l in range(len(self.layer_sizes)):
|
| 51 |
+
z = torch.mm(x, self.weights[l]) + self.biases[l]
|
| 52 |
+
frobenius_norm = torch.norm(z, p='fro')
|
| 53 |
+
self.frobenius_history[l].append(frobenius_norm.item())
|
| 54 |
+
d = torch.sigmoid(z)
|
| 55 |
+
self.Z[l] = z
|
| 56 |
+
self.D[l] = d
|
| 57 |
+
x = d
|
| 58 |
+
return x
|
| 59 |
+
|
| 60 |
+
def calculate_entropy(self):
|
| 61 |
+
total_entropy = 0
|
| 62 |
+
for l in range(len(self.layer_sizes)):
|
| 63 |
+
if self.Z[l] is not None and self.D_prev[l] is not None and self.D[l] is not None and self.Z_prev[l] is not None:
|
| 64 |
+
self.delta_D[l] = self.D[l] - self.D_prev[l]
|
| 65 |
+
delta_Z = self.Z[l] - self.Z_prev[l]
|
| 66 |
+
H_lk = (-1 / np.log(2)) * (self.Z[l] * self.delta_D[l])
|
| 67 |
+
layer_entropy = torch.sum(H_lk)
|
| 68 |
+
self.entropy[l] = layer_entropy.item()
|
| 69 |
+
self.entropy_history[l].append(layer_entropy.item())
|
| 70 |
+
|
| 71 |
+
dot_product = torch.sum(self.Z[l] * self.delta_D[l])
|
| 72 |
+
z_norm = torch.norm(self.Z[l])
|
| 73 |
+
delta_d_norm = torch.norm(self.delta_D[l])
|
| 74 |
+
if z_norm > 0 and delta_d_norm > 0:
|
| 75 |
+
cos_theta = dot_product / (z_norm * delta_d_norm)
|
| 76 |
+
self.cosine_history[l].append(cos_theta.item())
|
| 77 |
+
else:
|
| 78 |
+
self.cosine_history[l].append(0.0)
|
| 79 |
+
|
| 80 |
+
total_entropy += layer_entropy
|
| 81 |
+
|
| 82 |
+
D_prime = self.D[l] * (1 - self.D[l])
|
| 83 |
+
nabla_z_H = (1 / np.log(2)) * self.Z[l] * D_prime
|
| 84 |
+
tensor_net_step = torch.sum(delta_Z * (self.D[l] - nabla_z_H))
|
| 85 |
+
self.net_history[l].append(tensor_net_step.item())
|
| 86 |
+
self.tensor_net_total[l] += tensor_net_step.item()
|
| 87 |
+
|
| 88 |
+
return total_entropy
|
| 89 |
+
|
| 90 |
+
def ska_update(self, inputs, learning_rate=0.01):
|
| 91 |
+
for l in range(len(self.layer_sizes)):
|
| 92 |
+
if self.delta_D[l] is not None:
|
| 93 |
+
prev_output = inputs.view(inputs.shape[0], -1) if l == 0 else self.D_prev[l-1]
|
| 94 |
+
d_prime = self.D[l] * (1 - self.D[l])
|
| 95 |
+
gradient = -1 / np.log(2) * (self.Z[l] * d_prime + self.delta_D[l])
|
| 96 |
+
dW = torch.matmul(prev_output.t(), gradient) / prev_output.shape[0]
|
| 97 |
+
self.weights[l] = self.weights[l] - learning_rate * dW
|
| 98 |
+
self.biases[l] = self.biases[l] - learning_rate * gradient.mean(dim=0)
|
| 99 |
+
|
| 100 |
+
def initialize_tensors(self, batch_size):
|
| 101 |
+
for l in range(len(self.layer_sizes)):
|
| 102 |
+
self.Z[l] = None
|
| 103 |
+
self.Z_prev[l] = None
|
| 104 |
+
self.D[l] = None
|
| 105 |
+
self.D_prev[l] = None
|
| 106 |
+
self.delta_D[l] = None
|
| 107 |
+
self.entropy[l] = None
|
| 108 |
+
self.entropy_history[l] = []
|
| 109 |
+
self.cosine_history[l] = []
|
| 110 |
+
self.frobenius_history[l] = []
|
| 111 |
+
self.weight_frobenius_history[l] = []
|
| 112 |
+
self.net_history[l] = []
|
| 113 |
+
self.tensor_net_total[l] = 0.0
|
| 114 |
+
self.output_history = []
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
def get_mnist_per_class(samples_per_class):
|
| 118 |
+
"""Select N samples per class from MNIST, return dict of {digit: images}."""
|
| 119 |
+
targets = mnist_dataset.targets.numpy()
|
| 120 |
+
digit_images = {}
|
| 121 |
+
for digit in range(10):
|
| 122 |
+
indices = np.where(targets == digit)[0][:samples_per_class]
|
| 123 |
+
images_list = []
|
| 124 |
+
for idx in indices:
|
| 125 |
+
img, _ = mnist_dataset[idx]
|
| 126 |
+
images_list.append(img)
|
| 127 |
+
digit_images[digit] = torch.stack(images_list)
|
| 128 |
+
return digit_images
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
def run_ska_per_class(neurons_str, K, tau, samples_per_class):
|
| 132 |
+
# Parse layer sizes
|
| 133 |
+
try:
|
| 134 |
+
layer_sizes = [int(x.strip()) for x in neurons_str.split(",")]
|
| 135 |
+
except ValueError:
|
| 136 |
+
return None
|
| 137 |
+
|
| 138 |
+
K = int(K)
|
| 139 |
+
samples_per_class = int(samples_per_class)
|
| 140 |
+
learning_rate = tau / K
|
| 141 |
+
|
| 142 |
+
# Get data per class
|
| 143 |
+
digit_images = get_mnist_per_class(samples_per_class)
|
| 144 |
+
|
| 145 |
+
# Run SKA separately for each digit
|
| 146 |
+
all_entropy_histories = {}
|
| 147 |
+
|
| 148 |
+
for digit in range(10):
|
| 149 |
+
inputs = digit_images[digit]
|
| 150 |
+
|
| 151 |
+
# Fresh model with same seed for each digit
|
| 152 |
+
torch.manual_seed(42)
|
| 153 |
+
np.random.seed(42)
|
| 154 |
+
model = SKAModel(input_size=784, layer_sizes=layer_sizes, K=K)
|
| 155 |
+
model.initialize_tensors(inputs.size(0))
|
| 156 |
+
|
| 157 |
+
for k in range(K):
|
| 158 |
+
outputs = model.forward(inputs)
|
| 159 |
+
if k > 0:
|
| 160 |
+
model.calculate_entropy()
|
| 161 |
+
model.ska_update(inputs, learning_rate)
|
| 162 |
+
model.D_prev = [d.clone().detach() if d is not None else None for d in model.D]
|
| 163 |
+
model.Z_prev = [z.clone().detach() if z is not None else None for z in model.Z]
|
| 164 |
+
|
| 165 |
+
all_entropy_histories[digit] = [list(model.entropy_history[l]) for l in range(len(layer_sizes))]
|
| 166 |
+
|
| 167 |
+
num_layers = len(layer_sizes)
|
| 168 |
+
colors = plt.cm.tab10(np.linspace(0, 1, 10))
|
| 169 |
+
|
| 170 |
+
# Plot: per-class entropy trajectory per layer
|
| 171 |
+
fig, axes = plt.subplots(num_layers, 1, figsize=(12, 4 * num_layers), sharex=True)
|
| 172 |
+
if num_layers == 1:
|
| 173 |
+
axes = [axes]
|
| 174 |
+
for l in range(num_layers):
|
| 175 |
+
ax = axes[l]
|
| 176 |
+
for digit in range(10):
|
| 177 |
+
h = all_entropy_histories[digit][l]
|
| 178 |
+
ax.plot(h, color=colors[digit], label=f"Digit {digit}")
|
| 179 |
+
ax.set_title(f"Layer {l+1}: Per-Class Entropy Trajectory", fontsize=13)
|
| 180 |
+
ax.set_ylabel("Entropy")
|
| 181 |
+
ax.grid(True)
|
| 182 |
+
ax.legend(ncol=5, fontsize=8)
|
| 183 |
+
axes[-1].set_xlabel("Step Index K")
|
| 184 |
+
fig.tight_layout()
|
| 185 |
+
|
| 186 |
+
return fig
|
| 187 |
+
|
| 188 |
+
|
| 189 |
+
with gr.Blocks(title="SKA Per-Class Entropy Explorer") as demo:
|
| 190 |
+
gr.Image("logo.png", show_label=False, height=100, container=False)
|
| 191 |
+
gr.Markdown("# SKA Per-Class Entropy Explorer")
|
| 192 |
+
gr.Markdown("Runs SKA independently for each digit class and overlays entropy trajectories. Each digit has its own model and weights — the entropy trajectory is a pure fingerprint of that digit's structure.")
|
| 193 |
+
|
| 194 |
+
with gr.Row():
|
| 195 |
+
with gr.Column(scale=1):
|
| 196 |
+
neurons_input = gr.Textbox(label="Layer sizes (comma-separated)", value="256, 128, 64, 10")
|
| 197 |
+
k_slider = gr.Slider(1, 200, value=50, step=1, label="K (forward steps)")
|
| 198 |
+
tau_slider = gr.Slider(0.25, 0.75, value=0.5, step=0.01, label="Learning budget τ (τ = η.K)")
|
| 199 |
+
samples_slider = gr.Slider(1, 100, value=100, step=1, label="Samples per class")
|
| 200 |
+
run_btn = gr.Button("Run SKA Per-Class", variant="primary")
|
| 201 |
+
|
| 202 |
+
gr.Markdown("---")
|
| 203 |
+
gr.Markdown("### Reference Paper")
|
| 204 |
+
gr.HTML('<a href="https://arxiv.org/abs/2503.13942v1" target="_blank">arXiv:2503.13942v1</a>')
|
| 205 |
+
|
| 206 |
+
gr.Markdown("""
|
| 207 |
+
**Abstract**
|
| 208 |
+
|
| 209 |
+
We introduce the Structured Knowledge Accumulation (SKA) framework, which reinterprets entropy as a dynamic, layer-wise measure of knowledge alignment in neural networks. Instead of relying on traditional gradient-based optimization, SKA defines entropy in terms of knowledge vectors and their influence on decision probabilities across multiple layers. This formulation naturally leads to the emergence of activation functions such as the sigmoid as a consequence of entropy minimization. Unlike conventional backpropagation, SKA allows each layer to optimize independently by aligning its knowledge representation with changes in decision probabilities. As a result, total network entropy decreases in a hierarchical manner, allowing knowledge structures to evolve progressively. This approach provides a scalable, biologically plausible alternative to gradient-based learning, bridging information theory and artificial intelligence while offering promising applications in resource-constrained and parallel computing environments.
|
| 210 |
+
""")
|
| 211 |
+
|
| 212 |
+
gr.Markdown("---")
|
| 213 |
+
gr.Markdown("### SKA Eplorer")
|
| 214 |
+
gr.HTML('<a href="https://huggingface.co/spaces/quant-iota/SKA-Explorer" target="_blank">SKA Explorer</a>')
|
| 215 |
+
|
| 216 |
+
with gr.Column(scale=2):
|
| 217 |
+
plot_entropy = gr.Plot(label="Per-Class Entropy Trajectories")
|
| 218 |
+
|
| 219 |
+
run_btn.click(
|
| 220 |
+
fn=run_ska_per_class,
|
| 221 |
+
inputs=[neurons_input, k_slider, tau_slider, samples_slider],
|
| 222 |
+
outputs=[plot_entropy],
|
| 223 |
+
)
|
| 224 |
+
|
| 225 |
+
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
|