torch.narrow_copy¶
- torch.narrow_copy(input, dim, start, length, *, out=None) Tensor ¶
與
Tensor.narrow()
相同,但此方法會傳回副本,而不是共用儲存空間。這主要是為了稀疏張量,因為它們沒有共用儲存空間的 narrow 方法。- 參數
- 關鍵字參數
out (Tensor, optional) – 輸出張量 (可選)。
範例
>>> x = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> torch.narrow_copy(x, 0, 0, 2) tensor([[ 1, 2, 3], [ 4, 5, 6]]) >>> torch.narrow_copy(x, 1, 1, 2) tensor([[ 2, 3], [ 5, 6], [ 8, 9]]) >>> s = torch.arange(16).reshape(2, 2, 2, 2).to_sparse(2) >>> torch.narrow_copy(s, 0, 0, 1) tensor(indices=tensor([[0, 0], [0, 1]]), values=tensor([[[0, 1], [2, 3]], [[4, 5], [6, 7]]]), size=(1, 2, 2, 2), nnz=2, layout=torch.sparse_coo)
參見
torch.narrow()
,為非複製版本