- 训练集测试集划分固定
# 通过设置random_state设置固定比例
x_train, x_test, y_train, y_test = train_test_split(x_test, y, test_size = 0.1, random_state=seed)
- torch中的随机种子
# cpu
torch.manual_seed(seed)
# gpu
torch.cuda.manual_seed_all(seed) # 通过设置这个,保证每次运行按次序生成的随机数一样。·
#DataLoader的shuffle每一次都是一样的
-
numpy中随机种子的设置
-
综合
def setup_seed(seed):
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
torch.backends.cudnn.deterministic = True
- 实验结果