Pytorch:计算批处理张量的标准

发布于 2025-02-12 20:03:06 字数 315 浏览 0 评论 0原文

我的张量t带有shape(batch_size x dims)和另一种张量v带有shape(vocab_size x dims)。我想用shape(batch_size x vocab_size)产生张量d,以便d [i,j] = norm(t [i] - v [j])。

对于单个张量(无批次)进行此操作是微不足道的:d = torch.norm(v -t),因为t将被广播。当张量批处理时,该怎么办?

I have tensor t with shape (Batch_Size x Dims) and another tensor v with shape (Vocab_Size x Dims). I'd like to produce a tensor d with shape (Batch_Size x Vocab_Size), such that d[i,j] = norm(t[i] - v[j]).

Doing this for a single tensor (no batches) is trivial: d = torch.norm(v - t), since t would be broadcast. How can I do this when the tensors have batches?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一抹微笑 2025-02-19 20:03:06

将单位尺寸插入vt以使其(1 x vocab_size x dims)和(batch_size x 1 x dims)分别。接下来,以广播的差异获取形状张量(batch_size x vocab_size x dims)。将其传递给torch.norm以及可选的dim = 2参数,以便沿最后一个维度采取规范。这将导致所需的(batch_size x vocab_size)规范的张量。

d = torch.norm(v.unsqueeze(0) - t.unsqueeze(1), dim=2)

编辑:正如@konstantinoskokos在评论中指出的那样,由于广播规则 Numpy和Pytorch使用的是领先的统一维度V不需要明确。即您可以使用

d = torch.norm(v - t.unsqueeze(1), dim=2)

Insert unitary dimensions into v and t to make them (1 x Vocab_Size x Dims) and (Batch_Size x 1 x Dims) respectively. Next, take the broadcasted difference to get a tensor of shape (Batch_Size x Vocab_Size x Dims). Pass that to torch.norm along with the optional dim=2 argument so that the norm is taken along the last dimension. This will result in the desired (Batch_Size x Vocab_Size) tensor of norms.

d = torch.norm(v.unsqueeze(0) - t.unsqueeze(1), dim=2)

Edit: As pointed out by @KonstantinosKokos in the comments, due to the broadcasting rules used by numpy and pytorch, the leading unitary dimension on v does not need to be explicit. I.e. you can use

d = torch.norm(v - t.unsqueeze(1), dim=2)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文