農林漁牧網

您現在的位置是:首頁 > 農業

聯邦學習 (FL) 中常見的3種模型聚合方法的 Tensorflow 示例

2023-01-15由 deephub 發表于 農業

subplot怎麼使用

聯合學習 (FL) 是一種出色的 ML 方法,它使多個裝置(例如物聯網 (IoT) 裝置)或計算機能夠在模型訓練完成時進行協作,而無需共享它們的資料。

聯邦學習 (FL) 中常見的3種模型聚合方法的 Tensorflow 示例

“客戶端”是 FL 中使用的計算機和裝置,它們可以彼此完全分離並且擁有各自不同的資料,這些資料可以應用同不隱私策略,並由不同的組織擁有,並且彼此不能相互訪問。

使用 FL,模型可以在沒有資料的情況下從更廣泛的資料來源中學習。 FL 的廣泛使用的領域如下:

衛生保健

物聯網 (IoT)

移動裝置

由於資料隱私對於許多應用程式(例如醫療資料)來說是一個大問題,因此 FL 主要用於保護客戶的隱私而不與任何其他客戶或方共享他們的資料。 FL的客戶端與中央伺服器共享他們的模型更新以聚合更新後的全域性模型。 全域性模型被髮送回客戶端,客戶端可以使用它進行預測或對本地資料採取其他操作。

聯邦學習 (FL) 中常見的3種模型聚合方法的 Tensorflow 示例

FL的關鍵概念

資料隱私:適用於敏感或隱私資料應用。

資料分佈:訓練分佈在大量裝置或伺服器上;模型應該能夠泛化到新的資料。

模型聚合:跨不同客戶端更新的模型並且聚合生成單一的全域性模型,模型的聚合方式如下:

簡單平均:對所有客戶端進行平均

加權平均:在平均每個模型之前,根據模型的質量,或其訓練資料的數量進行加權。

聯邦平均:這在減少通訊開銷方面很有用,並有助於提高考慮模型更新和使用的本地資料差異的全域性模型的收斂性。

混合方法:結合上面多種模型聚合技術。

通訊開銷:客戶端與伺服器之間模型更新的傳輸,需要考慮通訊協議和模型更新的頻率。

收斂性:FL中的一個關鍵因素是模型收斂到一個關於資料的分散式性質的良好解決方案。

實現FL的簡單步驟

定義模型體系結構

將資料劃分為客戶端資料集

在客戶端資料集上訓練模型

更新全域性模型

重複上面的學習過程

Tensorflow程式碼示例

首先我們先建立一個簡單的服務端:

import tensorflow as tf

# Set up a server and some client devices

server = tf。keras。server。Server()

devices = [tf。keras。server。ClientDevice(worker_id=i) for i in range(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)

for x, y in federated_dataset:

# Train the model on the client data

model。fit(x, y)

然後我們實現模型聚合步驟:

1、簡單平均

# Average the updated model weights

model_weights = model。get_weights()

for device in devices:

device_weights = device。get_weights()

for i, (model_weight, device_weight) in enumerate(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

for device in devices:

device_weights = device。get_weights()

weight = compute_weight(device) # Replace this with a function that returns the weight for the device

total_weight += weight

for i, (model_weight, device_weight) in enumerate(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 = []

for device in devices:

client_weights。append(device。get_weights())

server_weights = model_weights

for _ in range(num_rounds):

for i, device in enumerate(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