nystrom-ncut 0.3.5__py3-none-any.whl → 0.3.7__py3-none-any.whl
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.
- nystrom_ncut/kernel/kernel_ncut.py +39 -25
- nystrom_ncut/sampling_utils.py +11 -0
- {nystrom_ncut-0.3.5.dist-info → nystrom_ncut-0.3.7.dist-info}/METADATA +3 -2
- {nystrom_ncut-0.3.5.dist-info → nystrom_ncut-0.3.7.dist-info}/RECORD +7 -7
- {nystrom_ncut-0.3.5.dist-info → nystrom_ncut-0.3.7.dist-info}/WHEEL +1 -1
- {nystrom_ncut-0.3.5.dist-info → nystrom_ncut-0.3.7.dist-info/licenses}/LICENSE +0 -0
- {nystrom_ncut-0.3.5.dist-info → nystrom_ncut-0.3.7.dist-info}/top_level.txt +0 -0
@@ -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
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
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
|
-
|
69
|
-
|
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
|
-
|
86
|
-
|
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]
|
nystrom_ncut/sampling_utils.py
CHANGED
@@ -186,6 +186,13 @@ class OnlineTransformerSubsampleFit(TorchTransformerMixin, OnlineTorchTransforme
|
|
186
186
|
V.scatter_(-2, indices[..., None].expand([-1] * indices.ndim + [V_sampled.shape[-1]]), _V)
|
187
187
|
else:
|
188
188
|
V = V_sampled
|
189
|
+
# from .visualize_utils import extrapolate_knn
|
190
|
+
# V = extrapolate_knn(
|
191
|
+
# anchor_features=self.base_transformer.anchor_features,
|
192
|
+
# anchor_output=V_sampled,
|
193
|
+
# extrapolation_features=features,
|
194
|
+
# affinity_type="rbf",
|
195
|
+
# )
|
189
196
|
return V
|
190
197
|
|
191
198
|
def update(self, features: torch.Tensor) -> torch.Tensor:
|
@@ -193,3 +200,7 @@ class OnlineTransformerSubsampleFit(TorchTransformerMixin, OnlineTorchTransforme
|
|
193
200
|
|
194
201
|
def transform(self, features: torch.Tensor = None, **transform_kwargs) -> torch.Tensor:
|
195
202
|
return self.base_transformer.transform(features)
|
203
|
+
|
204
|
+
@property
|
205
|
+
def eigenvalues_(self) -> torch.Tensor:
|
206
|
+
return getattr(self.base_transformer, "eigenvalues_", None)
|
@@ -1,6 +1,6 @@
|
|
1
|
-
Metadata-Version: 2.
|
1
|
+
Metadata-Version: 2.4
|
2
2
|
Name: nystrom_ncut
|
3
|
-
Version: 0.3.
|
3
|
+
Version: 0.3.7
|
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/
|
@@ -11,6 +11,7 @@ Classifier: Operating System :: OS Independent
|
|
11
11
|
Requires-Python: >=3
|
12
12
|
Description-Content-Type: text/markdown
|
13
13
|
License-File: LICENSE
|
14
|
+
Dynamic: license-file
|
14
15
|
|
15
16
|
|
16
17
|
|
@@ -3,10 +3,10 @@ nystrom_ncut/__init__.py,sha256=4qNyWD5s1Uvd9OpfiMV4mF-3yFCi_K2QVRJIcAOXh70,587
|
|
3
3
|
nystrom_ncut/common.py,sha256=eie19AHTMk6AGTxNnYq1UcFkHJVimeywAUYryXwaiHk,2428
|
4
4
|
nystrom_ncut/distance_utils.py,sha256=zMI651RlIbd6ygvIxRp6jY5Ilfu7j9WQ5FD4I1wmmeo,4198
|
5
5
|
nystrom_ncut/global_settings.py,sha256=TckHuF8geWM2ofd99jBupHD3TQdeEB583_3pdIVRU34,24
|
6
|
-
nystrom_ncut/sampling_utils.py,sha256=
|
6
|
+
nystrom_ncut/sampling_utils.py,sha256=QzafhT9BbYLzAM1OR7OEmKEpYcq-DDHOPHUS-u2jidg,9452
|
7
7
|
nystrom_ncut/visualize_utils.py,sha256=A1qmL8eNZjtvOOlyl9KIeObnpPVUGZVsCB9QBRV7n9I,22762
|
8
8
|
nystrom_ncut/kernel/__init__.py,sha256=pvJ3tFukmlNZqw8VUB_iKPY9gbchUAlNkmnRCZcp0uU,44
|
9
|
-
nystrom_ncut/kernel/kernel_ncut.py,sha256=
|
9
|
+
nystrom_ncut/kernel/kernel_ncut.py,sha256=QoQ-wRBZmdL9WcuzaHr2A3aPN-iuFL3UIUKpqFmX1_k,5809
|
10
10
|
nystrom_ncut/nystrom/__init__.py,sha256=NHse-dW4nTo9wJUEJ6G4_Gw8uAi2vP6Hxm9aeBWxmqc,49
|
11
11
|
nystrom_ncut/nystrom/distance_realization.py,sha256=kvPS-jGUn85MRJx-Dh2IZJ3IwvavRDCbXq6wh_aEBxc,5684
|
12
12
|
nystrom_ncut/nystrom/normalized_cut.py,sha256=BD1F9Wz1BXbTGC-AVwT4IGmsPp334z-7jE9CuwhpNjY,7415
|
@@ -14,8 +14,8 @@ nystrom_ncut/nystrom/nystrom_utils.py,sha256=5cMoF8UFgi_N-nEzbSQqVGhuep_eOn-FzEr
|
|
14
14
|
nystrom_ncut/transformer/__init__.py,sha256=2FJEG9CXavfDDdDk1i9OOGkd8uSOHMkP8LBH49nnPnM,138
|
15
15
|
nystrom_ncut/transformer/axis_align.py,sha256=j3LlAPrp8O_jQAlwZz-gu3D7n_wICEJranye-YK5wvA,4880
|
16
16
|
nystrom_ncut/transformer/transformer_mixin.py,sha256=9wYdWknnJP7jijz70lRAyDn_kpC9aWwYN7Pbt5Mf6gQ,2012
|
17
|
-
nystrom_ncut-0.3.
|
18
|
-
nystrom_ncut-0.3.
|
19
|
-
nystrom_ncut-0.3.
|
20
|
-
nystrom_ncut-0.3.
|
21
|
-
nystrom_ncut-0.3.
|
17
|
+
nystrom_ncut-0.3.7.dist-info/licenses/LICENSE,sha256=2bm9uFabQZ3Ykb_SaSU_uUbAj2-htc6WJQmS_65qD00,1073
|
18
|
+
nystrom_ncut-0.3.7.dist-info/METADATA,sha256=AdVSvMDyC6qB1NeXrpdEjHXtt0iK5k7U024XA5QYmWo,6080
|
19
|
+
nystrom_ncut-0.3.7.dist-info/WHEEL,sha256=CmyFI0kx5cdEMTLiONQRbGQwjIoR1aIYB7eCAQ4KPJ0,91
|
20
|
+
nystrom_ncut-0.3.7.dist-info/top_level.txt,sha256=gM8IWWHYysIRTCvCTcdS4RShOyl9pxpylgSwPUZR2XM,22
|
21
|
+
nystrom_ncut-0.3.7.dist-info/RECORD,,
|
File without changes
|
File without changes
|