我试图估计一个约300000张图像的数据集的特征之间的成对距离,以计算它们的最小值。但是,计算的距离矩阵太大,无法放入内存,导致OOM错误
在上下文中,我试图实现kcenter_贪婪方法来进行主动学习,如这里(https://github.com/google/active-learning/blob/master/sampling_methods/kcenter_greedy.py)
人们评估过的大多数学术数据集都很小,因此代码运行良好。在我的例子中,我希望使用一个更大的数据集,对于这个数据集,sklearn.metrics.pairwise_distances
函数没有那么有用
下面是代码的相关部分
def update_distances(self, cluster_centers, only_new=True, reset_dist=False):
"""Update min distances given cluster centers.
Args:
cluster_centers: indices of cluster centers
only_new: only calculate distance for newly selected points and update
min_distances.
rest_dist: whether to reset min_distances.
"""
if reset_dist:
self.min_distances = None
if only_new:
cluster_centers = [d for d in cluster_centers
if d not in self.already_selected]
if cluster_centers:
# Update min_distances for all examples given new cluster center.
x = self.features[cluster_centers]
################ This is the section causing OOM issues######################################
dist = pairwise_distances(self.features, x, metric=self.metric)
if self.min_distances is None:
self.min_distances = np.min(dist, axis=1).reshape(-1,1)
else:
self.min_distances = np.minimum(self.min_distances, dist)
def select_batch_(self, model, already_selected, N, **kwargs):
"""
Diversity promoting active learning method that greedily forms a batch
to minimize the maximum distance to a cluster center among all unlabeled
datapoints.
Args:
model: model with scikit-like API with decision_function implemented
already_selected: index of datapoints already selected
N: batch size
Returns:
indices of points selected to minimize distance to cluster centers
"""
try:
# Assumes that the transform function takes in original data and not
# flattened data.
print('Getting transformed features...')
self.features = model.transform(self.X)
print('Calculating distances...')
self.update_distances(already_selected, only_new=False, reset_dist=True)
except:
print('Using flat_X as features.')
self.update_distances(already_selected, only_new=True, reset_dist=False)
new_batch = []
for _ in range(N):
if self.already_selected is None:
# Initialize centers with a randomly selected datapoint
ind = np.random.choice(np.arange(self.n_obs))
else:
ind = np.argmax(self.min_distances)
# New examples should not be in already selected since those points
# should have min_distance of zero to a cluster center.
assert ind not in already_selected
self.update_distances([ind], only_new=True, reset_dist=False)
new_batch.append(ind)
print('Maximum distance from cluster centers is %0.2f'
% max(self.min_distances))
self.already_selected = already_selected
return new_batch
我也尝试过使用scipy.spatial.distance.cdist
函数,但这无助于解决OOM问题
有没有更好的方法更有效地找到wrt内存的最小距离?过去scikit-learn
库中的优化对我有所帮助,但在这种情况下,它似乎不适用于大型数据集
目前没有回答
相关问题 更多 >
编程相关推荐