pytorch squeeze
时间: 2023-10-16 13:59:52 浏览: 71
pytorch搭建的各种常见分类网络的工程,如googlenet、squeezenet、resnet等
PyTorch's `squeeze` function is used to remove dimensions of size 1 from a tensor. It returns a new tensor with the same data but with the specified dimensions removed. Here's an example:
```python
import torch
# Create a tensor with shape (1, 3, 1, 4)
x = torch.randn(1, 3, 1, 4)
print("Before squeeze:", x.shape)
# Squeeze the tensor
y = torch.squeeze(x)
print("After squeeze:", y.shape)
```
Output:
```
Before squeeze: torch.Size([1, 3, 1, 4])
After squeeze: torch.Size([3, 4])
```
In the example above, the `squeeze` function removes the dimensions of size 1 from the tensor `x`, resulting in a new tensor `y` with shape `(3, 4)`.
阅读全文