- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试实现排序聚类 here is a link to the paper (这是一种凝聚聚类)算法从头开始。我已经通读了这篇论文(多次)并且我有一个正在运行的实现,尽管它比我预期的要慢很多。
这是一个link到我的 Github,其中有下载和运行 Jupyter Notebook 的说明。
算法:
Algorithm 1 Rank-Order distance based clustering
Input:
N faces, Rank-Order distance threshold t .
Output:
A cluster set C and an “un-grouped” cluster Cun.
1: Initialize clusters C = { C1, C2, … CN }
by letting each face be a single-element cluster.
2: repeat
3: for all pair Cj and Ci in C do
4: Compute distances DR( Ci, Cj) by (4) and DN(Ci, Cj) by (5).
5: if DR(Ci, Cj) < t and DN(Ci, Cj) < 1 then
6: Denote ⟨Ci, Cj⟩ as a candidate merging pair.
7: end if
8: end for
9: Do “transitive” merge on all candidate merging pairs.
(For example, Ci, Cj, Ck are merged
if ⟨Ci, Cj⟩ and ⟨Cj, Ck⟩ are candidate merging pairs.)
10: Update C and absolute distances between clusters by (3).
11: until No merge happens
12: Move all single-element clusters in C into an “un-grouped” face cluster Cun.
13: return C and Cun.
我的实现:
我定义了一个 Cluster
类:
class Cluster:
def __init__(self):
self.faces = list()
self.absolute_distance_neighbours = None
Face
类如下:
class Face:
def __init__(self, embedding):
self.embedding = embedding # a point in 128 dimensional space
self.absolute_distance_neighbours = None
我还实现了查找排序距离 (D^R(C_i, C_j))
和归一化距离 (D^N(C_i, C_j))
在 step 4
中使用,因此这些可以被认为是理所当然的。
这是我寻找两个集群之间最近的绝对距离的实现:
def find_nearest_distance_between_clusters(cluster1, cluster2):
nearest_distance = sys.float_info.max
for face1 in cluster1.faces:
for face2 in cluster2.faces:
distance = np.linalg.norm(face1.embedding - face2.embedding, ord = 1)
if distance < nearest_distance:
nearest_distance = distance
# If there is a distance of 0 then there is no need to continue
if distance == 0:
return(0)
return(nearest_distance)
def assign_absolute_distance_neighbours_for_clusters(clusters, N = 20):
for i, cluster1 in enumerate(clusters):
nearest_neighbours = []
for j, cluster2 in enumerate(clusters):
distance = find_nearest_distance_between_clusters(cluster1, cluster2)
neighbour = Neighbour(cluster2, distance)
nearest_neighbours.append(neighbour)
nearest_neighbours.sort(key = lambda x: x.distance)
# take only the top N neighbours
cluster1.absolute_distance_neighbours = nearest_neighbours[0:N]
这是我对排序聚类算法的实现(假设 find_normalized_distance_between_clusters
和 find_rank_order_distance_between_clusters
的实现是正确的):
import networkx as nx
def find_clusters(faces):
clusters = initial_cluster_creation(faces) # makes each face a cluster
assign_absolute_distance_neighbours_for_clusters(clusters)
t = 14 # threshold number for rank-order clustering
prev_cluster_number = len(clusters)
num_created_clusters = prev_cluster_number
is_initialized = False
while (not is_initialized) or (num_created_clusters):
print("Number of clusters in this iteration: {}".format(len(clusters)))
G = nx.Graph()
for cluster in clusters:
G.add_node(cluster)
processed_pairs = 0
# Find the candidate merging pairs
for i, cluster1 in enumerate(clusters):
# Only get the top 20 nearest neighbours for each cluster
for j, cluster2 in enumerate([neighbour.entity for neighbour in \
cluster1.absolute_distance_neighbours]):
processed_pairs += 1
print("Processed {}/{} pairs".format(processed_pairs, len(clusters) * 20), end="\r")
# No need to merge with yourself
if cluster1 is cluster2:
continue
else:
normalized_distance = find_normalized_distance_between_clusters(cluster1, cluster2)
if (normalized_distance >= 1):
continue
rank_order_distance = find_rank_order_distance_between_clusters(cluster1, cluster2)
if (rank_order_distance >= t):
continue
G.add_edge(cluster1, cluster2) # add an edge to denote that these two clusters are to be merged
# Create the new clusters
clusters = []
# Note here that nx.connected_components(G) are
# the clusters that are connected
for _clusters in nx.connected_components(G):
new_cluster = Cluster()
for cluster in _clusters:
for face in cluster.faces:
new_cluster.faces.append(face)
clusters.append(new_cluster)
current_cluster_number = len(clusters)
num_created_clusters = prev_cluster_number - current_cluster_number
prev_cluster_number = current_cluster_number
# Recalculate the distance between clusters (this is what is taking a long time)
assign_absolute_distance_neighbours_for_clusters(clusters)
is_initialized = True
# Now that the clusters have been created, separate them into clusters that have one face
# and clusters that have more than one face
unmatched_clusters = []
matched_clusters = []
for cluster in clusters:
if len(cluster.faces) == 1:
unmatched_clusters.append(cluster)
else:
matched_clusters.append(cluster)
matched_clusters.sort(key = lambda x: len(x.faces), reverse = True)
return(matched_clusters, unmatched_clusters)
问题:
性能缓慢的原因是由于第 10 步:通过 (3) 更新 C 和簇之间的绝对距离
其中 (3)
是:
这是 C_i (cluster i)
和 C_j (cluster j)
中所有面之间的最小 L1 范数距离
合并集群后
因为每次在 steps 3 - 8
中找到候选合并对时,我都必须重新计算新创建的集群之间的绝对距离。我基本上必须为所有创建的集群做一个嵌套的 for 循环,然后有另一个嵌套的 for 循环来找到距离最近的两个面。之后,我仍然必须按最近距离对邻居进行排序!
我认为这是错误的方法,因为我看过 OpenBR它也实现了我想要的相同的排序聚类算法,它在方法名称下:
Clusters br::ClusterGraph(Neighborhood neighborhood, float aggressive, const QString &csv)
虽然我不太熟悉 C++,但我很确定他们不会重新计算集群之间的绝对距离,这让我相信这是我错误实现的算法的一部分。
此外,在他们的方法声明的顶部,评论说他们已经预先计算了一个 kNN 图,这很有意义,因为当我重新计算集群之间的绝对距离时,我正在做很多我以前做过的计算。我相信关键是为集群预先计算一个 kNN 图,尽管这是我坚持的部分。我想不出如何实现数据结构,以便每次合并时都不必重新计算集群的绝对距离。
最佳答案
在高层次上,这就是 OpenBR seems to do as well , 需要的是一个簇 ID 的查找表 -> 簇对象,从中生成一个新的簇列表而无需重新计算。
可以从 this section on OpenBR 处的 ID 查找表中查看新集群的生成位置.
对于您的代码,需要为每个 Cluster
对象添加一个 ID,整数可能最适合内存使用。然后更新合并代码以在 findClusters
处创建待合并索引列表,并根据现有集群索引创建新的集群列表。然后根据索引合并和更新邻居。
最后一步,邻居索引合并can be seen here on OpenBR .
关键部分是合并时不会创建新的集群,并且不会重新计算它们的距离。只有索引从现有的集群对象更新,并且它们的相邻距离被合并。
关于python - 在排序聚类算法中实现一个有效的图数据结构来保持聚类距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43462035/
我的应用程序包含两部分:网络部分和 GUI。它的工作方式有点像浏览器 - 用户从服务器请求一些信息,服务器发回一些代表某些 View 的数据,然后 GUI 显示它。 现在我已经将网络部分实现为一项服务
给定表达式字符串exp,编写程序检查exp中“{”、“}”、“(”、“)”、“[”、“]的对和顺序是否正确。 package main import ( "fmt" stack "gi
我想要一个简单的脚本在后台保持运行。目前看起来像这样: import keyboard while True: keyboard.wait('q') keyboard.send('ct
我维护着许多 RedHat Enterprise Linux(7 台和 8 台)服务器(>100 台),其中包含不同的应用程序。为了保持理智,我当然会使用 Ansible 等工具,更重要的是,公共(p
我有一个 winforms 应用程序,它在网络服务请求期间被锁定 我已经尝试使用 doEvents 来保持应用程序解锁,但它仍然不够响应, 我怎样才能绕过这个锁定,让应用程序始终响应? 最佳答案 最好
我正在努力在我的项目中获得并保持领先的 0。以下是当前相关的代码: Dim jobNum As String jobNum = Left(r1.Cells(1, 1), 6) r2.Cells(1
我正在尝试在我的 Canvas 中定位元素相对于我的背景。 窗口被重新调整大小,保持纵横比。 背景随着窗口大小而拉伸(stretch)。 问题是一旦重新调整窗口大小,元素位置就会不正确。如果窗口的大小
一直在玩弄 Hibernate 和 PostgreSQL,试图让它按预期工作。 但是由于某种原因,当我尝试将具有@OneToMany 关系的对象与集合中的多个项目保持一致时,除了第一个项目之外,所有项
我想将某些东西提交到 github 存储库,但我(显然)没有任何权利这样做。我对那个 repo 做了一个分支,提交了我的更改并提交了一个 pull-request。 现在,问题是过了一段时间其他人已经
这是一个初学者问题,我仍在考虑“在 OOP 中”,所以如果我错过了手册中的答案或者答案很明显,我深表歉意。 假设我们有一个抽象类型, abstract type My_Abstract_type en
我们正在开展的一些项目在 jQuery 1.4.2 或更早版本中有着深厚的根基,介于缺乏最新版本的性能优势(或语法糖)、使用现已弃用的方法的耻辱以及部署一个积极维护的库的 3 年以上旧版本,升级现在迫
我看到在FMDB 2.0中,作者为线程添加了FMDatabaseQueue。例子是: // First, make your queue. FMDatabaseQueue *queue = [FMDa
我在 NSScrollView 中有一个 NSTableView。 NSTableView 的内容是通过绑定(bind)到 NSArrayController 来提供的,而 NSArrayContro
我在 TreeView 上有一个节点,我手动填充该节点并希望保持排序。通过用户交互,TreeViewItem 上的标题可能会更改,它们应该移动到列表中的适当位置。 我遍历一个 foreach,创建多个
我从主 NSWindow 打开一个 NSWindow。 DropHereWindowController *dropHereWindowController = [[DropHereWindowCon
我需要放置一个 form 3 按钮,当我单击该按钮时,将其显示为按下,其他按钮向上,当我单击另一个按钮时,它应该为“向下”,其他按钮应为“向上” 最佳答案 所有按钮的属性“Groupindex”必须设
我有一个使用 AnyEvent::MQTT 订阅消息队列的 perl 脚本。 目前我想要它做的就是在收到消息时打印出来。我对 perl 完全陌生,所以我正在使用它附带的演示代码,其中包括将 STDIN
如何在 .NET 应用程序中保持 TreeView 控件的滚动位置?例如,我有一个树形 View 控件,并经历了一个向其添加各种节点的过程,并将它们固定在底部。在此过程中,我可以滚动浏览 TreeVi
我维护了大量的 vbscripts,用于在我的网络上执行各种启动脚本,并且有一些我在几乎所有脚本中使用的函数。 除了复制和粘贴之外,有没有人对我如何创建可重用 vbscript 代码库有建议。我并不反
我有一些关于 Azure 自托管的问题。 假设用户 Alex 在物理机 M 上设置了 Windows 自托管代理。当 Alex 注销且计算机进入休眠状态时,代理将脱机。现在,当 Bob 登录同一台计算
我是一名优秀的程序员,十分优秀!