nystrom-ncut 0.3.4__py3-none-any.whl → 0.3.6__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 +45 -26
- nystrom_ncut/sampling_utils.py +4 -0
- {nystrom_ncut-0.3.4.dist-info → nystrom_ncut-0.3.6.dist-info}/METADATA +1 -1
- {nystrom_ncut-0.3.4.dist-info → nystrom_ncut-0.3.6.dist-info}/RECORD +7 -7
- {nystrom_ncut-0.3.4.dist-info → nystrom_ncut-0.3.6.dist-info}/WHEEL +1 -1
- {nystrom_ncut-0.3.4.dist-info → nystrom_ncut-0.3.6.dist-info}/LICENSE +0 -0
- {nystrom_ncut-0.3.4.dist-info → nystrom_ncut-0.3.6.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,
|
@@ -28,44 +33,61 @@ class KernelNCutBaseTransformer(OnlineTorchTransformerMixin):
|
|
28
33
|
self.affinity_focal_gamma = affinity_focal_gamma
|
29
34
|
|
30
35
|
# Anchor matrices
|
31
|
-
self.
|
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
|
-
|
44
|
-
|
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
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
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
|
-
|
64
|
-
kernelized_features =
|
65
|
-
|
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
|
-
|
81
|
-
|
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]
|
nystrom_ncut/sampling_utils.py
CHANGED
@@ -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.
|
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/
|
@@ -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=Njfge6E0DzwWgS0hk1Os3VEhw896Z4eLWKhX_FTQado,9164
|
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.6.dist-info/LICENSE,sha256=2bm9uFabQZ3Ykb_SaSU_uUbAj2-htc6WJQmS_65qD00,1073
|
18
|
+
nystrom_ncut-0.3.6.dist-info/METADATA,sha256=T-Skh2IsSTV7xwJ7QO9FFTqe0yatgv_kw-7StlQj568,6058
|
19
|
+
nystrom_ncut-0.3.6.dist-info/WHEEL,sha256=beeZ86-EfXScwlR_HKu4SllMC9wUEj_8Z_4FJ3egI2w,91
|
20
|
+
nystrom_ncut-0.3.6.dist-info/top_level.txt,sha256=gM8IWWHYysIRTCvCTcdS4RShOyl9pxpylgSwPUZR2XM,22
|
21
|
+
nystrom_ncut-0.3.6.dist-info/RECORD,,
|
File without changes
|
File without changes
|