为了将x_data和y_data转换成小批量数据并打乱数据,可以使用PyTorch中的DataLoader和random库。下面是一个示例代码:
import torch from torch.utils.data import DataLoader, TensorDataset x_data = torch.randn(10, 3) y_data = torch.randn(10, 1) for i, j in zip(x_data ,y_data): print(i, j)
输出
tensor([-1.3064, -1.1474, -0.4826]) tensor([-2.0181]) tensor([-0.7043, 0.4129, -0.7812]) tensor([0.2593]) tensor([ 0.8225, 0.4909, -0.9564]) tensor([0.1052]) tensor([ 0.8489, 0.7734, -0.5316]) tensor([-0.1681]) tensor([ 2.6069, 0.3360, -1.2510]) tensor([-1.5229]) tensor([-0.2588, 0.1903, -1.1847]) tensor([-0.1975]) tensor([-2.6685, 2.1388, -0.7719]) tensor([0.8189]) tensor([-0.4615, -1.3020, 0.9347]) tensor([0.1780]) tensor([-0.6927, -0.1758, 0.0818]) tensor([-0.4284]) tensor([-0.7713, 0.0360, 0.3797]) tensor([-0.4796])
# import random # 创建TensorDataset对象 dataset = TensorDataset(x_data, y_data) # 创建DataLoader对象,并指定batch_size和是否要进行打乱 dataloader = DataLoader(dataset, batch_size=2, shuffle=True) # 遍历每个小批量数据 for batch_x, batch_y in dataloader: # 在这里执行训练或评估操作 print(batch_x, batch_y)
输出
tensor([[ 0.8225, 0.4909, -0.9564], [-0.7043, 0.4129, -0.7812]]) tensor([[0.1052], [0.2593]]) tensor([[-0.7713, 0.0360, 0.3797], [-2.6685, 2.1388, -0.7719]]) tensor([[-0.4796], [ 0.8189]]) tensor([[-0.2588, 0.1903, -1.1847], [ 0.8489, 0.7734, -0.5316]]) tensor([[-0.1975], [-0.1681]]) tensor([[-0.4615, -1.3020, 0.9347], [-0.6927, -0.1758, 0.0818]]) tensor([[ 0.1780], [-0.4284]]) tensor([[ 2.6069, 0.3360, -1.2510], [-1.3064, -1.1474, -0.4826]]) tensor([[-1.5229], [-2.0181]])
首先,使用TensorDataset将x_data和y_data合并到一个数据集中。然后,使用DataLoader创建一个迭代器,以便逐个处理每个小批量数据。在这里,batch_size设置为2,这意味着每个小批量将包含2个样本。shuffle参数设置为True,表示要对数据进行随机打乱。在遍历每个小批量数据时,可以在循环体内执行训练或评估操作。