Spec-Zone.ru › PyTorch 2.14

torch.where

torch.where(condition, input, other, *, out=None) → Tensor

Возвращает тензор, элементы которого выбираются из input или other в зависимости от condition.

Операция определяется следующим образом:

outi={inputiif conditioniotheriotherwise\text{out}_i = \begin{cases} \text{input}_i & \text{if } \text{condition}_i \\ \text{other}_i & \text{otherwise} \\ \end{cases}

Примечание

Тензоры condition, input, other должны быть совместимы для широковещательного распространения.

Параметры:
  • condition (BoolTensor) – Если значение равно True (ненулевое), возвращается input, иначе — other
  • input (Tensor or Scalar) – значение (если input — скаляр) или значения, выбранные по индексам, где condition равно True
  • other (Tensor or Scalar) – значение (если other — скаляр) или значения, выбранные по индексам, где condition равно False
Именованные аргументы:

out (Tensor, optional) – выходной тензор.

Возвращает:

Тензор с формой, соответствующей форме, полученной широковещательным распространением condition, input, other

Тип возвращаемого значения:

Tensor

Пример:

>>> 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, 1.0, 0.0)
tensor([[0., 1.],
        [1., 0.],
        [1., 0.]])
>>> 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().

© 2026, PyTorch Contributors
PyTorch has a BSD-style license, as found in the LICENSE file.
https://docs.pytorch.org/docs/2.14/generated/torch.where.html

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API