本文简要介绍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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。