l0-python 0.4.3__tar.gz → 0.5.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (23) hide show
  1. {l0_python-0.4.3/l0_python.egg-info → l0_python-0.5.0}/PKG-INFO +1 -1
  2. {l0_python-0.4.3 → l0_python-0.5.0}/l0/__init__.py +1 -1
  3. {l0_python-0.4.3 → l0_python-0.5.0}/l0/calibration.py +50 -2
  4. {l0_python-0.4.3 → l0_python-0.5.0/l0_python.egg-info}/PKG-INFO +1 -1
  5. {l0_python-0.4.3 → l0_python-0.5.0}/pyproject.toml +1 -1
  6. {l0_python-0.4.3 → l0_python-0.5.0}/LICENSE +0 -0
  7. {l0_python-0.4.3 → l0_python-0.5.0}/README.md +0 -0
  8. {l0_python-0.4.3 → l0_python-0.5.0}/l0/distributions.py +0 -0
  9. {l0_python-0.4.3 → l0_python-0.5.0}/l0/gates.py +0 -0
  10. {l0_python-0.4.3 → l0_python-0.5.0}/l0/layers.py +0 -0
  11. {l0_python-0.4.3 → l0_python-0.5.0}/l0/penalties.py +0 -0
  12. {l0_python-0.4.3 → l0_python-0.5.0}/l0/sparse.py +0 -0
  13. {l0_python-0.4.3 → l0_python-0.5.0}/l0_python.egg-info/SOURCES.txt +0 -0
  14. {l0_python-0.4.3 → l0_python-0.5.0}/l0_python.egg-info/dependency_links.txt +0 -0
  15. {l0_python-0.4.3 → l0_python-0.5.0}/l0_python.egg-info/requires.txt +0 -0
  16. {l0_python-0.4.3 → l0_python-0.5.0}/l0_python.egg-info/top_level.txt +0 -0
  17. {l0_python-0.4.3 → l0_python-0.5.0}/setup.cfg +0 -0
  18. {l0_python-0.4.3 → l0_python-0.5.0}/tests/test_calibration.py +0 -0
  19. {l0_python-0.4.3 → l0_python-0.5.0}/tests/test_distributions.py +0 -0
  20. {l0_python-0.4.3 → l0_python-0.5.0}/tests/test_gates.py +0 -0
  21. {l0_python-0.4.3 → l0_python-0.5.0}/tests/test_layers.py +0 -0
  22. {l0_python-0.4.3 → l0_python-0.5.0}/tests/test_penalties.py +0 -0
  23. {l0_python-0.4.3 → l0_python-0.5.0}/tests/test_sparse.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: l0-python
3
- Version: 0.4.3
3
+ Version: 0.5.0
4
4
  Summary: L0 regularization for sparse neural networks and intelligent sampling
5
5
  Author-email: PolicyEngine <hello@policyengine.org>
6
6
  License: MIT License
@@ -5,7 +5,7 @@ A PyTorch implementation of L0 regularization for neural network sparsification
5
5
  and intelligent sampling, based on Louizos, Welling, & Kingma (2017).
6
6
  """
7
7
 
8
- __version__ = "0.1.0"
8
+ __version__ = "0.5.0"
9
9
 
10
10
  from .distributions import HardConcrete
11
11
  from .gates import FeatureGate, HybridGate, L0Gate, SampleGate
@@ -60,6 +60,7 @@ class SparseCalibrationWeights(nn.Module):
60
60
  log_weight_jitter_sd: float = 0.0,
61
61
  log_alpha_jitter_sd: float = 0.01,
62
62
  device: str | torch.device = "cpu",
63
+ use_gates: bool = True,
63
64
  ):
64
65
  super().__init__()
65
66
  self.n_features = n_features
@@ -69,6 +70,7 @@ class SparseCalibrationWeights(nn.Module):
69
70
  self.log_weight_jitter_sd = log_weight_jitter_sd
70
71
  self.log_alpha_jitter_sd = log_alpha_jitter_sd
71
72
  self.device = torch.device(device)
73
+ self.use_gates = use_gates
72
74
 
73
75
  # Initialize weights (on original scale)
74
76
  if init_weights is None:
@@ -193,6 +195,10 @@ class SparseCalibrationWeights(nn.Module):
193
195
  torch.Tensor
194
196
  Positive calibration weights with L0 sparsity applied
195
197
  """
198
+ if not self.use_gates:
199
+ # No gates - just return positive weights
200
+ return torch.exp(self.log_weight)
201
+
196
202
  # Sample or get deterministic gates
197
203
  if deterministic:
198
204
  gates = self.get_deterministic_gates()
@@ -453,6 +459,46 @@ class SparseCalibrationWeights(nn.Module):
453
459
  1 - active_info["count"] / self.n_features
454
460
  )
455
461
 
462
+ # Weight distribution diagnostic
463
+ if active_weights.numel() > 0:
464
+ w_tiny = (active_weights < 0.01).sum().item()
465
+ w_small = (
466
+ ((active_weights >= 0.01) & (active_weights < 0.1))
467
+ .sum()
468
+ .item()
469
+ )
470
+ w_med = (
471
+ ((active_weights >= 0.1) & (active_weights < 1.0))
472
+ .sum()
473
+ .item()
474
+ )
475
+ w_normal = (
476
+ ((active_weights >= 1.0) & (active_weights < 10.0))
477
+ .sum()
478
+ .item()
479
+ )
480
+ w_large = (
481
+ (
482
+ (active_weights >= 10.0)
483
+ & (active_weights < 1000.0)
484
+ )
485
+ .sum()
486
+ .item()
487
+ )
488
+ w_huge = (active_weights >= 1000.0).sum().item()
489
+
490
+ total_active = active_weights.numel()
491
+ weight_dist = (
492
+ f"[<0.01: {100*w_tiny/total_active:.1f}%, "
493
+ f"0.01-0.1: {100*w_small/total_active:.1f}%, "
494
+ f"0.1-1: {100*w_med/total_active:.1f}%, "
495
+ f"1-10: {100*w_normal/total_active:.1f}%, "
496
+ f"10-1000: {100*w_large/total_active:.1f}%, "
497
+ f">1000: {100*w_huge/total_active:.1f}%]"
498
+ )
499
+ else:
500
+ weight_dist = "[no active weights]"
501
+
456
502
  # Calculate components of the actual loss being minimized
457
503
  actual_data_loss = data_loss.item()
458
504
  actual_l0_loss = l0_loss.item()
@@ -464,7 +510,8 @@ class SparseCalibrationWeights(nn.Module):
464
510
  f"mean_group_mare={mean_group_mare:.4%}, "
465
511
  f"max_error={max_rel_err:.1%}, "
466
512
  f"total_loss={actual_total_loss:.3f}, "
467
- f"active={active_info['count']:4d}/{self.n_features} ({sparsity_pct:.1f}% sparse)"
513
+ f"active={active_info['count']:4d}/{self.n_features} ({sparsity_pct:.1f}% sparse)\n"
514
+ f" Weight dist: {weight_dist}"
468
515
  )
469
516
  else:
470
517
  print(
@@ -472,7 +519,8 @@ class SparseCalibrationWeights(nn.Module):
472
519
  f"mean_error={mean_rel_err:.4%}, "
473
520
  f"max_error={max_rel_err:.1%}, "
474
521
  f"total_loss={actual_total_loss:.3f}, "
475
- f"active={active_info['count']:4d}/{self.n_features} ({sparsity_pct:.1f}% sparse)"
522
+ f"active={active_info['count']:4d}/{self.n_features} ({sparsity_pct:.1f}% sparse)\n"
523
+ f" Weight dist: {weight_dist}"
476
524
  )
477
525
 
478
526
  return self
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: l0-python
3
- Version: 0.4.3
3
+ Version: 0.5.0
4
4
  Summary: L0 regularization for sparse neural networks and intelligent sampling
5
5
  Author-email: PolicyEngine <hello@policyengine.org>
6
6
  License: MIT License
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "l0-python"
7
- version = "0.4.3"
7
+ version = "0.5.0"
8
8
  description = "L0 regularization for sparse neural networks and intelligent sampling"
9
9
  readme = "README.md"
10
10
  authors = [
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes