How to effectively implement the following operations in pytorch?

For example, I would like to extract each 2 × 2 submatrix in a 3 × 3 matrix, converting them to column vector, then obtaining a new matrix. Like this:

1…2…3./././././././././1…2…4…5
4…5…6…------->…2…3…5…6
7…8…9./././././././././4…5…7…8
./././././././././././././././/5…6…8…9

and how to effectively implement the inv-process.
Thank you for your attention and answer.

You are looking for nn.Unfold (otherwise commonly known as im2col). See https://pytorch.org/docs/master/nn.html#torch.nn.Unfold.

The functional form also exists and is at torch.nn.functional.unfold.

1 Like

The inverse is nn.Fold

2 Likes

Thank you very much, you’ve been a great help to me.

Hi, it seems that this function only exist in pytorch_0.5 which is an unstable version. So, have you been used this version, or is there a good solution to this problem under pytorch_0.4?

They are actually in 0.4.0! But we forgot to document it, and added the doc reference in 0.5. :smiley:

See: https://github.com/pytorch/pytorch/blob/v0.4.0/torch/nn/modules/fold.py

2 Likes

Oh, yes. I have been used it. Thank you!