[英]Any ideas on how to efficiently create this matrix/mask?
Pytorch 提供了tensor.diagonal方法,它使您可以访问张量的任何对角线。 要将值分配给张量的结果视图,您可以使用tensor.copy_ 。 这会给你类似的东西:
def circulant(n, window):
circulant_t = torch.zeros(n,n)
# [0, 1, 2, ..., window, -1, -2, ..., window]
offsets = [0] + [i for i in range(window)] + [-i for i in range(window)]
for offset in offsets:
#size of the 1-tensor depends on the length of the diagonal
circulant_t.diagonal(offset=offset).copy_(torch.ones(n-abs(offset)))
return circulant_t
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.