-
Notifications
You must be signed in to change notification settings - Fork 2
/
pattern_clustering.py
178 lines (143 loc) · 6.2 KB
/
pattern_clustering.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch_geometric.data import DataLoader
from sklearn.metrics.pairwise import cosine_similarity
import matplotlib.pyplot as plt
from datasets import traffic_dataset
from utils import *
import argparse
import yaml
import time
from pathlib import Path
from tqdm import tqdm
import sys
sys.path.append('./model')
sys.path.append('./model/TSFormer')
sys.path.append('./model/Meta_Models')
from meta_patch import *
from TSmodel import *
import copy
# from pykeops.torch import LazyTensor
# import faiss
from kmeans_pytorch import kmeans
parser = argparse.ArgumentParser()
parser.add_argument('--data_list', default='chengdu_shenzhen_metr',type=str)
parser.add_argument('--gpu', default=0, type = int)
parser.add_argument('--sim', default='cosine',type = str)
parser.add_argument('--K', default=1000, type = int)
args = parser.parse_args()
args.gpu=0
use_cuda = False
# def KMeans(x, K=10, Niter=10, verbose=True):
# """Implements Lloyd's algorithm for the Euclidean metric."""
# start = time.time()
# N, D = x.shape # Number of samples, dimension of the ambient space
# c = x[:K, :].clone() # Simplistic initialization for the centroids
# x_i = LazyTensor(x.view(N, 1, D)) # (N, 1, D) samples
# c_j = LazyTensor(c.view(1, K, D)) # (1, K, D) centroids
# # x_i = x.view(N, 1, D)
# # c_j = c.view(1, K, D)
# # K-means loop:
# # - x is the (N, D) point cloud,
# # - cl is the (N,) vector of class labels
# # - c is the (K, D) cloud of cluster centroids
# for i in range(Niter):
# print(i)
# # E step: assign points to the closest cluster -------------------------
# D_ij = ((x_i - c_j) ** 2).sum(-1) # (N, K) symbolic squared distances
# cl = D_ij.argmin(dim=1).long().view(-1) # Points -> Nearest cluster
# # M step: update the centroids to the normalized cluster average: ------
# # Compute the sum of points per cluster:
# c.zero_()
# c.scatter_add_(0, cl[:, None].repeat(1, D), x)
# # Divide by the number of points per cluster:
# Ncl = torch.bincount(cl, minlength=K).type_as(c).view(K, 1)
# c /= Ncl # in-place division to compute the average
# del Ncl,D_ij
# if verbose: # Fancy display -----------------------------------------------
# if use_cuda:
# torch.cuda.synchronize()
# end = time.time()
# print(
# f"K-means for the Euclidean metric with {N:,} points in dimension {D:,}, K = {K:,}:"
# )
# print(
# "Timing for {} iterations: {:.5f}s = {} x {:.5f}s\n".format(
# Niter, end - start, Niter, (end - start) / Niter
# )
# )
# return cl, c
# def KMeans_cosine(x, K=10, Niter=10, verbose=True):
# """Implements Lloyd's algorithm for the Cosine similarity metric."""
# start = time.time()
# N, D = x.shape # Number of samples, dimension of the ambient space
# c = x[:K, :].clone() # Simplistic initialization for the centroids
# # Normalize the centroids for the cosine similarity:
# c = torch.nn.functional.normalize(c, dim=1, p=2)
# x_i = LazyTensor(x.view(N, 1, D)) # (N, 1, D) samples
# c_j = LazyTensor(c.view(1, K, D)) # (1, K, D) centroids
# # K-means loop:
# # - x is the (N, D) point cloud,
# # - cl is the (N,) vector of class labels
# # - c is the (K, D) cloud of cluster centroids
# for i in range(Niter):
# # E step: assign points to the closest cluster -------------------------
# S_ij = x_i | c_j # (N, K) symbolic Gram matrix of dot products
# cl = S_ij.argmax(dim=1).long().view(-1) # Points -> Nearest cluster
# # M step: update the centroids to the normalized cluster average: ------
# # Compute the sum of points per cluster:
# c.zero_()
# c.scatter_add_(0, cl[:, None].repeat(1, D), x)
# # Normalize the centroids, in place:
# c[:] = torch.nn.functional.normalize(c, dim=1, p=2)
# if verbose: # Fancy display -----------------------------------------------
# if use_cuda:
# torch.cuda.synchronize()
# end = time.time()
# print(
# f"K-means for the cosine similarity with {N:,} points in dimension {D:,}, K = {K:,}:"
# )
# print(
# "Timing for {} iterations: {:.5f}s = {} x {:.5f}s\n".format(
# Niter, end - start, Niter, (end - start) / Niter
# )
# )
# return cl, c
if __name__ == "__main__":
if torch.cuda.is_available():
args.device = torch.device('cuda:{}'.format(args.gpu))
use_cuda = True
else:
args.device = torch.device('cpu')
# args.device = torch.device('cpu')
print("INFO: {}".format(args.device))
patch_pool = torch.load('./pattern/{}/patch.pt'.format(args.data_list)).to(args.device)
unnorm_patch_pool = torch.load('./pattern/{}/unorm_patch.pt'.format(args.data_list)).to(args.device)
emb_pool = torch.load('./pattern/{}/emb.pt'.format(args.data_list)).to(args.device)
N,D = emb_pool.shape
sample_ratio = 0.1
indices = torch.randperm(N)[:int(N * sample_ratio)]
emb_pool = emb_pool[indices]
print("Use {}. After sampling {}, N = {}".format(args.sim,sample_ratio, emb_pool.shape[0]))
# if(args.sim == 'euclidean'):
# c, cl = KMeans(emb_pool,args.K,50)
# elif(args.sim == 'cosine'):
# c, cl = KMeans_cosine(emb_pool,args.K,50)
# print(c.shape, cl.shape, args.K)
# ncentroids = args.K
# niter = 150
# verbose = True
# d = emb_pool.shape[1]
# kmeans = faiss.Kmeans(d, ncentroids, niter=niter, verbose=verbose, gpu=True)
# res = kmeans.train(emb_pool)
# cl = res.centroids
# c, cl = KMeans(
# X=emb_pool, num_clusters=args.K, distance=args.sim, device=torch.device('cuda:0')
# )
c, cl = kmeans(
X=emb_pool, num_clusters=args.K, distance=args.sim, device=torch.device('cuda:0')
)
torch.save(c,'./pattern/{}/{}_{}_c.pt'.format(args.data_list,args.sim,args.K))
torch.save(cl,'./pattern/{}/{}_{}_cl.pt'.format(args.data_list,args.sim,args.K))