一般来说,神经网络模型实验重复多次,但最终结果仍然有较大波动时,应该从三个方面结果:种子、初始点、优化器。

1.种子

其中种子的固定尤为关键,应在代码中加入下列种子固定代码

def setup_seed(seed):
    random.seed(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)
    torch.backends.cudnn.deterministic = True
    torch.backends.cudnn.benchmark = False
    dgl.seed(seed)
setup_seed(seed)

2.初始化

可以使用xavier正态分布

nn.init.xavier_normal_(self.fc_src.weight, gain=gain)

也可以使用kaiming均匀分布

 nn.init.kaiming_uniform_(self.fc_src.weight, a=self.negative_slope)

3.优化器

使用Adam优化器,固定学习学率lr和weight_decay

optimizer = torch.optim.Adam(model.parameters(),lr=args.lr,weight_decay=args.weight_decay)

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐