bensemble 0.2.0__tar.gz → 0.2.1__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.
- {bensemble-0.2.0 → bensemble-0.2.1}/PKG-INFO +1 -1
- {bensemble-0.2.0 → bensemble-0.2.1}/bensemble/layers/base.py +34 -0
- {bensemble-0.2.0 → bensemble-0.2.1}/bensemble/utils.py +30 -0
- {bensemble-0.2.0 → bensemble-0.2.1}/bensemble.egg-info/PKG-INFO +1 -1
- {bensemble-0.2.0 → bensemble-0.2.1}/pyproject.toml +1 -1
- {bensemble-0.2.0 → bensemble-0.2.1}/LICENSE +0 -0
- {bensemble-0.2.0 → bensemble-0.2.1}/README.md +0 -0
- {bensemble-0.2.0 → bensemble-0.2.1}/bensemble/__init__.py +0 -0
- {bensemble-0.2.0 → bensemble-0.2.1}/bensemble/calibration/__init__.py +0 -0
- {bensemble-0.2.0 → bensemble-0.2.1}/bensemble/calibration/scaling.py +0 -0
- {bensemble-0.2.0 → bensemble-0.2.1}/bensemble/core/__init__.py +0 -0
- {bensemble-0.2.0 → bensemble-0.2.1}/bensemble/core/base.py +0 -0
- {bensemble-0.2.0 → bensemble-0.2.1}/bensemble/core/ensemble.py +0 -0
- {bensemble-0.2.0 → bensemble-0.2.1}/bensemble/core/member.py +0 -0
- {bensemble-0.2.0 → bensemble-0.2.1}/bensemble/core/types.py +0 -0
- {bensemble-0.2.0 → bensemble-0.2.1}/bensemble/diversity/dropout.py +0 -0
- {bensemble-0.2.0 → bensemble-0.2.1}/bensemble/layers/__init__.py +0 -0
- {bensemble-0.2.0 → bensemble-0.2.1}/bensemble/layers/conv.py +0 -0
- {bensemble-0.2.0 → bensemble-0.2.1}/bensemble/layers/linear.py +0 -0
- {bensemble-0.2.0 → bensemble-0.2.1}/bensemble/losses.py +0 -0
- {bensemble-0.2.0 → bensemble-0.2.1}/bensemble/methods/__init__.py +0 -0
- {bensemble-0.2.0 → bensemble-0.2.1}/bensemble/methods/laplace_approximation.py +0 -0
- {bensemble-0.2.0 → bensemble-0.2.1}/bensemble/methods/probabilistic_backpropagation.py +0 -0
- {bensemble-0.2.0 → bensemble-0.2.1}/bensemble/metrics.py +0 -0
- {bensemble-0.2.0 → bensemble-0.2.1}/bensemble/search/__init__.py +0 -0
- {bensemble-0.2.0 → bensemble-0.2.1}/bensemble/search/bayesian.py +0 -0
- {bensemble-0.2.0 → bensemble-0.2.1}/bensemble/search/nes.py +0 -0
- {bensemble-0.2.0 → bensemble-0.2.1}/bensemble/search/selection.py +0 -0
- {bensemble-0.2.0 → bensemble-0.2.1}/bensemble/search/space.py +0 -0
- {bensemble-0.2.0 → bensemble-0.2.1}/bensemble/uncertainty/__init__.py +0 -0
- {bensemble-0.2.0 → bensemble-0.2.1}/bensemble/uncertainty/decomposition.py +0 -0
- {bensemble-0.2.0 → bensemble-0.2.1}/bensemble.egg-info/SOURCES.txt +0 -0
- {bensemble-0.2.0 → bensemble-0.2.1}/bensemble.egg-info/dependency_links.txt +0 -0
- {bensemble-0.2.0 → bensemble-0.2.1}/bensemble.egg-info/requires.txt +0 -0
- {bensemble-0.2.0 → bensemble-0.2.1}/bensemble.egg-info/top_level.txt +0 -0
- {bensemble-0.2.0 → bensemble-0.2.1}/setup.cfg +0 -0
- {bensemble-0.2.0 → bensemble-0.2.1}/tests/test_losses.py +0 -0
- {bensemble-0.2.0 → bensemble-0.2.1}/tests/test_metrics.py +0 -0
- {bensemble-0.2.0 → bensemble-0.2.1}/tests/test_utils.py +0 -0
|
@@ -52,6 +52,40 @@ class BaseBayesianLayer(nn.Module):
|
|
|
52
52
|
snr_dict = self._get_snr_dict()
|
|
53
53
|
return {name: (val > threshold).float() for name, val in snr_dict.items()}
|
|
54
54
|
|
|
55
|
+
def apply_pruning(self, threshold: float = 0.83) -> float:
|
|
56
|
+
"""
|
|
57
|
+
Applies pruning in-place: zeros out the means and minimizes the variance
|
|
58
|
+
for weights that fall below the SNR threshold.
|
|
59
|
+
|
|
60
|
+
Returns:
|
|
61
|
+
float: Sparsity of the layer (percentage of pruned weights, 0.0 to 1.0).
|
|
62
|
+
"""
|
|
63
|
+
masks = self.get_pruning_masks(threshold)
|
|
64
|
+
|
|
65
|
+
total_weights = 0
|
|
66
|
+
pruned_weights = 0
|
|
67
|
+
|
|
68
|
+
with torch.no_grad():
|
|
69
|
+
for name, param in self.named_parameters():
|
|
70
|
+
if name.endswith("_mu"):
|
|
71
|
+
mask = masks[name]
|
|
72
|
+
|
|
73
|
+
total_weights += mask.numel()
|
|
74
|
+
pruned_weights += (mask == 0.0).sum().item()
|
|
75
|
+
param.data *= mask
|
|
76
|
+
|
|
77
|
+
rho_name = name.replace("_mu", "_rho")
|
|
78
|
+
if hasattr(self, rho_name):
|
|
79
|
+
rho = getattr(self, rho_name)
|
|
80
|
+
rho.data = torch.where(
|
|
81
|
+
mask.bool(),
|
|
82
|
+
rho.data,
|
|
83
|
+
torch.tensor(-1e8, device=rho.device, dtype=rho.dtype),
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
sparsity = pruned_weights / total_weights if total_weights > 0 else 0.0
|
|
87
|
+
return sparsity
|
|
88
|
+
|
|
55
89
|
def _get_snr_dict(self) -> dict[str, torch.Tensor]:
|
|
56
90
|
"""Computes the Signal-to-Noise Ratio (SNR) for all Bayesian parameters.
|
|
57
91
|
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
from bensemble.layers.conv import BayesianConv2d
|
|
2
2
|
from bensemble.layers import BayesianLinear
|
|
3
|
+
from bensemble.layers.base import BaseBayesianLayer
|
|
3
4
|
import math
|
|
4
5
|
|
|
5
6
|
import torch
|
|
@@ -68,3 +69,32 @@ def predict_with_uncertainty(model: nn.Module, x: torch.Tensor, num_samples: int
|
|
|
68
69
|
model.train(was_training)
|
|
69
70
|
|
|
70
71
|
return mean, std
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def prune_model(model: torch.nn.Module, threshold: float = 0.83) -> float:
|
|
75
|
+
"""
|
|
76
|
+
Applies Graves' SNR-based pruning to all Bayesian layers in the model.
|
|
77
|
+
|
|
78
|
+
Args:
|
|
79
|
+
model (nn.Module): The trained Bayesian model.
|
|
80
|
+
threshold (float): SNR threshold. Defaults to 0.83.
|
|
81
|
+
|
|
82
|
+
Returns:
|
|
83
|
+
float: Overall sparsity of the model (percentage of pruned weights).
|
|
84
|
+
"""
|
|
85
|
+
total_weights = 0
|
|
86
|
+
total_pruned = 0
|
|
87
|
+
|
|
88
|
+
for module in model.modules():
|
|
89
|
+
if isinstance(module, BaseBayesianLayer):
|
|
90
|
+
# Считаем, сколько весов в слое всего и сколько мы обрезали
|
|
91
|
+
masks = module.get_pruning_masks(threshold)
|
|
92
|
+
for mask in masks.values():
|
|
93
|
+
total_weights += mask.numel()
|
|
94
|
+
total_pruned += (mask == 0.0).sum().item()
|
|
95
|
+
|
|
96
|
+
# Применяем прунинг к слою
|
|
97
|
+
module.apply_pruning(threshold)
|
|
98
|
+
|
|
99
|
+
overall_sparsity = total_pruned / total_weights if total_weights > 0 else 0.0
|
|
100
|
+
return overall_sparsity
|
|
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
|
|
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
|
|
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
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|