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
Pytorch提供了tensor.diagonal方法,可以访问张量的任何对角线。要为张量的结果视图赋值,可以使用tensor.copy_。这会给你一些类似的东西:
相关问题 更多 >
编程相关推荐