pytorch broadcast
时间: 2023-10-19 09:33:06 浏览: 110
PyTorch's `broadcast` operation is used to automatically expand tensors with different shapes to perform element-wise operations. It allows tensors to be compatible for operations where the shapes would otherwise not match. This operation is similar to NumPy's broadcasting.
For example, let's say you have two tensors, `tensor1` with shape (3, 1) and `tensor2` with shape (1, 4). If you try to add these tensors directly using the `+` operator, it will result in a size mismatch error. But with broadcasting, PyTorch will automatically expand the tensors to (3, 4) by duplicating elements along the appropriate dimensions, allowing for element-wise addition.
Here's an example of how to use broadcasting in PyTorch:
```python
import torch
tensor1 = torch.tensor([[1], [2], [3]])
tensor2 = torch.tensor([[4, 5, 6, 7]])
result = tensor1 + tensor2
print(result)
```
Output:
```
tensor([[5, 6, 7, 8],
[6, 7, 8, 9],
[7, 8, 9, 10]])
```
In this example, the `tensor1` with shape (3, 1) is broadcasted to (3, 4), and `tensor2` with shape (1, 4) is broadcasted to (3, 4) before performing the element-wise addition.
阅读全文