interpolate(inp, size=None, scale_factor=None, mode='bilinear', align_corners=None)[源代码]

按照给定的 sizescale_factor 对输入张量进行向下/向上采样。size 不可以与 scale_factor 共存。

  • inp (Tensor) – 输入张量。

  • size (Union[int, Tuple[int, int], None]) – 输出张量的形状。 默认:None

  • scale_factor (Union[float, Tuple[float, float], None]) – 输出张量的放缩参数。默认:None

  • mode (str) – interpolation methods, acceptable values are: “bilinear”, “linear”, “bicubic” and “nearest”. Default: “bilinear”

  • align_corners (Optional[bool]) – This only has an effect when mode is “bilinear” or “linear”. Geometrically, we consider the pixels of the input and output as squares rather than points. If set to True, the input and output tensors are aligned by the center points of their corner pixels, preserving the values at the corner pixels. If set to False, the input and output tensors are aligned by the corner points of their corner pixels, and the interpolation uses edge value padding for out-of-boundary values, making this operation independent of input size






import numpy as np
from megengine import tensor
import megengine.functional as F

x = tensor(np.arange(1, 5, dtype=np.float32).reshape(1, 1, 2, 2))
out =, [4, 4], align_corners=False)
out2 =, scale_factor=2.)
np.testing.assert_allclose(out.numpy(), out2.numpy())


[[[[1.   1.25 1.75 2.  ]
   [1.5  1.75 2.25 2.5 ]
   [2.5  2.75 3.25 3.5 ]
   [3.   3.25 3.75 4.  ]]]]