Text Classification
English
code
FurkanNar commited on
Commit
5b25cac
·
verified ·
1 Parent(s): a424144

Create inference.py

Browse files
Files changed (1) hide show
  1. inference.py +107 -0
inference.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Inference / demo script for Spatial Context Networks (SCN).
3
+ Designed for use as a HuggingFace Space or standalone demo.
4
+
5
+ Usage:
6
+ python inference.py
7
+ python inference.py --checkpoint path/to/model.pt --input_dim 10
8
+ """
9
+
10
+ import argparse
11
+ import torch
12
+ import json
13
+ from spatial_context_networks import SpatialContextNetwork
14
+
15
+
16
+ PATTERN_LABELS = ["Mathematics", "Language", "Vision", "Reasoning"]
17
+
18
+
19
+ def load_model(checkpoint_path: str | None, input_dim: int, n_neurons: int, output_dim: int):
20
+ model = SpatialContextNetwork(
21
+ input_dim=input_dim,
22
+ n_neurons=n_neurons,
23
+ output_dim=output_dim,
24
+ )
25
+ if checkpoint_path:
26
+ state = torch.load(checkpoint_path, map_location="cpu")
27
+ model.load_state_dict(state)
28
+ print(f"Loaded checkpoint: {checkpoint_path}")
29
+ else:
30
+ print("No checkpoint provided — using randomly initialized weights.")
31
+ model.eval()
32
+ return model
33
+
34
+
35
+ def run_inference(model: SpatialContextNetwork, x: torch.Tensor) -> dict:
36
+ """
37
+ Run a single forward pass and return rich diagnostic output.
38
+
39
+ Returns:
40
+ dict with output logits, predicted pattern, network efficiency stats.
41
+ """
42
+ with torch.no_grad():
43
+ output = model(x)
44
+ stats = model.get_network_stats(x)
45
+
46
+ probs = torch.softmax(output, dim=-1)
47
+ predicted_idx = probs.argmax(dim=-1)
48
+
49
+ results = {
50
+ "output_logits": output.tolist(),
51
+ "output_probabilities": probs.tolist(),
52
+ "predicted_pattern": [PATTERN_LABELS[i] for i in predicted_idx.tolist()],
53
+ "mean_active_neurons": round(stats["mean_active_neurons"], 2),
54
+ "network_efficiency": round(stats["network_efficiency"], 4),
55
+ "mean_context_score": round(stats["mean_context_score"], 4),
56
+ }
57
+ return results
58
+
59
+
60
+ def demo(args):
61
+ model = load_model(args.checkpoint, args.input_dim, args.n_neurons, args.output_dim)
62
+
63
+ total_params = sum(p.numel() for p in model.parameters())
64
+ print(f"\n{'='*60}")
65
+ print(" Spatial Context Network — Inference Demo")
66
+ print(f"{'='*60}")
67
+ print(f" Input dim : {args.input_dim}")
68
+ print(f" Hidden neurons: {args.n_neurons}")
69
+ print(f" Output dim : {args.output_dim}")
70
+ print(f" Parameters : {total_params}")
71
+ print(f"{'='*60}\n")
72
+
73
+ torch.manual_seed(42)
74
+ x = torch.randn(args.batch_size, args.input_dim)
75
+ print(f"Running inference on {args.batch_size} random samples...\n")
76
+
77
+ results = run_inference(model, x)
78
+
79
+ for i in range(args.batch_size):
80
+ probs = results["output_probabilities"][i]
81
+ predicted = results["predicted_pattern"][i]
82
+ prob_str = " | ".join(
83
+ f"{label}: {p:.3f}" for label, p in zip(PATTERN_LABELS, probs)
84
+ )
85
+ print(f" Sample {i}: [{prob_str}] → Predicted: {predicted}")
86
+
87
+ print(f"\n Network Stats:")
88
+ print(f" Active neurons : {results['mean_active_neurons']} / {args.n_neurons}")
89
+ print(f" Efficiency : {results['network_efficiency']:.1%}")
90
+ print(f" Context score : {results['mean_context_score']:.4f}")
91
+
92
+ if args.output_json:
93
+ with open(args.output_json, "w") as f:
94
+ json.dump(results, f, indent=2)
95
+ print(f"\n Results saved to {args.output_json}")
96
+
97
+
98
+ if __name__ == "__main__":
99
+ parser = argparse.ArgumentParser(description="SCN Inference Demo")
100
+ parser.add_argument("--checkpoint", type=str, default=None)
101
+ parser.add_argument("--input_dim", type=int, default=10)
102
+ parser.add_argument("--n_neurons", type=int, default=32)
103
+ parser.add_argument("--output_dim", type=int, default=4)
104
+ parser.add_argument("--batch_size", type=int, default=8)
105
+ parser.add_argument("--output_json", type=str, default=None)
106
+ args = parser.parse_args()
107
+ demo(args)