Kuhn-Munkres algorithm (Hungarian) in torch: is there any point here?

kpierce: <p>I have a very large assignment problem which takes quite some time on a CPU. I was solving this with the Munkres algorithm in numpy using this <a href="https://github.com/scipy/scipy/blob/v0.18.1/scipy/optimize/_hungarian.py#L13-L107" rel="nofollow noopener">scipy code</a>.</p>
<p>I wonder if this is the type of computation which would be greatly sped up by GPU? I would be interested in implementing this code in torch if this would help me. Any thoughts are appreciated, thanks.</p>

achark: <p>I found a implementation for solving LAP problem by torch on github, but it is not quiet usable.</p>
<aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/bkj/auction-lap/blob/master/auction_lap.py" target="_blank" rel="nofollow noopener">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/bkj/auction-lap/blob/master/auction_lap.py" target="_blank" rel="nofollow noopener">bkj/auction-lap/blob/master/auction_lap.py</a></h4>
<pre><code class="lang-py">#!/usr/bin/env python

"""
    auction_lap.py
    
    From
        https://dspace.mit.edu/bitstream/handle/1721.1/3265/P-2108-26912652.pdf;sequence=1
"""

from __future__ import print_function, division

import sys
import torch

def auction_lap(X, eps=None, compute_score=True):
    """
        X: n-by-n matrix w/ integer entries
        eps: "bid size" -- smaller values means higher accuracy w/ longer runtime
    """
    eps = 1 / X.shape[0] if eps is None else eps
</code></pre>

  This file has been truncated. <a href="https://github.com/bkj/auction-lap/blob/master/auction_lap.py" target="_blank" rel="nofollow noopener">show original</a>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>

<p>I would really appreciate it if someone would share a robust implementation for LAP problem</p>

Kushaj: <p>Can you share the difficulties you are facing with the above code? Do you want to use the auction algorithm for solving LAP problem?. I can work around a solution if you want.</p>

achark: <p>I am trying to get the best combination of two set of word embeddings. So I basically calculate a similar matrix a[i,j] = similarity(Ei, Ej) and then apply a LAP algorithm.</p>
<p>So it looks like:</p>
<pre><code class="lang-python">sim_matrix = torch.zeros([100, 100], dtype=torch.float)
nn.init.uniform_(sim_matrix)
auction_lap(sim_matrix)
</code></pre>
<p>and I got this error:</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "E:/文档/code/EAbyRule/graph_completion/auction_lap.py", line 67, in &lt;module&gt;
    auction_lap(a)
  File "E:/文档/code/EAbyRule/graph_completion/auction_lap.py", line 41, in auction_lap
    src=bid_increments.view(-1, 1)
RuntimeError: Dimension out of range (expected to be in range of [-1, 0], but got 1)
</code></pre>

Mirsadeghi: <p>This is a fast version of LAP for numpy.<br>
</p><aside class="onebox githubfolder">
  <header class="source">
      <img src="https://github.githubassets.com/favicons/favicon.svg" class="site-icon" width="16" height="16">
      <a href="https://github.com/cheind/py-lapsolver" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>
  <article class="onebox-body">
    <img src="https://avatars1.githubusercontent.com/u/38565?s=400&amp;amp;v=4" class="thumbnail onebox-full-image" width="60" height="60">

<h3><a href="https://github.com/cheind/py-lapsolver" target="_blank" rel="noopener nofollow ugc">cheind/py-lapsolver</a></h3>


  <p><span class="label1">Fast linear assignment problem (LAP) solvers for Python based on c-extensions</span></p>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>
<br>
transfer torch 2 numpy and come back!<br>
enjoy it.

NeverConvex: <p>Try adding</p>
<pre><code class="lang-auto">if len(bids_.shape) &lt; 2:
            bids_ = bids_.unsqueeze(dim=0)
</code></pre>
<p>between lines <code>50</code> and <code>51</code> in <code>auction_lap.py</code>:</p>
<pre><code class="lang-auto">bids_.zero_()
if len(bids_.shape) &lt; 2:
            bids_ = bids_.unsqueeze(dim=0)
bids_.scatter_(
            dim=1,
            index=first_idx.contiguous().view(-1, 1),
            src=bid_increments.view(-1, 1)
        )
</code></pre>
<p>edit:</p>
<p>You will also want make the following adjustment at <code>line 40</code>:</p>
<pre><code class="lang-auto">unassigned = (curr_ass == -1).nonzero().squeeze()
if len(unassigned.shape) &lt; 1:
            unassigned = unassigned.unsqueeze(0)
value = X[unassigned] - cost
</code></pre>

yuri: <p>does this not kill the computational graph. could one still calculate losses after converting the tensors to numpy?</p>

yuri: <p>when the original scipy method outputs the col and rows indices, this method output only one array indices. could this still link to the original one?<br>
thx</p>

Mirsadeghi: <p><a class="mention" href="/u/yuri">@yuri</a> . Yes, It will disconnect the computational graph. I just mentioned it as a fast solution. for End-to-End training you should look for a substiution.</p>

ivan-chai: <p>Here is a CUDA implementation for the batched linear assignment. It uses data parallelism and is useful for a large set of small problems.</p>
<aside class="onebox allowlistedgeneric" data-onebox-src="https://github.com/ivan-chai/torch-linear-assignment">
  <header class="source">
      <img src="https://github.githubassets.com/favicons/favicon.svg" class="site-icon" width="32" height="32">

      <a href="https://github.com/ivan-chai/torch-linear-assignment" target="_blank" rel="noopener nofollow ugc">GitHub</a>
  </header>

  <article class="onebox-body">
    <div class="aspect-image" style="--aspect-ratio:690/344;"><img src="https://opengraph.githubassets.com/c9779f38597bb9453bbd7116915650c42d7a43bec7a6516609066023eb516f00/ivan-chai/torch-linear-assignment" class="thumbnail" width="690" height="345"></div>

<h3><a href="https://github.com/ivan-chai/torch-linear-assignment" target="_blank" rel="noopener nofollow ugc">GitHub - ivan-chai/torch-linear-assignment: Batch computation of the linear...</a></h3>

  <p>Batch computation of the linear assignment problem on GPU. - ivan-chai/torch-linear-assignment</p>


  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>