title: Normalizing CNN network output to get a distance output between 0 and 1
body: <p>I'm using PyTorch based CNNs to do feature extraction on images of humans in order to use it to re-identify that same person given a different picture. After the whole process I am left with a 1D vector, about <code>2048x1</code> which I then compare using L2 distance as a metric. I am currently trying to normalize that L2 distance output so I can represent the model's prediction as a confidence from <code>0-1</code>.</p>
  <p>I noticed that PyTorch recommends using the where images are loaded in as loaded in to a range of <code>[0, 1]</code> and then normalized using <code>mean = [0.485, 0.456, 0.406]</code> and <code>std = [0.229, 0.224, 0.225]</code>. When it does this, it seems to alter the range of the L2 distance output which changes the range of values for the L2 distance which makes it no longer <code>0-1</code>. I am wondering is there a way to normalize the output vector back to the <code>0-1</code> range or even the L2 distance itself so I can represent it from <code>0-1</code></p>
  <p><strong>EDIT:</strong></p>
  <p>The following is the code that I am using to calculate the L2 distance</p>
  <pre class="lang-py prettyprint-override"><code>distmat = torch.pow(qf, 2).sum(dim=1, keepdim=True).expand(m, n) + \
                torch.pow(gf, 2).sum(dim=1, keepdim=True).expand(n, m).t()
  distmat.addmm_(qf, gf.t(), beta=1, alpha=-2, )
  distmat = distmat.cpu().numpy()
  </code></pre>
  <p>As was suggested by a commenter, I am attempting to change the L2 distance calculation to instead use cosine distance. This is my current code:</p>
  <pre class="lang-py prettyprint-override"><code>distmat = torch.mm(qf, gf.t())
  </code></pre>
  <p>However when I ran this, I was getting outputs that looked like this:</p>
  <p><code>tensor([[ 0.3244,  0.2478,  0.1808, -0.0249,  0.2137,  0.2113]])</code></p>
  <p>Wondering if this is the right way to do the cosine distance calculation?</p>
  <p>EDIT 2:</p>
  <p>Here's how cosine similarity's final implementation looked like for me:</p>
  <pre class="lang-py prettyprint-override"><code>qf_norm = qf / qf.norm(dim=1)[:, None]
  gf_norm = gf / gf.norm(dim=1)[:, None]
  distmat = torch.mm(qf_norm, gf_norm.transpose(0, 1)).cpu().numpy()
  </code></pre>