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



我有一个大小为 NxDxWxH 的小批量,其中 N 是小批量的大小,D 是尺寸,W 和 H 分别是宽度和高度。假设我有一组过滤器 F,每个过滤器的维度为 Dx1x1。我需要计算小批量和过滤器之间的成对距离。输出的大小应为 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

换句话说,我试图找到成对的 L2 距离,而不是卷积。

如何在 pytorch 中执行此操作?

您可以通过扩展输入和过滤器来实现正确的自动形状转换。

# 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)

相关内容

最新更新