Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save memory when compute shortest distance #27

Open
Tiamo666 opened this issue Jun 6, 2019 · 3 comments
Open

Save memory when compute shortest distance #27

Tiamo666 opened this issue Jun 6, 2019 · 3 comments

Comments

@Tiamo666
Copy link

Tiamo666 commented Jun 6, 2019

No description provided.

@Tiamo666
Copy link
Author

Tiamo666 commented Jun 6, 2019

@michuanhaohao Thanks for your great work.
But I had a suggestion that when compute the shortest distance will cost O(MN) extra space, where M, N is the size of the dist_mat. We can reduce the extra space from O(MN) to O(N). I implemented it as following:

def shortest_path(dist_mat):
    """
    Args:
    dist_mat: pytorch Variable, available shape:
      1) [m, n]
      2) [m, n, N], N is batch size
      3) [m, n, *], * can be arbitrary additional dimensions
      Returns:
    dist: three cases corresponding to `dist_mat`:
      1) scalar
      2) pytorch Tensor, with shape [N]
      3) pytorch Tensor, with shape [*]
    """
    if dist_mat.dim() == 3:
        M, N, B = dist_mat.size()
        helper = torch.zeros(N, B)
    elif dist_mat.dim() == 2:
        M, N = dist.size()
        helper = torch.zeros(N)
    for i in range(M):
        for j in range(N):
            if i == 0 and j == 0:
                helper[j] = dist_mat[i][j]
            elif i == 0:
                helper[j] = helper[j-1] + dist_mat[i][j]
            elif j == 0:
                helper[j] = helper[j] + dist_mat[i][j]
            else:
                helper[j] = torch.min(helper[j-1], helper[j]) + dist_mat[i][j]
    return helper[-1]

I think we can even do better to reduce the extra space to O(min(N, M))

@michuanhaohao
Copy link
Owner

Thank you for you job! If results can be reproduced by your new implementation, you can make a pull request and become a contributor of this project.

@Tiamo666
Copy link
Author

OK, but I was a little busy with other things recently, I'll make a pull request if I can reimplement the result.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants