Skip to content

Instantly share code, notes, and snippets.

@lebedov
Created February 21, 2017 15:39
Show Gist options
  • Save lebedov/85840f17dbfa264a4c16612279c4d0d9 to your computer and use it in GitHub Desktop.
Save lebedov/85840f17dbfa264a4c16612279c4d0d9 to your computer and use it in GitHub Desktop.
Pairwise distance module for pytorch.
#!/usr/bin/env python
"""
Pairwise distance module for pytorch.
"""
import torch.autograd as autograd
import torch.nn as nn
class PairwiseDistance(nn.Module):
def __init__(self, norm_type, dim=1):
super(PairwiseDistance, self).__init__()
self.norm_type = norm_type
self.dim = dim
def forward(self, input):
return autograd.variable.Norm(self.norm_type,
self.dim)(input[1]-input[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment