nystrom-ncut 0.3.4__tar.gz → 0.3.6__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 (28) hide show
  1. {nystrom_ncut-0.3.4/src/nystrom_ncut.egg-info → nystrom_ncut-0.3.6}/PKG-INFO +1 -1
  2. {nystrom_ncut-0.3.4 → nystrom_ncut-0.3.6}/pyproject.toml +1 -1
  3. {nystrom_ncut-0.3.4 → nystrom_ncut-0.3.6}/src/nystrom_ncut/kernel/kernel_ncut.py +45 -26
  4. {nystrom_ncut-0.3.4 → nystrom_ncut-0.3.6}/src/nystrom_ncut/sampling_utils.py +4 -0
  5. {nystrom_ncut-0.3.4 → nystrom_ncut-0.3.6/src/nystrom_ncut.egg-info}/PKG-INFO +1 -1
  6. {nystrom_ncut-0.3.4 → nystrom_ncut-0.3.6}/LICENSE +0 -0
  7. {nystrom_ncut-0.3.4 → nystrom_ncut-0.3.6}/MANIFEST.in +0 -0
  8. {nystrom_ncut-0.3.4 → nystrom_ncut-0.3.6}/README.md +0 -0
  9. {nystrom_ncut-0.3.4 → nystrom_ncut-0.3.6}/requirements.txt +0 -0
  10. {nystrom_ncut-0.3.4 → nystrom_ncut-0.3.6}/setup.cfg +0 -0
  11. {nystrom_ncut-0.3.4 → nystrom_ncut-0.3.6}/src/__init__.py +0 -0
  12. {nystrom_ncut-0.3.4 → nystrom_ncut-0.3.6}/src/nystrom_ncut/__init__.py +0 -0
  13. {nystrom_ncut-0.3.4 → nystrom_ncut-0.3.6}/src/nystrom_ncut/common.py +0 -0
  14. {nystrom_ncut-0.3.4 → nystrom_ncut-0.3.6}/src/nystrom_ncut/distance_utils.py +0 -0
  15. {nystrom_ncut-0.3.4 → nystrom_ncut-0.3.6}/src/nystrom_ncut/global_settings.py +0 -0
  16. {nystrom_ncut-0.3.4 → nystrom_ncut-0.3.6}/src/nystrom_ncut/kernel/__init__.py +0 -0
  17. {nystrom_ncut-0.3.4 → nystrom_ncut-0.3.6}/src/nystrom_ncut/nystrom/__init__.py +0 -0
  18. {nystrom_ncut-0.3.4 → nystrom_ncut-0.3.6}/src/nystrom_ncut/nystrom/distance_realization.py +0 -0
  19. {nystrom_ncut-0.3.4 → nystrom_ncut-0.3.6}/src/nystrom_ncut/nystrom/normalized_cut.py +0 -0
  20. {nystrom_ncut-0.3.4 → nystrom_ncut-0.3.6}/src/nystrom_ncut/nystrom/nystrom_utils.py +0 -0
  21. {nystrom_ncut-0.3.4 → nystrom_ncut-0.3.6}/src/nystrom_ncut/transformer/__init__.py +0 -0
  22. {nystrom_ncut-0.3.4 → nystrom_ncut-0.3.6}/src/nystrom_ncut/transformer/axis_align.py +0 -0
  23. {nystrom_ncut-0.3.4 → nystrom_ncut-0.3.6}/src/nystrom_ncut/transformer/transformer_mixin.py +0 -0
  24. {nystrom_ncut-0.3.4 → nystrom_ncut-0.3.6}/src/nystrom_ncut/visualize_utils.py +0 -0
  25. {nystrom_ncut-0.3.4 → nystrom_ncut-0.3.6}/src/nystrom_ncut.egg-info/SOURCES.txt +0 -0
  26. {nystrom_ncut-0.3.4 → nystrom_ncut-0.3.6}/src/nystrom_ncut.egg-info/dependency_links.txt +0 -0
  27. {nystrom_ncut-0.3.4 → nystrom_ncut-0.3.6}/src/nystrom_ncut.egg-info/top_level.txt +0 -0
  28. {nystrom_ncut-0.3.4 → nystrom_ncut-0.3.6}/tests/test.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: nystrom_ncut
3
- Version: 0.3.4
3
+ Version: 0.3.6
4
4
  Summary: Normalized Cut and Nyström Approximation
5
5
  Author-email: Huzheng Yang <huze.yann@gmail.com>, Wentinn Liao <wentinn.liao@gmail.com>
6
6
  Project-URL: Documentation, https://github.com/JophiArcana/Nystrom-NCUT/
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "nystrom_ncut"
7
- version = "0.3.4"
7
+ version = "0.3.6"
8
8
  authors = [
9
9
  { name = "Huzheng Yang", email = "huze.yann@gmail.com" },
10
10
  { name = "Wentinn Liao", email = "wentinn.liao@gmail.com" },
@@ -1,5 +1,10 @@
1
+ from typing import Dict
2
+
1
3
  import torch
2
4
 
5
+ from ..common import (
6
+ lazy_normalize,
7
+ )
3
8
  from ..distance_utils import (
4
9
  AffinityOptions,
5
10
  AFFINITY_TO_DISTANCE,
@@ -28,44 +33,61 @@ class KernelNCutBaseTransformer(OnlineTorchTransformerMixin):
28
33
  self.affinity_focal_gamma = affinity_focal_gamma
29
34
 
30
35
  # Anchor matrices
31
- self.W: torch.Tensor = None # [... x d x kernel_dim]
36
+ self.anchor_count: int = None # n
32
37
  self.kernelized_anchor: torch.Tensor = None # [... x n x (2 * kernel_dim)]
38
+ self.store: Dict[str, torch.Tensor] = {}
33
39
 
34
40
  # Updated matrices
41
+ self.total_count: int = None # m
35
42
  self.r: torch.Tensor = None # [... x (2 * kernel_dim)]
36
43
  self.transform_matrix: torch.Tensor = None # [... x (2 * kernel_dim) x n_components]
37
44
  self.eigenvalues_: torch.Tensor = None # [... x n_components]
38
45
 
46
+ def _kernelize_features(self, features: torch.Tensor) -> torch.Tensor:
47
+ match self.affinity_type:
48
+ case "cosine" | "rbf":
49
+ if self.affinity_type == "cosine":
50
+ features = lazy_normalize(features)
51
+ W_features = features @ self.store["W"] # [... x m x kernel_dim]
52
+ return torch.cat((
53
+ torch.cos(W_features),
54
+ torch.sin(W_features),
55
+ ), dim=-1) / (self.kernel_dim ** 0.5) # [... x m x (2 * kernel_dim)]
56
+
57
+ case _:
58
+ raise ValueError(self.affinity_type)
59
+
39
60
  def _update(self) -> None:
40
61
  row_sum = self.kernelized_anchor @ self.r[..., None] # [... x n x 1]
41
62
  normalized_kernelized_anchor = self.kernelized_anchor / (row_sum ** 0.5) # [... x n x (2 * kernel_dim)]
42
- _, S, V = torch.svd_lowrank(torch.nan_to_num(
43
- normalized_kernelized_anchor, nan=0.0,
44
- ), q=self.n_components) # [... x n_components], [... x (2 * kernel_dim) x n_components]
45
- self.transform_matrix = V * torch.nan_to_num(1 / S, posinf=0.0, neginf=0.0)[..., None, :] # [... x (2 * kernel_dim) x n_components]
63
+ _, S, V = torch.svd_lowrank(torch.nan_to_num(normalized_kernelized_anchor, nan=0.0), q=self.n_components) # [... x n_components], [... x (2 * kernel_dim) x n_components]
64
+ S = S * (self.total_count / self.anchor_count) ** 0.5
65
+ self.transform_matrix = V * torch.nan_to_num(1 / S, posinf=0.0, neginf=0.0)[..., None, :] # [... x (2 * kernel_dim) x n_components]
46
66
  self.eigenvalues_ = S ** 2
47
67
 
48
68
  def fit(self, features: torch.Tensor) -> "KernelNCutBaseTransformer":
49
- d = features.shape[-1]
50
- scale = get_normalization_factor(features) * (self.affinity_focal_gamma ** 0.5) # [...]
51
- self.W = torch.randn((*features.shape[:-2], d, self.kernel_dim)) / scale[..., None, None] # [... x d x kernel_dim]
52
-
53
- W_anchor = features @ self.W # [... x n x kernel_dim]
54
- self.kernelized_anchor = torch.cat((
55
- torch.cos(W_anchor),
56
- torch.sin(W_anchor),
57
- ), dim=-1) / (self.kernel_dim ** 0.5) # [... x n * (2 * kernel_dim)]
58
- self.r = torch.sum(torch.nan_to_num(self.kernelized_anchor, nan=0.0), dim=-2) # [... x (2 * kernel_dim)]
69
+ self.anchor_count = self.total_count = features.shape[-2]
70
+ shape, d = features.shape[:-2], features.shape[-1]
71
+
72
+ match self.affinity_type:
73
+ case "cosine" | "rbf":
74
+ scale = self.affinity_focal_gamma ** 0.5
75
+ if self.affinity_type == "rbf":
76
+ scale = get_normalization_factor(features)[..., None, None] * scale # [... x 1 x 1]
77
+ self.store["W"] = torch.randn((*shape, d, self.kernel_dim), device=features.device) / scale # [... x d x kernel_dim]
78
+
79
+ case _:
80
+ raise ValueError(self.affinity_type)
81
+
82
+ self.kernelized_anchor = self._kernelize_features(features) # [... x n * (2 * kernel_dim)]
83
+ self.r = torch.sum(torch.nan_to_num(self.kernelized_anchor, nan=0.0), dim=-2) # [... x (2 * kernel_dim)]
59
84
  self._update()
60
85
  return self
61
86
 
62
87
  def update(self, features: torch.Tensor) -> torch.Tensor:
63
- W_features = features @ self.W # [... x m x kernel_dim]
64
- kernelized_features = torch.cat((
65
- torch.cos(W_features),
66
- torch.sin(W_features),
67
- ), dim=-1) / (self.kernel_dim ** 0.5) # [... x m x (2 * kernel_dim)]
68
- b_r = torch.sum(torch.nan_to_num(kernelized_features, nan=0.0), dim=-2) # [... x (2 * kernel_dim)]
88
+ self.total_count += features.shape[-2]
89
+ kernelized_features = self._kernelize_features(features) # [... x m x (2 * kernel_dim)]
90
+ b_r = torch.sum(torch.nan_to_num(kernelized_features, nan=0.0), dim=-2) # [... x (2 * kernel_dim)]
69
91
  self.r = self.r + b_r
70
92
  self._update()
71
93
 
@@ -77,11 +99,8 @@ class KernelNCutBaseTransformer(OnlineTorchTransformerMixin):
77
99
  if features is None:
78
100
  kernelized_features = self.kernelized_anchor # [... x n x (2 * kernel_dim)]
79
101
  else:
80
- W_features = features @ self.W
81
- kernelized_features = torch.cat((
82
- torch.cos(W_features),
83
- torch.sin(W_features),
84
- ), dim=-1) / (self.kernel_dim ** 0.5) # [... x m x (2 * kernel_dim)]
102
+ kernelized_features = self._kernelize_features(features) # [... x m x (2 * kernel_dim)]
103
+
85
104
  row_sum = kernelized_features @ self.r[..., None] # [... x m x 1]
86
105
  normalized_kernelized_features = kernelized_features / (row_sum ** 0.5) # [... x m x (2 * kernel_dim)]
87
106
  return normalized_kernelized_features @ self.transform_matrix # [... x m x n_components]
@@ -193,3 +193,7 @@ class OnlineTransformerSubsampleFit(TorchTransformerMixin, OnlineTorchTransforme
193
193
 
194
194
  def transform(self, features: torch.Tensor = None, **transform_kwargs) -> torch.Tensor:
195
195
  return self.base_transformer.transform(features)
196
+
197
+ @property
198
+ def eigenvalues_(self) -> torch.Tensor:
199
+ return getattr(self.base_transformer, "eigenvalues_", None)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: nystrom_ncut
3
- Version: 0.3.4
3
+ Version: 0.3.6
4
4
  Summary: Normalized Cut and Nyström Approximation
5
5
  Author-email: Huzheng Yang <huze.yann@gmail.com>, Wentinn Liao <wentinn.liao@gmail.com>
6
6
  Project-URL: Documentation, https://github.com/JophiArcana/Nystrom-NCUT/
File without changes
File without changes
File without changes
File without changes
File without changes