在Pythorch中如何计算小批量和一组过滤器之间的距离

2024-10-01 07:23:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个小批量,大小为nxtxwxh,其中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对距离。在

我怎么能在Pythorch里做到这一点?在


Tags: ofthe距离过滤器inputoutputsize宽度
1条回答
网友
1楼 · 发布于 2024-10-01 07:23:55

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

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

相关问题 更多 >