E-means: Outperforming k-means in one-tenth the epochs
本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。
E-means: Outperforming k-means in one-tenth the epochs
100,000 datapoints in 200 clusters, two wide Gaussians and three small ones, cut off at a box, on a light uniform background. Lloyd has 1 update against 97 per epoch for e-means. 在200个聚类中有100,000个数据点,包括两个宽的高斯分布和三个小的高斯分布,被限制在一个方框内,背景为均匀的浅色。对于e-means,每个epoch有97次更新,而Lloyd算法只有1次。
Across a large range of scales and datasets, e-means reaches a test loss below Lloyd’s at 25 epochs, in 1.2 to 3 epochs. Measured end to end, that is a 10–45× wall-clock reduction on GPU against the state-of-the-art Lloyd implementation (Flash-KMeans[2]), and 8–15× on CPU against Faiss[1]. Speed and quality usually trade against each other in k-means, while e-means achieves both better speed and loss. 在广泛的规模和数据集上,e-means在1.2到3个epoch内即可达到Lloyd算法在25个epoch时的测试损失水平。从端到端测量来看,这相当于在GPU上比最先进的Lloyd实现(Flash-KMeans[2])减少了10–45倍的实际运行时间,在CPU上比Faiss[1]减少了8–15倍。在k-means中,速度和质量通常是相互权衡的,而e-means同时实现了更好的速度和更低的损失。
Given a fixed dataset of n points X = {x1, …, xn} ⊂ Rd and an integer k, the k-means problem is to find k centroids C = (c1, …, ck) minimizing the mean squared distance between each point and its nearest centroid: 给定一个包含n个点 X = {x1, …, xn} ⊂ Rd 的固定数据集和一个整数k,k-means问题旨在找到k个质心 C = (c1, …, ck),以最小化每个点与其最近质心之间的平均平方距离:
It’s a static dataset, unlike codebook learning inside VQ-VAEs, where the distribution to cluster is constantly moving. 这是一个静态数据集,与VQ-VAE内部的码本学习不同,后者的聚类分布是不断变化的。
The goal of e-means is to provide a modern optimization rule for the k-means problem. We started this project from the following observations: e-means的目标是为k-means问题提供一种现代化的优化规则。我们从以下观察结果开始了该项目:
For e-means, we chose to do mini-batch updates with exponential moving averages[6], which we found to be most efficient for the k-means problem. Contrary to previous mini-batch approaches, e-means requires no hyperparameter tuning because it automatically sets the batch size and step size with scaling laws (see Automatic hyperparameter tuning with scaling laws). In addition, e-means improves the initialization and the dead-cluster revival strategy, adds a cosine-scheduled decay rate, and introduces a finalization stage that reduces the training loss for a negligible runtime penalty at the end of training. e-means is highly parallelizable, and comes with specialized kernels that translate its algorithmic advantage into wall-clock speedups. 对于e-means,我们选择使用指数移动平均[6]进行小批量更新,我们发现这对k-means问题最为有效。与以往的小批量方法不同,e-means不需要超参数调整,因为它通过缩放定律自动设置批量大小和步长(参见“利用缩放定律进行自动超参数调整”)。此外,e-means改进了初始化和死聚类恢复策略,增加了余弦调度衰减率,并引入了一个最终化阶段,在训练结束时以微不足道的运行时间代价降低了训练损失。e-means具有高度可并行性,并配备了专门的内核,将其算法优势转化为实际运行时间的加速。
Each mini-batch updates every cluster’s count and sum. The centroid is their ratio. 每个小批量更新每个聚类的计数和总和。质心即为它们的比值。
Everything is derived from scaling laws. 一切都源于缩放定律。
Novel initialization and finalization methods. The run ends on each cluster’s exact last-epoch mean. 新颖的初始化和最终化方法。运行结束时,每个聚类都处于其最后一个epoch的精确均值上。
The step α anneals from α0 to 0.1α0, as a learning rate does. 步长α从α0退火至0.1α0,就像学习率一样。
Clusters with size below a threshold are revived by splitting a large cluster. 大小低于阈值的聚类通过拆分大聚类来恢复。
GPU kernels fuse the nearest-centroid assignment, turning the epoch savings above into matching wall-clock speedups. GPU内核融合了最近质心分配,将上述epoch的节省转化为相应的实际运行时间加速。
The centroid update rule is the central component of e-means. This section aims to provide an overview of update rules for k-means clustering. All methods maintain, for each cluster j, a sum Sj and a count Nj of the points assigned to it, and take the centroid as their ratio: 质心更新规则是e-means的核心部分。本节旨在概述k-means聚类的更新规则。所有方法都为每个聚类j维护分配给它的点的总和Sj和计数Nj,并将质心取为它们的比值:
They differ only in how Sj and Nj are updated. Below, sj and nj denote the sum and the count of the points in the current batch that are assigned to cluster j (both zero if none are). 它们仅在Sj和Nj的更新方式上有所不同。下文中,sj和nj表示当前批次中分配给聚类j的点的总和和计数(如果没有分配,则两者均为零)。
Lloyd’s algorithm[7] assigns every point to its nearest centroid and recomputes the statistics from scratch, using the full dataset as the batch: Lloyd算法[7]将每个点分配给其最近的质心,并使用完整数据集作为批次,从头重新计算统计数据:
so cj is the exact mean of the points currently assigned to it. Each update costs one full pass over the data, and the procedure is repeated a fixed number of times (typically 20 to 50 iterations). 因此,cj是当前分配给它的点的精确均值。每次更新都需要对数据进行一次完整遍历,该过程重复固定次数(通常为20到50次迭代)。
MacQueen’s update[3] accumulates the statistics without ever discarding them: MacQueen更新[3]在累积统计数据时从不丢弃它们:
so cj is the uniform mean of every point assigned to cluster j since the start of training, including points assigned under earlier, possibly poor, centroid positions. This was later partially addressed by Staleness-Reduction Mini-Batch K-Means[8], a hybrid method between k-means and MacQueen. They obtained important speedups, but only on very low numbers of clusters. 因此,cj是自训练开始以来分配给聚类j的每个点的均匀均值,包括在早期可能较差的质心位置下分配的点。这一点后来通过“陈旧度减少小批量K-Means”[8]得到了部分解决,这是一种介于k-means和MacQueen之间的混合方法。他们获得了显著的加速,但仅限于极少数的聚类数量。
e-means works on mini-batches and replaces this sum with an exponential moving average: e-means在小批量上工作,并用指数移动平均值替换了此总和:
so older assignments are forgotten at rate α, and the centroid tracks recent assignments rather than the whole history. This update rule is typically used for moving distributions, hence their adoption for training VQ-VAEs[6]. What we found is that it’s also extremely efficient for static datasets if it’s combined with e-means’ other components, and hyperparameters are properly tuned. 因此,较旧的分配以α的速率被遗忘,质心跟踪的是最近的分配而不是整个历史。这种更新规则通常用于移动分布,因此被用于训练VQ-VAE[6]。我们发现,如果将其与e-means的其他组件结合使用,并且适当地调整超参数,它对于静态数据集也非常有效。
The e-means project started from previous work at Meta FAIR by Hugo Touvron and Hervé Jégou, who had found that mini-batch k-means by gradient descent could beat Lloyd’s algorithm. While I could reproduce these results, I found that the performance of gradient-descent based centroid updates is unstable at large learning rates, unlike exponential moving average updates. I suspect that is because EMA updates guarantee that the centroid is always in the convex hull of points assigned to it, while gradient descent updates may overshoot past that hull. e-means项目始于Meta FAIR的Hugo Touvron和Hervé Jégou之前的工作,他们发现通过梯度下降进行的小批量k-means可以胜过Lloyd算法。虽然我能够复现这些结果,但我发现基于梯度下降的质心更新在学习率较高时表现不稳定,这与指数移动平均更新不同。我怀疑这是因为EMA更新保证了质心始终位于分配给它的点的凸包内,而梯度下降更新可能会越过该凸包。
In order to achieve the lowest loss at a fixed number of epochs, we chose exponential moving average updates, hence the name e-means for our method. 为了在固定的epoch数下实现最低损失,我们选择了指数移动平均更新,因此我们的方法命名为e-means。
If you set the batch to the full dataset, B = n, and α = 1, then Nj and Sj become exactly the count and sum of the whole cluster, so one e-means iteration becomes exactly one Lloyd iteration. Lloyd is an extreme case of e-means without cosine scheduling on the learning rate. However, our hyperparameter search found that this extreme setting is highly suboptimal, which explains why e-means largely outperforms Lloyd. Below, the loss is measured for every batch size B and step α: Lloyd lands in a corner, well outside the region where the loss is lowest. 如果你将批次设置为完整数据集,即B = n,且α = 1,那么Nj和Sj就完全变成了整个聚类的计数和总和,因此一次e-means迭代就完全变成了一次Lloyd迭代。Lloyd是e-means在没有学习率余弦调度情况下的一个极端情况。然而,我们的超参数搜索发现这种极端设置是非常次优的,这解释了为什么e-means在很大程度上优于Lloyd。下文中,针对每个批量大小B和步长α测量了损失:Lloyd落在了一个角落里,远在损失最低的区域之外。
This hyperparameter study motivated us to derive scaling laws that automatically pick the optimal batch size B and step size α. 这项超参数研究促使我们推导出自动选择最佳批量大小B和步长α的缩放定律。
To better understand why e-means reaches better losses than Lloyd at convergence, we plotted a few trainings on toy datasets to evaluate their ability to escape local minima. Repeating trainings over 200 seeds, we found that e-means is much less likely to fall in pathological configurations compared to Lloyd. 为了更好地理解为什么e-means在收敛时比Lloyd达到更好的损失,我们在玩具数据集上绘制了几次训练过程,以评估它们逃离局部极小值的能力。在200个随机种子下重复训练,我们发现与Lloyd相比,e-means陷入病态配置的可能性要小得多。
Our explanation for that is that mini-batches provide more noisy updates that are more likely to explore the optimization landscape and escape local minima. Lloyd, with its always decreasing loss, is more likely to exploit found local minima and ne 我们的解释是,小批量提供了更具噪声的更新,更有可能探索优化空间并逃离局部极小值。Lloyd由于其损失总是递减,更有可能利用已发现的局部极小值,并且……