Given a 2D tensor

```
T = [[1, 2, 3]
[4, 5, 6]]
```

and a 1D tensor containing horizontal shifts, say, `s = [0, -2, 1]`

, how can I obtain the following 3D tensor `R`

?

```
R[0] = T
R[1] = [[3, 0, 0], # shifted two to the left,
[6, 0, 0]] # padding the rest with zeros
R[2] = [[0, 1, 2], # shifted one to the right,
[0, 4, 5]] # padding the rest with zeros
```

I know about `tf.contrib.image.translate`

, but that isn't differentiable, so I am looking for some elegant combination of padding/slicing/looping/concatenating operations that accomplishes the same thing.

I have only come up with two ways to use

`tf.map_fn()`

. The first method is to fill about 0 in`T`

and slice it.The second method is to compute a corresponding mask matrix by

`tf.sequence_mask`

and`tf.roll()`

.Then take the value by`tf.where()`

.UpdateI found new method to achieve it. In essence, horizontal shifts are

`T`

multiplied by an offset identity matrix. So we can use`np.eye()`

to create factor.