简体   繁体   English

如何在PyTorch中计算迷你批次与一组过滤器之间的距离

[英]How to calculate the distance between a mini batch and a set of filters in PyTorch

I have a mini-batch of size NxDxWxH, where N is the size of the mini-batch, D is the dimension, and W and H are the width and height respectively. 我有一个大小为NxDxWxH的微型批处理,其中N是微型批处理的大小,D是尺寸,W和H分别是宽度和高度。 Assume that I have a set of filters F, each with dimension Dx1x1. 假设我有一组过滤器F,每个过滤器的尺寸为Dx1x1。 I need to calculate the pairwise distance between the mini-batch and the filters. 我需要计算迷你批次和过滤器之间的成对距离。 The size of the output should be NxFxWxH. 输出的大小应为NxFxWxH。

      input:   NxDxWxH
      filters: FxDx1x1
      output:  NxFxWxH
      Lets assume a is a vector of size D extracted at the location (x,y)
      of the input and f is filter of size Dx1x1. Each value in the output
      should be \sum_{d=1}^D (x_d - f_c)^2

In other words, instead of convolution I am trying to find the pair-wise L2 distances. 换句话说,我不是在进行卷积运算,而是在寻找成对的L2距离。

How can I do this in pytorch? 如何在pytorch中执行此操作?

You can do this by expanding the input and filters for proper automatic shape casting. 您可以通过扩展输入和过滤器以进行适当的自动造型来实现此目的。

# Assuming that input.size() is (N, D, W, H) and filters.size() is (F, D, 1, 1)
input.unsqueeze_(1)
filters.unsqueeze_(0)
output = torch.sum((input - filters)**2, dim=2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM