1.介绍
pytorch并行处理是指将模型和相应的数据放在多个GPU机器上运行。
假如有4台GPU机器,那么会在每一个GPU机器上拷贝一个模型,然后把数据平均分成四份(若batch_size除不尽,会自动适配)
2.代码
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu") #获得所有GPU #对model做并行处理,在声明model的位置进行 self.model.to(self.device) self.model= torch.nn.DataParallel(self.model) #torch.nn.DataParallel会返回一个model,所有必须接收 #对相应data做并行处理,在调用model的地方进行 data = data.to(self.device) output=self.model(data)