YouTip LogoYouTip

Pytorch Torch Bitwise_Xor

## PyTorch `torch.bitwise_xor` Reference `torch.bitwise_xor` is a PyTorch function used to compute the element-wise bitwise XOR (exclusive OR) of two input tensors. This function is highly useful for low-level binary data manipulation, masking operations, and working with boolean logic in deep learning and scientific computing. --- ## Syntax and Parameters ### Function Signature ```python torch.bitwise_xor(input, other, *, out=None) -> Tensor ``` ### Parameters * **`input` (Tensor or Scalar)**: The first input tensor. It must contain integer or boolean types. * **`other` (Tensor or Scalar)**: The second input tensor. It must also contain integer or boolean types. If its shape differs from `input`, it must be broadcastable to a common shape. * **`out` (Tensor, optional)**: The output tensor where the result will be stored. ### Supported Data Types * **Integer types**: `torch.int8`, `torch.uint8`, `torch.int16`, `torch.int32`, `torch.int64`. * **Boolean type**: `torch.bool` (acts as a logical XOR). --- ## How Bitwise XOR Works For integer types, the operation is performed on their binary representations. The bitwise XOR operator returns `1` if the corresponding bits of the two operands are different, and `0` if they are the same: | Bit A | Bit B | A XOR B | | :---: | :---: | :-----: | | 0 | 0 | 0 | | 0 | 1 | 1 | | 1 | 0 | 1 | | 1 | 1 | 0 | --- ## Code Examples ### Example 1: Bitwise XOR with Integer Tensors This example demonstrates how to perform a bitwise XOR operation on two 1D integer tensors. ```python import torch # Define two 1D integer tensors x = torch.tensor([5, 3, 7], dtype=torch.int8) y = torch.tensor([1, 3, 4], dtype=torch.int8) # Perform bitwise XOR result = torch.bitwise_xor(x, y) print("Input x: ", x) print("Input y: ", y) print("Result: ", result) # Output: tensor([4, 0, 3], dtype=int8) ``` #### Binary Explanation of the Example: * **`5` XOR `1`**: * `5` in binary: `0101` * `1` in binary: `0001` * `0101` $\oplus$ `0001` = `0100` (which is **`4`** in decimal) * **`3` XOR `3`**: * `3` in binary: `0011` * `3` in binary: `0011` * `0011` $\oplus$ `0011` = `0000` (which is **`0`** in decimal) * **`7` XOR `4`**: * `7` in binary: `0111` * `4` in binary: `0100` * `0111` $\oplus$ `0100` = `0011` (which is **`3`** in decimal) --- ### Example 2: Logical XOR with Boolean Tensors When applied to boolean tensors, `torch.bitwise_xor` behaves as a standard logical XOR operation. ```python import torch # Define two boolean tensors bool_a = torch.tensor([True, True, False, False], dtype=torch.bool) bool_b = torch.tensor([True, False, True, False], dtype=torch.bool) # Perform logical XOR bool_result = torch.bitwise_xor(bool_a, bool_b) print("Result: ", bool_result) # Output: tensor([False, True, True, False]) ``` --- ### Example 3: Tensor and Scalar Broadcasting You can also perform a bitwise XOR between a tensor and a scalar value. PyTorch will automatically broadcast the scalar to match the shape of the tensor. ```python import torch # Define an integer tensor x = torch.tensor([1, 2, 3], dtype=torch.int32) # Perform bitwise XOR with a scalar value of 1 result = torch.bitwise_xor(x, 1) print("Result: ", result) # Output: tensor([0, 3, 2], dtype=int32) ``` --- ## Important Considerations 1. **In-place Operations**: You can perform the operation in-place using the syntax `input.bitwise_xor_(other)` or by using the shorthand operator `^=`. 2. **Floating-point Types**: `torch.bitwise_xor` is **not** supported for floating-point tensors (such as `torch.float32` or `torch.float64`). Attempting to use them will raise a `RuntimeError`. 3. **Broadcasting**: If the input tensors have different shapes, PyTorch will attempt to broadcast them to a compatible shape following standard NumPy-style broadcasting rules.
← Pytorch Torch Broadcast_ShapesPytorch Torch Bitwise_Or β†’