让模型训练不受随机干扰

除了

np.random.seed(seed)
random.seed(seed)
torch.manual_seed(seed) # cpu
torch.cuda.manual_seed_all(seed) # gpu
torch.backends.cudnn.enabled = True # 默认值
torch.backends.cudnn.benchmark = True # 默认为False
torch.backends.cudnn.deterministic = True # 默认为False

还要注意,把下面的num_works设置为0, 这样训练所使用的样本也一样了。

self.dataloader = data.DataLoader(
dataset,
batch_size=args.batch,
sampler=data_sampler(dataset, shuffle=True, distributed=False),
drop_last=True,
num_workers=0
)

原文地址:https://www.cnblogs.com/rabitvision/p/13672790.html