torch.diagflat¶
- torch.diagflat(input, offset=0) Tensor ¶
如果
input
是一個向量(1 維 Tensor),則傳回一個 2 維正方形 Tensor,其中input
的元素作為對角線。如果
input
是一個具有多個維度的 Tensor,則傳回一個 2 維 Tensor,其對角線元素等於一個扁平化的input
。
引數
offset
控制要考慮哪個對角線如果
offset
= 0,則為主對角線。如果
offset
> 0,則在主對角線上方。如果
offset
< 0,則在主對角線下方。
- 參數 (Parameters)
範例 (Examples)
>>> a = torch.randn(3) >>> a tensor([-0.2956, -0.9068, 0.1695]) >>> torch.diagflat(a) tensor([[-0.2956, 0.0000, 0.0000], [ 0.0000, -0.9068, 0.0000], [ 0.0000, 0.0000, 0.1695]]) >>> torch.diagflat(a, 1) tensor([[ 0.0000, -0.2956, 0.0000, 0.0000], [ 0.0000, 0.0000, -0.9068, 0.0000], [ 0.0000, 0.0000, 0.0000, 0.1695], [ 0.0000, 0.0000, 0.0000, 0.0000]]) >>> a = torch.randn(2, 2) >>> a tensor([[ 0.2094, -0.3018], [-0.1516, 1.9342]]) >>> torch.diagflat(a) tensor([[ 0.2094, 0.0000, 0.0000, 0.0000], [ 0.0000, -0.3018, 0.0000, 0.0000], [ 0.0000, 0.0000, -0.1516, 0.0000], [ 0.0000, 0.0000, 0.0000, 1.9342]])