1. dataloader查看迭代数据
phase_dataset = PhaseDataset(transforms=T.ToTensor())
phase_loader = DataLoader(phase_dataset, batch_size=1)
dataiter = iter(phase_loader)
t_data, g_data = next(dataiter)
t_data, g_data = next(dataiter)
2. tensor变array
numpy.array(Hor_tensor)
3.数据集划分为训练集和验证集
已知所有数据在一个文件夹内,先用Dataset获取所有文件,再用random_spilt划分
phase_dataset = PhaseDataset(transforms=T.ToTensor())
train_dataset, val_dataset = random_split(phase_dataset, [round(0.8 * len(phase_dataset)), round(0.2 * len(phase_dataset))],
generator=torch.Generator().manual_seed(42))
train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True, num_workers=0)
val_loader = DataLoader(val_dataset, batch_size=batch_size, shuffle=True, num_workers=0)
参考代码
import torch
from torch.utils.data import random_split
dataset = range(10)
train_dataset, test_dataset = random_split(
dataset=dataset,
lengths=[7, 3],
generator=torch.Generator().manual_seed(0)
)
print(list(train_dataset))
print(list(test_dataset))
4.pytorch 的weight数据类型为float32,输入数据需和其保持一致
(1)三维初始化数据,各个维度数据经数分别进行据强制转换,无效; (2)作为一个整体转换,有效
g_data = np.empty([512, 512, 2])
g_data[:, :, 1] = np.array(t_data, dtype='float32')
g_data[:, :, 0] = np.array(t_data, dtype='float32')
c_data = np.array(g_data, dtype='float32')
c_data = g_data.astype(np.float32)
|