【问题标题】:torch : Dijkstra's algorithm火炬:Dijkstra 算法
【发布时间】:2021-03-24 14:15:50
【问题描述】:

我正在研究 3D 点云。我有点云图形结构的 SPARSE MATRIX 表示(如 scipy.sparse 中的 csr_matrix)。我想将在测地距离的某个阈值内的点(由图中的路径长度近似)组合在一起并将它们一起处理。要找到这些点,我需要运行一些最短路径查找算法,例如 Dijkstra 的。简而言之,我的想法是这样的

  1. 从 N 个点中采样 K 个点(我可以使用最远点采样)
  2. 为每个 K 点查找最近的测地线邻居(使用 BackProp 支持的算法)
  3. 使用一些神经网络处理每个点的邻居

这将进入我的转发功能。 有没有办法在我的功能中实现 Dijkstra?

或者我可以实现的任何其他想法?

非常感谢!

【问题讨论】:

    标签: python machine-learning deep-learning pytorch


    【解决方案1】:

    我使用here 讨论的优先级队列为 Dijkstra 创建了自定义实现 同样,我使用以下 Torch 函数创建了一个自定义的PriorityQ

    class priorityQ_torch(object):
        """Priority Q implelmentation in PyTorch
    
        Args:
            object ([torch.Tensor]): [The Queue to work on]
        """
    
        def __init__(self, val):
            self.q = torch.tensor([[val, 0]])
            # self.top = self.q[0]
            # self.isEmpty = self.q.shape[0] == 0
    
        def push(self, x):
            """Pushes x to q based on weightvalue in x. Maintains ascending order
    
            Args:
                q ([torch.Tensor]): [The tensor queue arranged in ascending order of weight value]
                x ([torch.Tensor]): [[index, weight] tensor to be inserted]
    
            Returns:
                [torch.Tensor]: [The queue tensor after correct insertion]
            """
            if type(x) == np.ndarray:
                x = torch.tensor(x)
            if self.isEmpty():
                self.q = x
                self.q = torch.unsqueeze(self.q, dim=0)
                return
            idx = torch.searchsorted(self.q.T[1], x[1])
            print(idx)
            self.q = torch.vstack([self.q[0:idx], x, self.q[idx:]]).contiguous()
    
        def top(self):
            """Returns the top element from the queue
    
            Returns:
                [torch.Tensor]: [top element]
            """
            return self.q[0]
    
        def pop(self):
            """pops(without return) the highest priority element with the minimum weight
    
            Args:
                q ([torch.Tensor]): [The tensor queue arranged in ascending order of weight value]
    
            Returns:
                [torch.Tensor]: [highest priority element]
            """
            if self.isEmpty():
                print("Can Not Pop")
            self.q = self.q[1:]
    
        def isEmpty(self):
            """Checks is the priority queue is empty
    
            Args:
                q ([torch.Tensor]): [The tensor queue arranged in ascending order of weight value]
    
            Returns:
                [Bool] : [Returns True is empty]
            """
            return self.q.shape[0] == 0
    
    

    现在是dijkstra,带有邻接矩阵(以图权重作为输入)

    def dijkstra(adj):
        n = adj.shape[0]
        distance_matrix = torch.zeros([n, n])
        for i in range(n):
            u = torch.zeros(n, dtype=torch.bool)
            d = np.inf * torch.ones(n)
            d[i] = 0
            q = priorityQ_torch(i)
            while not q.isEmpty():
                v, d_v = q.top()  # point and distance
                v = v.int()
                q.pop()
                if d_v != d[v]:
                    continue
                for j, py in enumerate(adj[v]):
                    if py == 0 and j != v:
                        continue
                    else:
                        to = j
                        weight = py
                        if d[v] + py < d[to]:
                            d[to] = d[v] + py
                            q.push(torch.Tensor([to, d[to]]))
            distance_matrix[i] = d
        return distance_matrix
    

    返回图形点的最短路径距离矩阵!

    【讨论】:

      猜你喜欢
      • 2016-03-12
      • 1970-01-01
      • 2018-09-02
      • 2017-02-23
      • 2021-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多