Convert pytorch tensor to numpy, and reshape
I have a pytorch tensor [100, 1, 32, 32] corresponding to batch size of 100 images, 1 channel, height 32 and width 32. I want to reshape this tensor to have dimension [32*10, 32*10], such that the images are represented as a 10x10 grid, with the first 10 images on row 1, and so on. How to achieve this? 

Update

More efficient and shorter version. To avoid using for-loop, we can permute a first.

import torch
a = torch.arange(9*2*2).view(9,1,2,2)
b = a.permute([0,1,3,2])
torch.cat(torch.split(b, 3),-1).view(6,6).t()
# tensor([[ 0,  1,  4,  5,  8,  9],
#         [ 2,  3,  6,  7, 10, 11],
#         [12, 13, 16, 17, 20, 21],
#         [14, 15, 18, 19, 22, 23],
#         [24, 25, 28, 29, 32, 33],
#         [26, 27, 30, 31, 34, 35]])


Original Answer

You can use torch.split and torch.cat to implement it. 

import torch
a = torch.arange(9*2*2).view(9,1,2,2)


Assuming we have a tensor, which is a mini version of your original tensor. And it looks like,

tensor([[[[ 0,  1],
          [ 2,  3]]],
        [[[ 4,  5],
          [ 6,  7]]],
        [[[ 8,  9],
          [10, 11]]],
        [[[12, 13],
          [14, 15]]],
        [[[16, 17],
          [18, 19]]],
        [[[20, 21],
          [22, 23]]],
        [[[24, 25],
          [26, 27]]],
        [[[28, 29],
          [30, 31]]],
        [[[32, 33],
          [34, 35]]]])


Each 2x2 sub-matrix can be seen as one image. What you want to do is stacking the first three images to one row, next three images to the second row, and last three images to the third row. The "row" has actually two dim due to the 2x2 sub-matrix. 

three_parts = torch.split(a,3)

torch.cat(torch.split(three_parts[0],1), dim=-1)

#tensor([[[[ 0,  1,  4,  5,  8,  9],
#          [ 2,  3,  6,  7, 10, 11]]]])


Here we only take the first part. 

torch.cat([torch.cat(torch.split(three_parts[i],1),-1) for i in range(3)],0).view(6,6)
# tensor([[ 0,  1,  4,  5,  8,  9],
#         [ 2,  3,  6,  7, 10, 11],
#         [12, 13, 16, 17, 20, 21],
#         [14, 15, 18, 19, 22, 23],
#         [24, 25, 28, 29, 32, 33],
#         [26, 27, 30, 31, 34, 35]])

