Очень наивная реализация может быть
y = torch.randn(5, 5)
x = torch.zeros((9, 3, 3))
count = 0
for i in range(3) :
for j in range(3) :
x[count] = y[i : i + 3, j : j + 3]
count += 1
Пример вывода:
y = tensor([[ 0.0361, -0.4931, -1.1977, -0.5224, -3.4067],
[ 0.2380, -1.1042, -0.0696, -2.0487, -0.4123],
[ 0.6567, -0.2485, -0.3954, -0.8197, -0.4903],
[ 1.0073, 1.4759, 0.3532, 0.3565, -1.5257],
[-0.8493, -0.0532, 1.0918, 1.2715, -0.1775]])
x = tensor([[[ 0.0361, -0.4931, -1.1977],
[ 0.2380, -1.1042, -0.0696],
[ 0.6567, -0.2485, -0.3954]],
[[-0.4931, -1.1977, -0.5224],
[-1.1042, -0.0696, -2.0487],
[-0.2485, -0.3954, -0.8197]],
[[-1.1977, -0.5224, -3.4067],
[-0.0696, -2.0487, -0.4123],
[-0.3954, -0.8197, -0.4903]],
[[ 0.2380, -1.1042, -0.0696],
[ 0.6567, -0.2485, -0.3954],
[ 1.0073, 1.4759, 0.3532]],
[[-1.1042, -0.0696, -2.0487],
[-0.2485, -0.3954, -0.8197],
[ 1.4759, 0.3532, 0.3565]],
[[-0.0696, -2.0487, -0.4123],
[-0.3954, -0.8197, -0.4903],
[ 0.3532, 0.3565, -1.5257]],
[[ 0.6567, -0.2485, -0.3954],
[ 1.0073, 1.4759, 0.3532],
[-0.8493, -0.0532, 1.0918]],
[[-0.2485, -0.3954, -0.8197],
[ 1.4759, 0.3532, 0.3565],
[-0.0532, 1.0918, 1.2715]],
[[-0.3954, -0.8197, -0.4903],
[ 0.3532, 0.3565, -1.5257],
[ 1.0918, 1.2715, -0.1775]]])