Table of Contents
Shortcuts

# torch.combinations

torch.combinations(input, r=2, with_replacement=False) → seq

  Compute combinations of length r r r of the given tensor. The behavior is similar to python’s itertools.combinations when with_replacement is set to False, and itertools.combinations_with_replacement when with_replacement is set to True.

  Parameters:

    - input (Tensor) – 1D vector.

    - r (int, optional) – number of elements to combine

    - with_replacement (bool, optional) – whether to allow duplication in combination

  Returns:

    A tensor equivalent to converting all the input tensors into lists, do itertools.combinations or itertools.combinations_with_replacement on these lists, and finally convert the resulting list into tensor.

  Return type:

    Tensor

  Example:

      >>> a = [1, 2, 3]
      >>> list(itertools.combinations(a, r=2))
      [(1, 2), (1, 3), (2, 3)]
      >>> list(itertools.combinations(a, r=3))
      [(1, 2, 3)]
      >>> list(itertools.combinations_with_replacement(a, r=2))
      [(1, 1), (1, 2), (1, 3), (2, 2), (2, 3), (3, 3)]
      >>> tensor_a = torch.tensor(a)
      >>> torch.combinations(tensor_a)
      tensor([[1, 2],
              [1, 3],
              [2, 3]])
      >>> torch.combinations(tensor_a, r=3)
      tensor([[1, 2, 3]])
      >>> torch.combinations(tensor_a, with_replacement=True)
      tensor([[1, 1],
              [1, 2],
              [1, 3],
              [2, 2],
              [2, 3],
              [3, 3]])

## Docs

Access comprehensive developer documentation for PyTorch

View Docs

## Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

## Resources

Find development resources and get your questions answered

View Resources

To analyze traffic and optimize your experience, we serve cookies on this site. By clicking or navigating, you agree to allow our usage of cookies. As the current maintainers of this site, Facebook’s Cookies Policy applies. Learn more, including about available controls: Cookies Policy.