nn.functional.interpolate
时间: 2023-11-23 18:07:47 浏览: 152
`nn.functional.interpolate` is a function provided by PyTorch's `nn.functional` module. It is used to perform interpolation or resizing of input tensors.
Here is the syntax for `nn.functional.interpolate`:
```python
output = nn.functional.interpolate(input, size=None, scale_factor=None, mode='nearest', align_corners=None)
```
Parameters:
- `input`: The input tensor to be interpolated.
- `size`: The desired size of the output tensor. It can be a tuple `(H, W)` representing the height and width, or an integer representing both height and width.
- `scale_factor`: The scaling factor for resizing the input tensor.
- `mode`: The interpolation mode. It can be one of the following: `'nearest'`, `'linear'`, `'bilinear'`, `'bicubic'`, `'trilinear'`, or `'area'`. By default, it is set to `'nearest'`.
- `align_corners`: Optional parameter for corner alignment in interpolation. It is only applicable to the modes `'linear'`, `'bilinear'`, `'bicubic'`, and `'trilinear'`. By default, it is set to `None`.
The `nn.functional.interpolate` function returns the interpolated output tensor.
Here's an example usage of `nn.functional.interpolate`:
```python
import torch
import torch.nn.functional as F
input = torch.randn(1, 3, 32, 32) # Input tensor of shape (batch_size, channels, height, width)
resized_output = F.interpolate(input, size=(64, 64), mode='bilinear')
```
In this example, the `input` tensor is resized to a size of (64, 64) using bilinear interpolation. The resulting output tensor will have a shape of (1, 3, 64, 64).
阅读全文