关于人工智能:联邦学习-FL-中常见的3种模型聚合方法的-Tensorflow-示例

6次阅读

共计 2795 个字符,预计需要花费 7 分钟才能阅读完成。

联结学习 (FL) 是一种杰出的 ML 办法,它使多个设施(例如物联网 (IoT) 设施)或计算机可能在模型训练实现时进行合作,而无需共享它们的数据。

“客户端”是 FL 中应用的计算机和设施,它们能够彼此齐全拆散并且领有各自不同的数据,这些数据能够利用同不隐衷策略,并由不同的组织领有,并且彼此不能互相拜访。

应用 FL,模型能够在没有数据的状况下从更宽泛的数据源中学习。FL 的宽泛应用的畛域如下:

  • 卫生保健
  • 物联网 (IoT)
  • 挪动设施

因为数据隐衷对于许多应用程序(例如医疗数据)来说是一个大问题,因而 FL 次要用于爱护客户的隐衷而不与任何其余客户或方共享他们的数据。FL 的客户端与地方服务器共享他们的模型更新以聚合更新后的全局模型。全局模型被发送回客户端,客户端能够应用它进行预测或对本地数据采取其余操作。

FL 的要害概念

数据隐衷: 实用于敏感或隐衷数据利用。

数据分布: 训练散布在大量设施或服务器上; 模型应该可能泛化到新的数据。

模型聚合: 跨不同客户端更新的模型并且聚合生成繁多的全局模型,模型的聚合形式如下:

  • 简略均匀: 对所有客户端进行均匀
  • 加权均匀: 在均匀每个模型之前,依据模型的品质,或其训练数据的数量进行加权。
  • 联邦均匀: 这在缩小通信开销方面很有用,并有助于进步思考模型更新和应用的本地数据差别的全局模型的收敛性。
  • 混合办法: 联合下面多种模型聚合技术。

通信开销: 客户端与服务器之间模型更新的传输,须要思考通信协议和模型更新的频率。

收敛性:FL 中的一个关键因素是模型收敛到一个对于数据的分布式性质的良好解决方案。

实现 FL 的简略步骤

  1. 定义模型体系结构
  2. 将数据划分为客户端数据集
  3. 在客户端数据集上训练模型
  4. 更新全局模型
  5. 反复下面的学习过程

Tensorflow 代码示例

首先咱们先建设一个简略的服务端:

 importtensorflowastf
 
 # Set up a server and some client devices
 server=tf.keras.server.Server()
 devices= [tf.keras.server.ClientDevice(worker_id=i) foriinrange(4)]
 
 # Define a simple model and compile it
 inputs=tf.keras.Input(shape=(10,))
 outputs=tf.keras.layers.Dense(2, activation='softmax')(inputs)
 model=tf.keras.Model(inputs=inputs, outputs=outputs)
 model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
 
 # Define a federated dataset and iterate over it
 federated_dataset=tf.keras.experimental.get_federated_dataset(devices, model, x=X, y=y)
 forx, yinfederated_dataset:
     # Train the model on the client data
     model.fit(x, y)

而后咱们实现模型聚合步骤:

1、简略均匀

 # Average the updated model weights
 model_weights=model.get_weights()
 fordeviceindevices:
     device_weights=device.get_weights()
     fori, (model_weight, device_weight) inenumerate(zip(model_weights, device_weights)):
         model_weights[i] = (model_weight+device_weight) /len(devices)
 
 # Update the model with the averaged weights
 model.set_weights(model_weights)

2、加权均匀

 # Average the updated model weights using weights based on the quality of the model or the amount of data used to train it
     model_weights=model.get_weights()
     total_weight=0
     fordeviceindevices:
         device_weights=device.get_weights()
         weight=compute_weight(device)  # Replace this with a function that returns the weight for the device
         total_weight+=weight
         fori, (model_weight, device_weight) inenumerate(zip(model_weights, device_weights)):
             model_weights[i] =model_weight+ (device_weight-model_weight) * (weight/total_weight)
 
 # Update the model with the averaged weights    
 model.set_weights(model_weights)

3、联邦均匀

 # Use federated averaging to aggregate the updated models
 model_weights=model.get_weights()
 client_weights= []
 fordeviceindevices:
     client_weights.append(device.get_weights())
 server_weights=model_weights
 for_inrange(num_rounds):
     fori, deviceinenumerate(devices):
         device.set_weights(server_weights)
         model.fit(x[i], y[i])
         client_weights[i] =model.get_weights()
     server_weights=server.federated_average(client_weights)
 
 # Update the model with the averaged weights
 model.set_weights(server_weights)

以上就是联邦学习中最根本的 3 个模型聚合办法,心愿对你有所帮忙

https://avoid.overfit.cn/post/d426b291716c48409d3b68704545f6d0

作者:Dr Roushanak Rahmat, PhD

正文完
 0