本文簡要介紹python語言中 torch.where
的用法。
用法:
torch.where(condition, x, y) → Tensor
形狀等於
condition
、x
、y
的廣播形狀的張量返回從
x
或y
中選擇的元素的張量,具體取決於condition
。操作定義為:
注意
張量
condition
、x
、y
必須是可廣播的。注意
當前有效的標量和張量組合是 1. 浮點數 dtype 和 torch.double 的標量 2. 積分 dtype 和 torch.long 的標量 3. 複雜 dtype 和 torch.complex128 的標量
例子:
>>> x = torch.randn(3, 2) >>> y = torch.ones(3, 2) >>> x tensor([[-0.4620, 0.3139], [ 0.3898, -0.7197], [ 0.0478, -0.1657]]) >>> torch.where(x > 0, x, y) tensor([[ 1.0000, 0.3139], [ 0.3898, 1.0000], [ 0.0478, 1.0000]]) >>> x = torch.randn(2, 2, dtype=torch.double) >>> x tensor([[ 1.0779, 0.0383], [-0.8785, -1.1089]], dtype=torch.float64) >>> torch.where(x > 0, x, 0.) tensor([[1.0779, 0.0383], [0.0000, 0.0000]], dtype=torch.float64)
torch.where(condition) → tuple of LongTensor
torch.where(condition)
與torch.nonzero(condition, as_tuple=True)
相同。注意
另見
torch.nonzero()
。
參數:
返回:
返回類型:
相關用法
- Python PyTorch wrap_torch_function用法及代碼示例
- Python PyTorch wrap用法及代碼示例
- Python PyTorch wav2vec2_model用法及代碼示例
- Python PyTorch weight_norm用法及代碼示例
- 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用法及代碼示例
- Python PyTorch ShardedEmbeddingBagCollection.named_parameters用法及代碼示例
- Python PyTorch sqrt用法及代碼示例
- Python PyTorch PackageImporter.id用法及代碼示例
- Python PyTorch column_stack用法及代碼示例
注:本文由純淨天空篩選整理自pytorch.org大神的英文原創作品 torch.where。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。