本文主要是介绍额外干饭人东方红波尔多发吧合法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
#四层卷积神经网络模型结构代码
class Mnist_CNN(nn.Module):def __init__(self):super().__init__()self.conv1 = nn.Conv2d(in_channels=3, out_channels=32, kernel_size=5, stride=1, padding=2)self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2, padding=0)self.conv2 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=5, stride=1, padding=2)self.pool2 = nn.MaxPool2d(kernel_size=2, stride=2, padding=0)self.conv3 = nn.Conv2d(in_channels=64, out_channels=128, kernel_size=5, stride=1, padding=2)self.pool3 = nn.MaxPool2d(kernel_size=2, stride=2, padding=0)self.conv4 = nn.Conv2d(in_channels=128, out_channels=256, kernel_size=5, stride=1, padding=2)self.pool4 = nn.MaxPool2d(kernel_size=2, stride=2, padding=0)self.fc1 = nn.Linear(4*4*256, 512)self.fc2 = nn.Linear(512, 10)def forward(self, inputs):tensor = inputs.view(-1, 3, 64, 64)tensor = F.relu(self.conv1(tensor))tensor = self.pool1(tensor)tensor = F.relu(self.conv2(tensor))tensor = self.pool2(tensor)tensor = F.relu(self.conv3(tensor))tensor = self.pool3(tensor)tensor = F.relu(self.conv4(tensor))tensor = self.pool4(tensor)tensor = tensor.view(-1, 4*4*256)tensor = F.relu(self.fc1(tensor))tensor = self.fc2(tensor)return tensorclass client(object):def __init__(self, trainDataSet, dev):self.train_ds = trainDataSetself.dev = devself.train_dl = Noneself.local_parameters = Nonedef localUpdate(self, localEpoch, localBatchSize, Net, lossFun, opti, global_parameters):# 加载当前通信中最新全局参数,传入网络模型,并加载global_parameters参数的Net.load_state_dict(global_parameters, strict=True)# 载入Client自有数据集# 加载本地数据self.train_dl = DataLoader(self.train_ds, batch_size=localBatchSize, shuffle=True)# 设置迭代次数for epoch in range(localEpoch):for data, label in self.train_dl:# 加载到GPU上data, label = data.to(self.dev), label.to(self.dev)# 模型上传入数据preds = Net(data)# 计算损失函数loss = lossFun(preds, label.to(torch.int64))# 反向传播loss.backward()# 计算梯度,并更新梯度opti.step()# 将梯度归零,初始化梯度opti.zero_grad()# 返回当前Client基于自己的数据训练得到的新的模型参数return Net.state_dict()def local_val(self):pass# 对所有的Client返回的参数累加(最后取平均值)if sum_parameters is None:sum_parameters = {}for key, var in local_parameters.items():sum_parameters[key] = var.clone()else:for var in sum_parameters:sum_parameters[var] = sum_parameters[var] + local_parameters[var]# 取平均值,得到本次通信中Server得到的更新后的模型参数for var in global_parameters:global_parameters[var] = (sum_parameters[var] / num_in_comm)# 定义损失函数loss_func = F.cross_entropy# 优化算法的,随机梯度下降法,使用Adam下降法opti = optim.Adam(net.parameters(), lr=args['learning_rate'])'''训练结束之后,我们要通过测试集来验证方法的泛化性,虽然训练时,Server没有得到过任何一条数据,但是联邦学习最终的目的还是要在Server端学习到一个鲁棒的模型,所以在做测试的时候,是在Server端进行的'''# 加载Server在最后得到的模型参数with torch.no_grad():net.load_state_dict(global_parameters, strict=True)sum_accu = 0lossall = 0num = 0# 载入测试集for data, label in tqdm(testDataLoader):data, label = data.to(dev), label.to(dev)preds = net(data)loss = loss_func(preds, label.to(torch.int64))preds = torch.argmax(preds, dim=1)sum_accu += (preds == label).float().mean()lossall += lossnum += 1print("\n"+'accuracy: {}'.format(sum_accu / num) +'loss: {}'.format(lossall / num))losslist.append(lossall / num)acclist.append(sum_accu / num)if (i + 1) % args['save_freq'] == 0:torch.save(net, './checkpoints/net_convid_model.h5') # 保存完整模型torch.save(net.state_dict(), os.path.join(args['save_path'],'{}_num_comm{}_E{}_B{}_lr{}_num_clients{}_cf{}'.format(args['model_name'],i, args['epoch'],args['batchsize'],args['learning_rate'],args['num_of_clients'],args['cfraction'])))# 这里的clients_for client in tqdm(clients_in_comm):# local_parameters 得到客户端的局部变量local_parameters = myClients.clients_set[client].localUpdate(args['epoch'], args['batchsize'], net, loss_func, opti, global_parameters)
这篇关于额外干饭人东方红波尔多发吧合法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!