本文簡要介紹python語言中 torch.unique
的用法。
用法:
torch.unique(*args, **kwargs)
一個張量或一個張量元組包含
輸出(Tensor):唯一標量元素的輸出列表。
inverse_indices(Tensor):(可選)如果
return_inverse
為 True,將有一個額外的返回張量(與輸入的形狀相同)表示原始輸入中的元素映射到輸出中的位置的索引;否則,此函數將僅返回一個張量。計數(Tensor):(可選)如果
return_counts
為 True 時,將有一個額外的返回張量(與輸出或 output.size(dim) 相同的形狀,如果指定了dim),表示每個唯一值或張量的出現次數。
返回輸入張量的唯一元素。
注意
此函數與
torch.unique_consecutive()
的不同之處在於此函數還消除了不連續的重複值。注意
目前在 CUDA 實現和 CPU 實現中,當指定 dim 時,
torch.unique
始終在開頭對張量進行排序,而不管sort
參數如何。排序可能會很慢,因此如果您的輸入張量已經排序,建議使用torch.unique_consecutive()
,這樣可以避免排序。例子:
>>> output = torch.unique(torch.tensor([1, 3, 2, 3], dtype=torch.long)) >>> output tensor([ 2, 3, 1]) >>> output, inverse_indices = torch.unique( ... torch.tensor([1, 3, 2, 3], dtype=torch.long), sorted=True, return_inverse=True) >>> output tensor([ 1, 2, 3]) >>> inverse_indices tensor([ 0, 2, 1, 2]) >>> output, inverse_indices = torch.unique( ... torch.tensor([[1, 3], [2, 3]], dtype=torch.long), sorted=True, return_inverse=True) >>> output tensor([ 1, 2, 3]) >>> inverse_indices tensor([[ 0, 2], [ 1, 2]])
參數:
返回:
返回類型:
相關用法
- Python PyTorch unique_consecutive用法及代碼示例
- Python PyTorch unicode_csv_reader用法及代碼示例
- Python PyTorch uniform_partition用法及代碼示例
- Python PyTorch uniform_用法及代碼示例
- Python PyTorch unused用法及代碼示例
- Python PyTorch unbind用法及代碼示例
- Python PyTorch unsqueeze用法及代碼示例
- Python PyTorch use_deterministic_algorithms用法及代碼示例
- Python PyTorch frexp用法及代碼示例
- Python PyTorch jvp用法及代碼示例
- Python PyTorch cholesky用法及代碼示例
- Python PyTorch vdot用法及代碼示例
- Python PyTorch ELU用法及代碼示例
- Python PyTorch ScaledDotProduct.__init__用法及代碼示例
- Python PyTorch gumbel_softmax用法及代碼示例
- Python PyTorch get_tokenizer用法及代碼示例
- Python PyTorch saved_tensors_hooks用法及代碼示例
- Python PyTorch positive用法及代碼示例
- Python PyTorch renorm用法及代碼示例
- Python PyTorch AvgPool2d用法及代碼示例
- Python PyTorch MaxUnpool3d用法及代碼示例
- Python PyTorch Bernoulli用法及代碼示例
- Python PyTorch Tensor.unflatten用法及代碼示例
- Python PyTorch Sigmoid用法及代碼示例
- Python PyTorch Tensor.register_hook用法及代碼示例
注:本文由純淨天空篩選整理自pytorch.org大神的英文原創作品 torch.unique。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。