forked from rmunro/pytorch_active_learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpytorch_clusters.py
313 lines (205 loc) · 8.53 KB
/
pytorch_clusters.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/usr/bin/env python
"""Cosine distance kernal for KMeans-type clustering
This is an open source example to accompany Chapter 4 from the book:
"Human-in-the-Loop Machine Learning"
It is a general clustering library.
In this code-base, it supports three Active Learning strategies:
1. Cluster-based sampling
2. Representative sampling
3. Adaptive Representative sampling
"""
import torch
import torch.nn.functional as F
from random import shuffle
class CosineClusters():
"""Represents a set of clusters over a dataset
"""
def __init__(self, num_clusters=100):
self.clusters = [] # clusters for unsupervised and lightly supervised sampling
self.item_cluster = {} # each item's cluster by the id of the item
# Create initial clusters
for i in range(0, num_clusters):
self.clusters.append(Cluster())
def add_random_training_items(self, items):
""" Adds items randomly to clusters
"""
cur_index = 0
for item in items:
self.clusters[cur_index].add_to_cluster(item)
textid = item[0]
self.item_cluster[textid] = self.clusters[cur_index]
cur_index += 1
if cur_index >= len(self.clusters):
cur_index = 0
def add_items_to_best_cluster(self, items):
""" Adds multiple items to best clusters
"""
added = 0
for item in items:
new = self.add_item_to_best_cluster(item)
if new:
added += 1
return added
def get_best_cluster(self, item):
""" Finds the best cluster for this item
returns the cluster and the score
"""
best_cluster = None
best_fit = float("-inf")
for cluster in self.clusters:
fit = cluster.cosine_similary(item)
if fit > best_fit:
best_fit = fit
best_cluster = cluster
return [best_cluster, best_fit]
def add_item_to_best_cluster(self, item):
""" Adds items to best fit cluster
Removes from previous cluster if it existed in one
Returns True if item is new or moved cluster
Returns Fales if the item remains in the same cluster
"""
best_cluster = None
best_fit = float("-inf")
previous_cluster = None
# Remove from current cluster so it isn't contributing to own match
textid = item[0]
if textid in self.item_cluster:
previous_cluster = self.item_cluster[textid]
previous_cluster.remove_from_cluster(item)
for cluster in self.clusters:
fit = cluster.cosine_similary(item)
if fit > best_fit:
best_fit = fit
best_cluster = cluster
best_cluster.add_to_cluster(item)
self.item_cluster[textid] = best_cluster
if best_cluster == previous_cluster:
return False
else:
return True
def get_items_cluster(self, item):
textid = item[0]
if textid in self.item_cluster:
return self.item_cluster[textid]
else:
return None
def get_centroids(self):
centroids = []
for cluster in self.clusters:
centroids.append(cluster.get_centroid())
return centroids
def get_outliers(self):
outliers = []
for cluster in self.clusters:
outliers.append(cluster.get_outlier())
return outliers
def get_randoms(self, number_per_cluster=1, verbose=False):
randoms = []
for cluster in self.clusters:
randoms += cluster.get_random_members(number_per_cluster, verbose)
return randoms
def shape(self):
lengths = []
for cluster in self.clusters:
lengths.append(cluster.size())
return str(lengths)
class Cluster():
"""Represents on cluster for unsupervised or lightly supervised clustering
"""
feature_idx = {} # the index of each feature as class variable to be constant
def __init__(self):
self.members = {} # dict of items by item ids in this cluster
self.feature_vector = [] # feature vector for this cluster
def add_to_cluster(self, item):
textid = item[0]
text = item[1]
self.members[textid] = item
words = text.split()
for word in words:
if word in self.feature_idx:
while len(self.feature_vector) <= self.feature_idx[word]:
self.feature_vector.append(0)
self.feature_vector[self.feature_idx[word]] += 1
else:
# new feature that is not yet in any cluster
self.feature_idx[word] = len(self.feature_vector)
self.feature_vector.append(1)
def remove_from_cluster(self, item):
""" Removes if exists in the cluster
"""
textid = item[0]
text = item[1]
exists = self.members.pop(textid, False)
if exists:
words = text.split()
for word in words:
index = self.feature_idx[word]
if index < len(self.feature_vector):
self.feature_vector[index] -= 1
def cosine_similary(self, item):
text = item[1]
words = text.split()
vector = [0] * len(self.feature_vector)
for word in words:
if word not in self.feature_idx:
self.feature_idx[word] = len(self.feature_vector)
self.feature_vector.append(0)
vector.append(1)
else:
while len(vector) <= self.feature_idx[word]:
vector.append(0)
self.feature_vector.append(0)
vector[self.feature_idx[word]] += 1
item_tensor = torch.FloatTensor(vector)
cluster_tensor = torch.FloatTensor(self.feature_vector)
similarity = F.cosine_similarity(item_tensor, cluster_tensor, 0)
# Alternatively using `F.pairwise_distance()` but normalize the cluster first
return similarity.item() # item() converts tensor value to float
def size(self):
return len(self.members.keys())
def get_centroid(self):
if len(self.members) == 0:
return []
best_item = None
best_fit = float("-inf")
for textid in self.members.keys():
item = self.members[textid]
similarity = self.cosine_similary(item)
if similarity > best_fit:
best_fit = similarity
best_item = item
best_item[3] = "cluster_centroid"
best_item[4] = best_fit
return best_item
def get_outlier(self):
if len(self.members) == 0:
return []
best_item = None
biggest_outlier = float("inf")
for textid in self.members.keys():
item = self.members[textid]
similarity = self.cosine_similary(item)
if similarity < biggest_outlier:
biggest_outlier = similarity
best_item = item
best_item[3] = "cluster_outlier"
best_item[4] = 1 - biggest_outlier
return best_item
def get_random_members(self, number=1, verbose=False):
if len(self.members) == 0:
return []
keys = list(self.members.keys())
shuffle(keys)
randoms = []
for i in range(0, number):
if i < len(keys):
textid = keys[i]
item = self.members[textid]
item[3] = "cluster_member"
item[4] = self.cosine_similary(item)
randoms.append(item)
if verbose:
print("\nRandomly items selected from cluster:")
for item in randoms:
print("\t"+item[1])
return randoms