我有這樣的 run_one_round 函式:
def run_one_round(server_state, federated_dataset):
"""Orchestration logic for one round of computation.
Args:
server_state: A `ServerState`.
federated_dataset: A federated `tf.data.Dataset` with placement
`tff.CLIENTS`.
Returns:
A tuple of updated `ServerState` and `tf.Tensor` of average loss.
"""
tf.print("run_one_round")
server_message = tff.federated_map(server_message_fn, server_state)
server_message_at_client = tff.federated_broadcast(server_message)
client_outputs = tff.federated_map(
client_update_fn, (federated_dataset, server_message_at_client))
weight_denom = client_outputs.client_weight
from tensorflow_federated.python.core.impl.federated_context import value_impl
value = value_impl.to_value(client_outputs.test, None)
from tensorflow_federated.python.core.impl.types import placements
from tensorflow_federated.python.core.impl.federated_context import value_utils
value = value_utils.ensure_federated_value(value, placements.CLIENTS,
'value to be averaged')
value_comp = value.comp
testing = []
import sparse_ternary_compression
for index in range(len(value_comp[0])):
testing.append(
sparse_ternary_compression.stc_decompression(value_comp[0][index][0], value_comp[0][index][1],
value_comp[0][index][2], value_comp[0][index][3],
value_comp[0][index][4]))
# round_model_delta indica i pesi che vengono usati su server_update. Quindi è quello che va cambiato
round_model_delta = tff.federated_mean(
client_outputs.weights_delta, weight=weight_denom)
server_state = tff.federated_map(server_update_fn, (server_state, round_model_delta))
round_loss_metric = tff.federated_mean(client_outputs.model_output, weight=weight_denom)
return server_state, round_loss_metric, value.comp
但是當我嘗試這樣做時:
value_comp = value.comp
testing = []
import sparse_ternary_compression
for index in range(len(value_comp[0])):
testing.append(
sparse_ternary_compression.stc_decompression(value_comp[0][index][0], value_comp[0][index][1],
value_comp[0][index][2], value_comp[0][index][3],
value_comp[0][index][4]))
我收到此錯誤:”
File "/mnt/d/Davide/Uni/TesiMagistrale/ProgettoTesi/simple_fedavg_tff.py", line 137, in run_one_round
for index in range(len(value_comp[0])):
TypeError: 'Call' object is not subscriptable
如果我回傳該值value.comp,然后在 main 中執行相同的操作,則它可以正常作業。
for round_num in range(FLAGS.total_rounds):
print("--------------------------------------------------------")
sampled_clients = np.random.choice(train_data.client_ids, size=FLAGS.train_clients_per_round, replace=False)
sampled_train_data = [train_data.create_tf_dataset_for_client(client) for client in sampled_clients]
代碼是一樣的,為什么我不能for在run_one_round函式內部使用回圈呢?
server_state, train_metrics, value_comp = iterative_process.next(server_state, sampled_train_data)
testing = []
import sparse_ternary_compression
for index in range(len(value_comp[0])):
testing.append(sparse_ternary_compression.stc_decompression(value_comp[0][index][0], value_comp[0][index][1],
value_comp[0][index][2], value_comp[0][index][3],
value_comp[0][index][4]))
print(testing)
print(f'Round {round_num}')
print(f'\tTraining loss: {train_metrics:.4f}')
if round_num % FLAGS.rounds_per_eval == 0:
server_state.model_weights.assign_weights_to(keras_model)
accuracy = evaluate(keras_model, test_data)
print(f'\tValidation accuracy: {accuracy * 100.0:.2f}%')
tf.print(tf.compat.v2.summary.scalar("Accuracy", accuracy * 100.0, step=round_num))
基本上我只想訪問test客戶端發送使用的變數client_update并在tff.federated_mean函式之前對該串列執行一些操作。
問題可能是那run_one_round是一個tff.federated_computation?
uj5u.com熱心網友回復:
也許嘗試拆開value_comp:
import tensorflow as tf
import numpy as np
value_comp = tf.constant(np.random.random((1, 8, 5)))
value_comp = tf.unstack(value_comp)
testing = []
for index in value_comp:
testing.append(
sparse_ternary_compression.stc_decompression(index[0], index[1],
index[2], index[3],
index[4]))
uj5u.com熱心網友回復:
首先閱讀構建您自己的聯邦學習演算法教程可能會有所幫助。
我認為您應該記住的主要事情是任何 TensorFlow 代碼或任何結構操作都應該在tff.tf_computation-decorated 方法中。然后使用-decorated 方法tff.federated_*范圍內的運算子連接這些構建塊tff.federated_computation。
我假設stc_decompression您的代碼片段中的 是某種 TensorFlow 邏輯。然而,你傳遞給它的不是任何 TF 可理解的值,而是計算結果的抽象表示,主要是 TFF 的內部實作細節。
因此,無論您想用這些方法做什么,都可以在tff.tf_computation裝飾方法中進行,在其中您可以撰寫任何 TF 代碼。您將通過使用tff.federated_map運算子呼叫該方法來獲取您的價值。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/328252.html
