用星号操作员重塑`*`p pytorch

发布于 2025-02-10 11:54:11 字数 285 浏览 1 评论 0原文

在阅读

def extract(a, t, x_shape):
    batch_size = t.shape[0]
    out = a.gather(-1, t.cpu())
    return out.reshape(batch_size, *((1,) * (len(x_shape) - 1))).to(t.device)

,这是最终的返回语句,*((1,)均值含义reshape函数?打开操作员?如果是,这里如何使用?

While reading this annotated implementation of Diffusion Probabilistic models in PyTorch, I got stuck at understanding this function

def extract(a, t, x_shape):
    batch_size = t.shape[0]
    out = a.gather(-1, t.cpu())
    return out.reshape(batch_size, *((1,) * (len(x_shape) - 1))).to(t.device)

What it's not clear it's the final return statement, what does the *((1,) mean into reshape function? Does that asterisk correspond to the unpacking operator? And if yes, how is it used here?

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

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

发布评论

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

评论(1

遮云壑 2025-02-17 11:54:12
(1,) * (len(x_shape) - 1))

意味着创建一个带长度len(x_shape)的元组 - 1填充仅1 s的

*(...)

意思是将元组扩展到参数中

,因此最终会变成(例如len) (x_shape) == 5)

return out.reshape(batch_size, 1, 1, 1, 1).to(t.device)
(1,) * (len(x_shape) - 1))

means to create a tuple with length len(x_shape) - 1 filled with just 1s

*(...)

means to spread the tuple into arguments

So it ends up being (say len(x_shape) == 5)

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