python中chunk方法

chunk可以起到分块的作用:

import numpy as np
import torch

data = torch.from_numpy(np.random.rand(5, 10))
print(str(data))
for i, data_i in enumerate(data.chunk(data.size(1), dim = 1)): # 沿data的1轴方向每10个元素分为一块,并对生成的列表进行迭代。 
  print(str(data_i))

结果:

data =  

tensor([[0.6479, 0.6469, 0.8648, 0.7100, 0.3431, 0.3456, 0.2619, 0.8148, 0.1438, 0.1072],
            [0.6868, 0.3495, 0.4733, 0.1450, 0.1213, 0.6868, 0.6731, 0.5042, 0.8478, 0.4494],
            [0.7455, 0.2055, 0.0808, 0.3880, 0.5710, 0.5437, 0.4880, 0.7759, 0.7309, 0.0098],
            [0.8330, 0.5572, 0.7451, 0.9313, 0.3816, 0.0829, 0.9214, 0.1661, 0.2805, 0.4337],
            [0.7331, 0.8521, 0.6694, 0.9012, 0.1606, 0.1643, 0.3767, 0.9132, 0.7562, 0.4731]], dtype=torch.float64)

data_i = 

tensor([[0.6479],
[0.6868],
[0.7455],
[0.8330],
[0.7331]], dtype=torch.float64)
tensor([[0.6469],
[0.3495],
[0.2055],
[0.5572],
[0.8521]], dtype=torch.float64)
tensor([[0.8648],
[0.4733],
[0.0808],
[0.7451],
[0.6694]], dtype=torch.float64)
tensor([[0.7100],
[0.1450],
[0.3880],
[0.9313],
[0.9012]], dtype=torch.float64)
tensor([[0.3431],
[0.1213],
[0.5710],
[0.3816],
[0.1606]], dtype=torch.float64)
tensor([[0.3456],
[0.6868],
[0.5437],
[0.0829],
[0.1643]], dtype=torch.float64)
tensor([[0.2619],
[0.6731],
[0.4880],
[0.9214],
[0.3767]], dtype=torch.float64)
tensor([[0.8148],
[0.5042],
[0.7759],
[0.1661],
[0.9132]], dtype=torch.float64)
tensor([[0.1438],
[0.8478],
[0.7309],
[0.2805],
[0.7562]], dtype=torch.float64)
tensor([[0.1072],
[0.4494],
[0.0098],
[0.4337],
[0.4731]], dtype=torch.float64)

猜你喜欢

转载自www.cnblogs.com/jiangkejie/p/10309766.html
今日推荐