SoFunction
Updated on 2024-11-12

Pytorch learning torch usage ---- Comparison Ops

1. (input, other, out=None)

Description: Compare elements for equality, the second argument can be a number, or a tensor of the same type and shape as the first argument.

Parameters:

input(Tensor) ---- tensor to be compared

other(Tenosr or float) ---- Compare tensor or number

out(Tensor, optional) ---- Output tensor

Return Value: A tensor containing the comparison results for each position (1 for equal, 0 for unequal)

>>> a = ([[1, 2], [3, 4]])
>>> b = ([[1, 1], [4, 4]])
>>> (a, b)
tensor([[1, 0],
  [0, 1]], dtype=torch.uint8)

2. (tensor1, tensor2, out=None)

Description: Returns true if the two tensors have the same shape and element values, otherwise False.

Parameters:

tensor1(Tenosr) ---- Compare tensor1

tensor2(Tensor) ---- Compare tensor2

out(Tensor, optional) ---- Output tensor

>>> a = ([1, 2])
>>> b = ([1, 2])
>>> (a, b)
True

3. (input, other, out=None)

Description: Compare input and other on an element-by-element basis, i.e. whether input >= other.

Parameters:

input(Tensor) ---- Tensor to be compared

other(Tensor or float) ---- Contrasting tensor or float value

out(Tensor, optional) ---- outputs the tensor.

>>> a = ([[1, 2], [3, 4]])
>>> b = ([[1, 1], [4, 4]])
>>> (a, b)
tensor([[1, 1],
  [0, 1]], dtype=torch.uint8)

4. (input, other, out=None)

Description: Compare input and other element by element, i.e. whether input > other.

Parameters:

input(Tensor) ---- tensor to compare

other(Tensor or float) ---- Tensor or float values to compare

out(Tensor, optional) ---- Output tensor

>>> a = ([[1, 2], [3, 4]])
>>> b = ([[1, 1], [4, 4]])
>>> (a, b)
tensor([[0, 1],
  [0, 0]], dtype=torch.uint8)

5. (input, k, dim=None, out=None)

Description: Takes the kth smallest value on the dimension specified by the input tensor input. If dim is not specified, it defaults to the last dimension. Returns a tuple (value, indices), where indices is the subscript of the kth smallest value along the dim dimension of the original input tensor.

Parameters:

input(Tensor) ---- tensor to compare

k(int) ---- kth smallest value

dim(int, optional) ---- Sort along this dimension

out(tuple, optional) ---- output tuple

>>> x = (1, 6)
>>> x
tensor([1, 2, 3, 4, 5])
>>> (x, 4)
torch.return_types.kthvalue(
values=tensor(4),
indices=tensor(3))
>>> (x, 1)
torch.return_types.kthvalue(
values=tensor(1),
indices=tensor(0))

6. (input, other, out=None)

Description: Compare input and other element by element, i.e. whether input <= other.

Parameters:

input(Tenosr) ---- Tensor to be compared

other(Tensor or float) ---- Comparative tensor or float value

out(Tensor, optional) ---- Output tensor

>>> a = ([[1, 2], [3, 4]])
>>> b = ([[1, 1], [4, 4]])
>>> (a, b)
tensor([[1, 0],
  [1, 1]], dtype=torch.uint8)

7. (input, other, out=None)

Description: Compare input and other element by element, i.e., whether input < other

Parameters:

input(Tensor) ---- tensor to compare

other(Tensor or float) ---- Comparative tensor or float value

out(Tensor, optional) ---- Output tensor

>>> a = ([[1, 2], [3, 4]])
>>> b = ([[1, 1], [4, 4]])
>>> (a, b)
tensor([[0, 0],
  [1, 0]], dtype=torch.uint8)

8. (input)

Description: Returns the maximum value of all elements of the input tensor.

Parameters:

input(Tensor) ---- input tensor

>>> a = (1, 3)
>>> a
tensor([[ 0.1553, -0.4140, 1.8393]])
>>> (a)
tensor(1.8393)

9. (input, dim, max=None, max_indices=None)

Description: Returns the maximum value of each row of the given dimension of the input tensor, along with the position index of each maximum value.

Parameters:

input(Tensor) ---- input tensor

dim(int) ---- Specified dimensions

max(Tensor, optional) ---- The result tensor, containing the maximum value in the given dimension

max_indices(LongTensor, optional) ---- The result tensor containing the index of the position of each maximum value on the given dimension.

>>> a = (4, 4)
>>> a
tensor([[ 0.4067, -0.7722, -0.6560, -0.9621],
  [-0.8754, 0.0282, -0.7947, -0.1870],
  [ 0.4300, 0.5444, 0.3180, 1.2647],
  [ 0.0775, 0.5886, 0.1662, 0.8986]])
>>> (a, 1)
torch.return_types.max(
values=tensor([0.4067, 0.0282, 1.2647, 0.8986]),
indices=tensor([0, 1, 3, 3]))

10. (input, other, out=None)

Description: Returns the maximum value of two elements.

Parameters:

input(Tensor) ---- tensor to be compared

other(Tensor) ---- Compare tensor

out(Tensor, optional) ---- Result tensor

>>> a = (4)
>>> a
tensor([ 0.5767, -1.0841, -0.0942, -0.9405])
>>> b = (4)
>>> b
tensor([-0.6375, 1.4165, 0.2738, -0.8996])
>>> (a, b)
tensor([ 0.5767, 1.4165, 0.2738, -0.8996])

(input)

Description: Returns the minimum value of all elements of the input tensor.

Parameters:

input(Tensor) ---- input tensor

>>> a = (1, 4)
>>> a
tensor([[-0.8142, -0.9847, -0.3637, 0.5191]])
>>> (a)
tensor(-0.9847)

12. (input, dim, min=None, min_indices=None)

Description: Returns the minimum value of each row of the given dimension of the input tensor, and also returns the position index of each minimum value.

Parameters:

input(Tensor) ---- input tensor

dim(int) ---- Specified dimension

min(Tensor, optional) ---- The result tensor, containing the minimum value on the given dimension

min_indices(LongTensor, optional) ---- The result tensor containing the positional indices of each minimum value in the given dimension.

>>> a = (4, 4)
>>> a
tensor([[-0.0243, -0.7382, 0.3102, 0.9720],
  [-0.3805, -0.7999, -1.2856, 0.2657],
  [-1.0284, -0.1638, -0.8840, 1.2679],
  [-1.0347, -2.3428, 0.3107, 1.0575]])
>>> (a, 1)
torch.return_types.min(
values=tensor([-0.7382, -1.2856, -1.0284, -2.3428]),
indices=tensor([1, 2, 0, 1]))

13. (input, other, out=None)

Description: compares input and other element by element, i.e. if input is not equal to other. the second argument can be a number or a tensor of the same shape and type as the first argument.

Parameters:

input(Tensor) ---- Tensor to be compared

other(Tensor or float) ---- Comparative tensor or float value

out(Tensor, optional) ---- Output Tensor

** Return Value:** A tensor containing the comparison results for each position, if tensor and other are not equal True, return 1.

>>> import torch
>>> a = ([[1, 2], [3, 4]])
>>> b = ([[1, 1], [4, 4]])
>>> (a, b)
tensor([[0, 1],
  [1, 0]], dtype=torch.uint8)

14. (input, dim=None, descending=False, out=None)

Description: Sorts the input tensor input in ascending order along the specified dimensions, defaulting to the last dimension of the input if dim is not given. If the parameter descending is given as True, the input is sorted in descending order.

Parameters:

input(Tensor) ---- Tensor to be sorted

dim(int, optional) ---- Sorting along this dimension

descending(bool, optional) ---- Boolean value to control ascending sorting

out(tuple, optional) ---- Output tensor

Return value: of type ByteTensor or the same type as tensor, a tuple (sorted_tensor,sorted_indices), sorted_indices is the subscript in the original input

>>> x = (3, 4)
>>> x
tensor([[-0.3613, -0.2583, -0.4276, -1.3106],
  [-1.1577, -0.7505, 1.7217, -0.6247],
  [-0.1338, 0.4423, 0.0280, -1.4796]])
>>> sorted, indices = (x)
>>> sorted
tensor([[-1.3106, -0.4276, -0.3613, -0.2583],
  [-1.1577, -0.7505, -0.6247, 1.7217],
  [-1.4796, -0.1338, 0.0280, 0.4423]])
>>> indices
tensor([[3, 2, 0, 1],
  [0, 1, 3, 2],
  [3, 0, 2, 1]])

15. (input, dim=None, largest=True, sorted=True, out=None)

Description: Returns the k largest values in the input tensor input along the specified dim dimension. If dim is not specified, it defaults to the last dimension of the input, and if largest is False, the smallest k values are returned.

Parameters:

input(Tensor) ---- input tensor

k(int) ---- Value of k in "top-k"

dim(int, optional) ---- Dimension of sorting

largest(bool, optional) ---- boolean value, control to return the maximum or minimum value

sorted(bool, optional) ---- boolean, control whether the return value is sorted or not

out(tuple, optional) ---- Optional output tensor

Return Value: Returns a tuple (values, indices), where indices are the subscripts of the sorted elements in the original input tensor input. Setting the boolean sorted to True will ensure that the k values returned are sorted.

>>> x = (1, 6)
>>> x
tensor([1, 2, 3, 4, 5])
>>> (x, 3)
torch.return_types.topk(
values=tensor([5, 4, 3]),
indices=tensor([4, 3, 2]))
>>> (x, 3, 0, largest=False)
torch.return_types.topk(
values=tensor([1, 2, 3]),
indices=tensor([0, 1, 2]))

Above this Pytorch learning of torch usage ---- Comparison Ops (Comparison Ops) is all I have to share with you, I hope to be able to give you a reference, and I hope you will support me more.