具体的步骤
- 首先对数据进行处理(这两个应该放在util中)
- 构建dataset
--Init-- :初始化参数
--Len--:数据长度
--Getitem--:根据索引取数据(索引和len有关)返回对应的数据
- 构建dataloader
生成的是一个迭代器,用于小批量运算
自定义collate_fn函数可以设置dataloader返回的数据
train_dataloader = DataLoader(train_dataset, batch_size=64, shuffle=True,collate_fn=collate_fn)
这里可以将训练集和测试机合成的整个数据集放入 设置sampler来划分训练集和测试集,设置了sampler 那么shuffle自动失效
indices = list(range(dataset_size))
split = int(np.floor(validation_split * dataset_size))
train_indices, val_indices = indices[split:], indices[:split]
train_sampler = SubsetRandomSampler(train_indices)
valid_sampler = SubsetRandomSampler(val_indices)
training_generator = data.DataLoader(train_dataset, **params,
sampler=train_sampler)
val_generator = data.DataLoader(train_dataset, **params,
sampler=valid_sampler)
设置种子
def seed_torch(seed=2):
seed = int(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic =True
运行部分:
这里小数据可以使用交叉验证的方式
分一部分训练模式:
训练
print('Plan to train {} epoches \n'.format(epochs))
for epoch in range(epochs):
# mini-batch for training
train_loss_list = []
train_acc_list = []
model.train()
for step, (input_nodes, seeds, blocks) in enumerate(train_dataloader):
# forward
batch_inputs, batch_labels = load_subtensor(node_feat, labels, seeds, input_nodes, device_id)
blocks = [block.to(device_id) for block in blocks]
# metric and loss
train_batch_logits = model(blocks, batch_inputs)
batch_labels=th.tensor(batch_labels,dtype=th.long)
train_loss = loss_fn(train_batch_logits, batch_labels)
# backward
optimizer.zero_grad()
train_loss.backward()
optimizer.step()
train_loss_list.append(train_loss.cpu().detach().numpy())
tr_batch_pred = th.sum(th.argmax(train_batch_logits, dim=1) == batch_labels) / th.tensor(
batch_labels.shape[0])
if step % 10 == 0:
print('In epoch:{:03d}|batch:{:04d}, train_loss:{:4f}, train_acc:{:.4f}'.format(epoch,
step,
np.mean(
train_loss_list),
tr_batch_pred.detach()))
# mini-batch for validation
val_loss_list = []
val_acc_list = []
model.eval()
测试
model.eval() # 测试模式,不会更新参数 不使用dropout等
with torch.no_grad(): # 不计算梯度加快计算
交叉验证版本:
# 根据id选择数据
class get_Dataset():
def __init__(self,idx,all_data): # 设置初始信息
self.seg_data=all_data[idx]
def __len__(self): # 返回长度
return len(self.seg_data)
def __getitem__(self, item): # 根据item返回数据
return self.seg_data[item]
skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=random_seed)
for fold, (train_index, valid_index) in enumerate(skf.split(all_label, all_label)):
training_data=get_dataset(train_index,train_data)
train_dataloader = DataLoader(training_data, batch_size=64, shuffle=True)
validing_data=get_dataset(valid_index,train_data)
valid_dataloader = DataLoader(validing_data, batch_size=64, shuffle=True)
在GPU下运行
网络模型
数据(只有数据需要赋值)
损失函数
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model.to(device ) #这就放到GPU上运行了