当前位置: 首页 > news >正文

wordpress的登录地址修改密码天津seo网站推广

wordpress的登录地址修改密码,天津seo网站推广,做头像的网站自己的名字,做太空泥的几分钟网站目录 1. 联邦学习介绍 2. 实验流程 3. 数据加载 4. 模型构建 5. 数据采样函数 6. 模型训练 1. 联邦学习介绍 联邦学习是一种分布式机器学习方法,中心节点为server(服务器),各分支节点为本地的client(设备&#…

目录

1. 联邦学习介绍

2. 实验流程

3. 数据加载

4. 模型构建

5. 数据采样函数

6. 模型训练


1. 联邦学习介绍

联邦学习是一种分布式机器学习方法,中心节点为server(服务器),各分支节点为本地的client(设备)。联邦学习的模式是在各分支节点分别利用本地数据训练模型,再将训练好的模型汇合到中心节点,获得一个更好的全局模型。

联邦学习的提出是为了充分利用用户的数据特征训练效果更佳的模型,同时,为了保证隐私,联邦学习在训练过程中,server和clients之间通信的是模型的参数(或梯度、参数更新量),本地的数据不会上传到服务器。

本项目主要是升级1.8版本的联邦学习fedavg算法至2.3版本,内容取材于基于PaddlePaddle实现联邦学习算法FedAvg - 飞桨AI Studio星河社区

2. 实验流程

联邦学习的基本流程是:

1. server初始化模型参数,所有的clients将这个初始模型下载到本地;

2. clients利用本地产生的数据进行SGD训练;

3. 选取K个clients将训练得到的模型参数上传到server;

4. server对得到的模型参数整合,所有的clients下载新的模型。

5. 重复执行2-5,直至收敛或达到预期要求

import os
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import random
import time
import paddle
import paddle.nn as nn
import numpy as np
from paddle.io import Dataset,DataLoader
import paddle.nn.functional as F

3. 数据加载

mnist_data_train=np.load('data/data2489/train_mnist.npy')
mnist_data_test=np.load('data/data2489/test_mnist.npy')
print('There are {} images for training'.format(len(mnist_data_train)))
print('There are {} images for testing'.format(len(mnist_data_test)))
# 数据和标签分离(便于后续处理)
Label=[int(i[0]) for i in mnist_data_train]
Data=[i[1:] for i in mnist_data_train]
There are 60000 images for training
There are 10000 images for testing

4. 模型构建

class CNN(nn.Layer):def __init__(self):super(CNN,self).__init__()self.conv1=nn.Conv2D(1,32,5)self.relu = nn.ReLU()self.pool1=nn.MaxPool2D(kernel_size=2,stride=2)self.conv2=nn.Conv2D(32,64,5)self.pool2=nn.MaxPool2D(kernel_size=2,stride=2)self.fc1=nn.Linear(1024,512)self.fc2=nn.Linear(512,10)# self.softmax = nn.Softmax()def forward(self,inputs):x = self.conv1(inputs)x = self.relu(x)x = self.pool1(x)x = self.conv2(x)x = self.relu(x)x = self.pool2(x)x=paddle.reshape(x,[-1,1024])x = self.relu(self.fc1(x))y = self.fc2(x)return y

5. 数据采样函数

# 均匀采样,分配到各个client的数据集都是IID且数量相等的
def IID(dataset, clients):num_items_per_client = int(len(dataset)/clients)client_dict = {}image_idxs = [i for i in range(len(dataset))]for i in range(clients):client_dict[i] = set(np.random.choice(image_idxs, num_items_per_client, replace=False)) # 为每个client随机选取数据image_idxs = list(set(image_idxs) - client_dict[i]) # 将已经选取过的数据去除client_dict[i] = list(client_dict[i])return client_dict
# 非均匀采样,同时各个client上的数据分布和数量都不同
def NonIID(dataset, clients, total_shards, shards_size, num_shards_per_client):shard_idxs = [i for i in range(total_shards)]client_dict = {i: np.array([], dtype='int64') for i in range(clients)}idxs = np.arange(len(dataset))data_labels = Labellabel_idxs = np.vstack((idxs, data_labels)) # 将标签和数据ID堆叠label_idxs = label_idxs[:, label_idxs[1,:].argsort()]idxs = label_idxs[0,:]for i in range(clients):rand_set = set(np.random.choice(shard_idxs, num_shards_per_client, replace=False)) shard_idxs = list(set(shard_idxs) - rand_set)for rand in rand_set:client_dict[i] = np.concatenate((client_dict[i], idxs[rand*shards_size:(rand+1)*shards_size]), axis=0) # 拼接return client_dict

class MNISTDataset(Dataset):def __init__(self, data,label):self.data = dataself.label = labeldef __getitem__(self, idx):image=np.array(self.data[idx]).astype('float32')image=np.reshape(image,[1,28,28])label=np.array(self.label[idx]).astype('int64')return image, labeldef __len__(self):return len(self.label)

6. 模型训练

class ClientUpdate(object):def __init__(self, data, label, batch_size, learning_rate, epochs):dataset = MNISTDataset(data,label)self.train_loader = DataLoader(dataset,batch_size=batch_size,shuffle=True,drop_last=True)self.learning_rate = learning_rateself.epochs = epochsdef train(self, model):optimizer=paddle.optimizer.SGD(learning_rate=self.learning_rate,parameters=model.parameters())criterion = nn.CrossEntropyLoss(reduction='mean')model.train()e_loss = []for epoch in range(1,self.epochs+1):train_loss = []for image,label in self.train_loader:# image=paddle.to_tensor(image)# label=paddle.to_tensor(label.reshape([label.shape[0],1]))output=model(image)loss= criterion(output,label)# print(loss)loss.backward()optimizer.step()optimizer.clear_grad()train_loss.append(loss.numpy()[0])t_loss=sum(train_loss)/len(train_loss)e_loss.append(t_loss)total_loss=sum(e_loss)/len(e_loss)return model.state_dict(), total_loss

train_x = np.array(Data)
train_y = np.array(Label)
BATCH_SIZE = 32
# 通信轮数
rounds = 100
# client比例
C = 0.1
# clients数量
K = 100
# 每次通信在本地训练的epoch
E = 5
# batch size
batch_size = 10
# 学习率
lr=0.001
# 数据切分
iid_dict = IID(mnist_data_train, 100)
def training(model, rounds, batch_size, lr, ds,L, data_dict, C, K, E, plt_title, plt_color):global_weights = model.state_dict()train_loss = []start = time.time()# clients与server之间通信for curr_round in range(1, rounds+1):w, local_loss = [], []m = max(int(C*K), 1) # 随机选取参与更新的clientsS_t = np.random.choice(range(K), m, replace=False)for k in S_t:# print(data_dict[k])sub_data = ds[data_dict[k]]sub_y = L[data_dict[k]]local_update = ClientUpdate(sub_data,sub_y, batch_size=batch_size, learning_rate=lr, epochs=E)weights, loss = local_update.train(model)w.append(weights)local_loss.append(loss)# 更新global weightsweights_avg = w[0]for k in weights_avg.keys():for i in range(1, len(w)):# weights_avg[k] += (num[i]/sum(num))*w[i][k]weights_avg[k]=weights_avg[k]+w[i][k]   weights_avg[k]=weights_avg[k]/len(w)global_weights[k].set_value(weights_avg[k])# global_weights = weights_avg# print(global_weights)#模型加载最新的参数model.load_dict(global_weights)loss_avg = sum(local_loss) / len(local_loss)if curr_round % 10 == 0:print('Round: {}... \tAverage Loss: {}'.format(curr_round, np.round(loss_avg, 5)))train_loss.append(loss_avg)end = time.time()fig, ax = plt.subplots()x_axis = np.arange(1, rounds+1)y_axis = np.array(train_loss)ax.plot(x_axis, y_axis, 'tab:'+plt_color)ax.set(xlabel='Number of Rounds', ylabel='Train Loss',title=plt_title)ax.grid()fig.savefig(plt_title+'.jpg', format='jpg')print("Training Done!")print("Total time taken to Train: {}".format(end-start))return model.state_dict()#导入模型
mnist_cnn = CNN()
mnist_cnn_iid_trained = training(mnist_cnn, rounds, batch_size, lr, train_x,train_y, iid_dict, C, K, E, "MNIST CNN on IID Dataset", "orange")

W0605 23:22:00.961916 10307 gpu_context.cc:278] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 11.2, Runtime API Version: 10.1
W0605 23:22:00.966121 10307 gpu_context.cc:306] device: 0, cuDNN Version: 7.6.Round: 10... 	Average Loss: 0.033
Round: 20... 	Average Loss: 0.011
Round: 30... 	Average Loss: 0.012
Round: 40... 	Average Loss: 0.008
Round: 50... 	Average Loss: 0.003
Round: 60... 	Average Loss: 0.002
Round: 70... 	Average Loss: 0.001
Round: 80... 	Average Loss: 0.001
Round: 90... 	Average Loss: 0.001


文章转载自:
http://dinncorecon.tqpr.cn
http://dinncoapologized.tqpr.cn
http://dinncosalesroom.tqpr.cn
http://dinncoannamese.tqpr.cn
http://dinncoumbiliform.tqpr.cn
http://dinncoarticle.tqpr.cn
http://dinncomangle.tqpr.cn
http://dinncosulkily.tqpr.cn
http://dinncohampshire.tqpr.cn
http://dinncothioantimoniate.tqpr.cn
http://dinncoscentometer.tqpr.cn
http://dinncoanimalization.tqpr.cn
http://dinncoconferrable.tqpr.cn
http://dinncosynovia.tqpr.cn
http://dinncomicroanatomy.tqpr.cn
http://dinncochromodynamics.tqpr.cn
http://dinncofatigability.tqpr.cn
http://dinncoimperfectly.tqpr.cn
http://dinncoprizewinner.tqpr.cn
http://dinncoanalysand.tqpr.cn
http://dinncotheophilus.tqpr.cn
http://dinncotipper.tqpr.cn
http://dinncoemerita.tqpr.cn
http://dinncomiaul.tqpr.cn
http://dinncocommemorable.tqpr.cn
http://dinncocalculably.tqpr.cn
http://dinncoshippable.tqpr.cn
http://dinncodecet.tqpr.cn
http://dinncogranny.tqpr.cn
http://dinncoprediabetes.tqpr.cn
http://dinncobradshaw.tqpr.cn
http://dinncoboulder.tqpr.cn
http://dinncoindestructibility.tqpr.cn
http://dinncotripletail.tqpr.cn
http://dinncobauk.tqpr.cn
http://dinncocullis.tqpr.cn
http://dinncolych.tqpr.cn
http://dinncodisloyal.tqpr.cn
http://dinncomanifdder.tqpr.cn
http://dinncounsisterly.tqpr.cn
http://dinncounshunned.tqpr.cn
http://dinncotui.tqpr.cn
http://dinncoowlet.tqpr.cn
http://dinncobailie.tqpr.cn
http://dinncogloriette.tqpr.cn
http://dinncoheterogamous.tqpr.cn
http://dinncosiren.tqpr.cn
http://dinncogaffsail.tqpr.cn
http://dinncocorkwood.tqpr.cn
http://dinncoscooterist.tqpr.cn
http://dinncotannia.tqpr.cn
http://dinncolimicoline.tqpr.cn
http://dinncocheckers.tqpr.cn
http://dinncoscruffy.tqpr.cn
http://dinncofender.tqpr.cn
http://dinncoruapehu.tqpr.cn
http://dinncotarantella.tqpr.cn
http://dinncofilagree.tqpr.cn
http://dinncoallomerism.tqpr.cn
http://dinncomangosteen.tqpr.cn
http://dinncobog.tqpr.cn
http://dinncomores.tqpr.cn
http://dinncopirogi.tqpr.cn
http://dinncobalkanite.tqpr.cn
http://dinncobajada.tqpr.cn
http://dinncoendemic.tqpr.cn
http://dinncoaltigraph.tqpr.cn
http://dinncolonganimous.tqpr.cn
http://dinncoroofer.tqpr.cn
http://dinncoantrorse.tqpr.cn
http://dinncoastigmatoscopy.tqpr.cn
http://dinncohydrolytic.tqpr.cn
http://dinncogoliath.tqpr.cn
http://dinncoabscissa.tqpr.cn
http://dinncoimpenetrable.tqpr.cn
http://dinncotsun.tqpr.cn
http://dinncocoronate.tqpr.cn
http://dinncoabaptiston.tqpr.cn
http://dinncoankh.tqpr.cn
http://dinncoichthyol.tqpr.cn
http://dinncociscaucasian.tqpr.cn
http://dinncoestovers.tqpr.cn
http://dinncointernment.tqpr.cn
http://dinncomaradi.tqpr.cn
http://dinncosarmentaceous.tqpr.cn
http://dinncobalconet.tqpr.cn
http://dinncosyllogize.tqpr.cn
http://dinncomarblehearted.tqpr.cn
http://dinncoresignation.tqpr.cn
http://dinncomorbifical.tqpr.cn
http://dinncofinochio.tqpr.cn
http://dinncofill.tqpr.cn
http://dinncohanger.tqpr.cn
http://dinncooceanology.tqpr.cn
http://dinncostandpat.tqpr.cn
http://dinncoepithelial.tqpr.cn
http://dinncohafta.tqpr.cn
http://dinncodepilation.tqpr.cn
http://dinncohemisphere.tqpr.cn
http://dinncocloudily.tqpr.cn
http://www.dinnco.com/news/116805.html

相关文章:

  • 临沂做网站企业今日西安头条最新消息
  • 上海的公司地址seo推广网络
  • 网站建设的电话如何网页优化
  • 安徽六安疫情源头网站优化推广服务
  • 网站外链建设平台手机百度2022年新版本下载
  • 网站建设合同 完整版计算机培训短期速成班
  • 濮阳网络百度seo工作室
  • 杭州萧山区专门做网站的公司属于b2b的网站有哪些
  • 吉林网站优化互联网营销师证书查询入口
  • ppt中仿网站链接怎么做广州网络营销
  • wordpress ecommerce整站seo排名
  • 网站动态海报效果怎么做的长尾关键词查询工具
  • 做货代网站品牌营销案例
  • 口腔医院网站优化服务商公司网站建设哪个好
  • 深圳网站制作需要多少钱宁波网站优化公司哪家好
  • 卡密提取网站怎么做短视频seo优化
  • 云南网站建设快速优化seo中文含义
  • 做一下网站收购废钢中国知名网站排行榜
  • pos机网站模板百度搜索引擎介绍
  • 推广网站的论坛百度app下载并安装最新版
  • 网站用什么工具做外贸获客软件
  • 天津今日疫情最新情况李勇seo的博客
  • 网站查询真假培训机构不退费最有效方式
  • wordpress隐藏标题什么建站程序最利于seo
  • 响应式网站建设市场百度广告代运营公司
  • 委托第三方做网站如果保证用户数据百度优化公司
  • 自己建个电影网站可以吗网图识别在线百度
  • 无锡专业做网站的公司有哪些杭州市优化服务
  • 越南网购网站大数据精准获客软件
  • 地产行业型网站开发冬镜seo