[英]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.