nystrom-ncut 0.3.5__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.5/src/nystrom_ncut.egg-info → nystrom_ncut-0.3.6}/PKG-INFO +1 -1
  2. {nystrom_ncut-0.3.5 → nystrom_ncut-0.3.6}/pyproject.toml +1 -1
  3. {nystrom_ncut-0.3.5 → nystrom_ncut-0.3.6}/src/nystrom_ncut/kernel/kernel_ncut.py +39 -25
  4. {nystrom_ncut-0.3.5 → nystrom_ncut-0.3.6}/src/nystrom_ncut/sampling_utils.py +4 -0
  5. {nystrom_ncut-0.3.5 → nystrom_ncut-0.3.6/src/nystrom_ncut.egg-info}/PKG-INFO +1 -1
  6. {nystrom_ncut-0.3.5 → nystrom_ncut-0.3.6}/LICENSE +0 -0
  7. {nystrom_ncut-0.3.5 → nystrom_ncut-0.3.6}/MANIFEST.in +0 -0
  8. {nystrom_ncut-0.3.5 → nystrom_ncut-0.3.6}/README.md +0 -0
  9. {nystrom_ncut-0.3.5 → nystrom_ncut-0.3.6}/requirements.txt +0 -0
  10. {nystrom_ncut-0.3.5 → nystrom_ncut-0.3.6}/setup.cfg +0 -0
  11. {nystrom_ncut-0.3.5 → nystrom_ncut-0.3.6}/src/__init__.py +0 -0
  12. {nystrom_ncut-0.3.5 → nystrom_ncut-0.3.6}/src/nystrom_ncut/__init__.py +0 -0
  13. {nystrom_ncut-0.3.5 → nystrom_ncut-0.3.6}/src/nystrom_ncut/common.py +0 -0
  14. {nystrom_ncut-0.3.5 → nystrom_ncut-0.3.6}/src/nystrom_ncut/distance_utils.py +0 -0
  15. {nystrom_ncut-0.3.5 → nystrom_ncut-0.3.6}/src/nystrom_ncut/global_settings.py +0 -0
  16. {nystrom_ncut-0.3.5 → nystrom_ncut-0.3.6}/src/nystrom_ncut/kernel/__init__.py +0 -0
  17. {nystrom_ncut-0.3.5 → nystrom_ncut-0.3.6}/src/nystrom_ncut/nystrom/__init__.py +0 -0
  18. {nystrom_ncut-0.3.5 → nystrom_ncut-0.3.6}/src/nystrom_ncut/nystrom/distance_realization.py +0 -0
  19. {nystrom_ncut-0.3.5 → nystrom_ncut-0.3.6}/src/nystrom_ncut/nystrom/normalized_cut.py +0 -0
  20. {nystrom_ncut-0.3.5 → nystrom_ncut-0.3.6}/src/nystrom_ncut/nystrom/nystrom_utils.py +0 -0
  21. {nystrom_ncut-0.3.5 → nystrom_ncut-0.3.6}/src/nystrom_ncut/transformer/__init__.py +0 -0
  22. {nystrom_ncut-0.3.5 → nystrom_ncut-0.3.6}/src/nystrom_ncut/transformer/axis_align.py +0 -0
  23. {nystrom_ncut-0.3.5 → nystrom_ncut-0.3.6}/src/nystrom_ncut/transformer/transformer_mixin.py +0 -0
  24. {nystrom_ncut-0.3.5 → nystrom_ncut-0.3.6}/src/nystrom_ncut/visualize_utils.py +0 -0
  25. {nystrom_ncut-0.3.5 → nystrom_ncut-0.3.6}/src/nystrom_ncut.egg-info/SOURCES.txt +0 -0
  26. {nystrom_ncut-0.3.5 → nystrom_ncut-0.3.6}/src/nystrom_ncut.egg-info/dependency_links.txt +0 -0
  27. {nystrom_ncut-0.3.5 → nystrom_ncut-0.3.6}/src/nystrom_ncut.egg-info/top_level.txt +0 -0
  28. {nystrom_ncut-0.3.5 → 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.5
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.5"
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,
@@ -29,8 +34,8 @@ class KernelNCutBaseTransformer(OnlineTorchTransformerMixin):
29
34
 
30
35
  # Anchor matrices
31
36
  self.anchor_count: int = None # n
32
- self.W: torch.Tensor = None # [... x d x kernel_dim]
33
37
  self.kernelized_anchor: torch.Tensor = None # [... x n x (2 * kernel_dim)]
38
+ self.store: Dict[str, torch.Tensor] = {}
34
39
 
35
40
  # Updated matrices
36
41
  self.total_count: int = None # m
@@ -38,39 +43,51 @@ class KernelNCutBaseTransformer(OnlineTorchTransformerMixin):
38
43
  self.transform_matrix: torch.Tensor = None # [... x (2 * kernel_dim) x n_components]
39
44
  self.eigenvalues_: torch.Tensor = None # [... x n_components]
40
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
+
41
60
  def _update(self) -> None:
42
61
  row_sum = self.kernelized_anchor @ self.r[..., None] # [... x n x 1]
43
62
  normalized_kernelized_anchor = self.kernelized_anchor / (row_sum ** 0.5) # [... x n x (2 * kernel_dim)]
44
- _, S, V = torch.svd_lowrank(torch.nan_to_num(
45
- normalized_kernelized_anchor, nan=0.0,
46
- ), q=self.n_components) # [... x n_components], [... 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]
47
64
  S = S * (self.total_count / self.anchor_count) ** 0.5
48
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]
49
66
  self.eigenvalues_ = S ** 2
50
67
 
51
68
  def fit(self, features: torch.Tensor) -> "KernelNCutBaseTransformer":
52
69
  self.anchor_count = self.total_count = features.shape[-2]
53
- d = features.shape[-1]
54
- scale = get_normalization_factor(features) * (self.affinity_focal_gamma ** 0.5) # [...]
55
- self.W = torch.randn((*features.shape[:-2], d, self.kernel_dim)) / scale[..., None, None] # [... x d x kernel_dim]
56
-
57
- W_anchor = features @ self.W # [... x n x kernel_dim]
58
- self.kernelized_anchor = torch.cat((
59
- torch.cos(W_anchor),
60
- torch.sin(W_anchor),
61
- ), dim=-1) / (self.kernel_dim ** 0.5) # [... x n * (2 * kernel_dim)]
62
- self.r = torch.sum(torch.nan_to_num(self.kernelized_anchor, nan=0.0), dim=-2) # [... x (2 * kernel_dim)]
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)]
63
84
  self._update()
64
85
  return self
65
86
 
66
87
  def update(self, features: torch.Tensor) -> torch.Tensor:
67
88
  self.total_count += features.shape[-2]
68
- W_features = features @ self.W # [... x m x kernel_dim]
69
- kernelized_features = torch.cat((
70
- torch.cos(W_features),
71
- torch.sin(W_features),
72
- ), dim=-1) / (self.kernel_dim ** 0.5) # [... x m x (2 * kernel_dim)]
73
- b_r = torch.sum(torch.nan_to_num(kernelized_features, nan=0.0), dim=-2) # [... x (2 * kernel_dim)]
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)]
74
91
  self.r = self.r + b_r
75
92
  self._update()
76
93
 
@@ -82,11 +99,8 @@ class KernelNCutBaseTransformer(OnlineTorchTransformerMixin):
82
99
  if features is None:
83
100
  kernelized_features = self.kernelized_anchor # [... x n x (2 * kernel_dim)]
84
101
  else:
85
- W_features = features @ self.W
86
- kernelized_features = torch.cat((
87
- torch.cos(W_features),
88
- torch.sin(W_features),
89
- ), 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
+
90
104
  row_sum = kernelized_features @ self.r[..., None] # [... x m x 1]
91
105
  normalized_kernelized_features = kernelized_features / (row_sum ** 0.5) # [... x m x (2 * kernel_dim)]
92
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.5
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