paddle.fluid.layers.nn. temporal_shift ( x, seg_num, shift_ratio=0.25, name=None, data_format='NCHW' ) [source]

Temporal Shift Operator

This operator calculates the temporal shifting features for Input(X).

Input(X) should be in shape of [N*T, C, H, W] or [N*T, H, W, C], while N is the batch size, T is the temporal segment number specified by seg_num, C is the channel number, H and W is the height and width of features.

Temporal Shifting is calculated as follows when data format is NCHW:

Step 1: Reshape Input(X) to [N, T, C, H, W].

Step 2: Pad 0 to reshaping result in the 2nd(T) dimension with padding width as 1 on each side, padding result will be in shape of [N, T+2, C, H, W].

Step 3: Assume shift_ratio is \(1/4\), slice padding result as follows:

$$ slice1 = x[:, :T, :C/4, :, :] $$ $$ slice2 = x[:, 2:T+2, C/4:C/2, :, :] $$ $$ slice3 = x[:, 1:T+1, C/2:, :, :] $$

Step 4: Concatenate three slices along the 3rd(C) dimension and reshape result to [N*T, C, H, W].

For details of temporal shifting, please refer to paper: Temporal Shift Module .

  • x (Tensor) – The input tensor of temporal shift operator. This is a 4-D tensor with shape of [N*T, C, H, W] or [N*T, H, W, C]. While N is the batch size, T is the temporal segment number, C is the channel number, H is the height of features and W is the width of features. The data type is float32 and float64

  • seg_num (int) – The temporal segment number, this should be a positive integer

  • shift_ratio (float) – The shift ratio of the channels, the first shift_ratio part of channels will be shifted by -1 along the temporal dimension, and the second shift_ratio part of channels will be shifted by 1 along the temporal dimension. shift_ratio should be in range [0, 0.5]. Default 0.25

  • name (str, optional) – For detailed information, please refer to Name. Usually name is no need to set and None by default.

  • data_format (str, optional) – Data format that specifies the layout of input. It can be “NCHW” or “NHWC”. Default: “NCHW”.


The temporal shifting result is a tensor with the same shape and same data type as the input.

Return type



TypeError – seg_num must be int type.


import paddle
import paddle.nn.functional as F

input = paddle.randn([6, 4, 2, 2])
out = F.temporal_shift(x=input, seg_num=2, shift_ratio=0.2)