AttributeError When Trying to Use build_federated_averaging_process in TensorFlow Federated
Reehan9 opened this issue · comments
Hello,
I am trying to use the build_federated_averaging_process function in TensorFlow Federated for a project on federated learning related to traffic management. However, I am encountering an AttributeError that says the tensorflow_federated.python.learning module has no attribute build_federated_averaging_process.
Here is the code I am trying to run:
import tensorflow as tf
import tensorflow_federated as tff
# Define a function to create your model
def create_model():
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(10, activation='relu', input_shape=(NUM_FEATURES,)),
tf.keras.layers.Dense(1, activation='sigmoid')
])
return model
# Define a function to create tff model
def model_fn():
keras_model = create_model()
return tff.learning.from_keras_model(
keras_model,
input_spec=preprocessed_example_dataset.element_spec,
loss=tf.keras.losses.BinaryCrossentropy(),
metrics=[tf.keras.metrics.BinaryAccuracy()])
# Define federated averaging process
federated_averaging = tff.learning.build_federated_averaging_process(
model_fn,
client_optimizer_fn=lambda: tf.keras.optimizers.SGD(learning_rate=0.02),
server_optimizer_fn=lambda: tf.keras.optimizers.SGD(learning_rate=1.0))
# Initialize the Federated Averaging process
state = federated_averaging.initialize()
# Run one round of Federated Averaging
state, metrics = federated_averaging.next(state, [example_dataset])
print('round 1, metrics={}'.format(metrics))
# Run additional rounds
for round_num in range(2, NUM_ROUNDS+1):
state, metrics = federated_averaging.next(state, [example_dataset])
print('round {:2d}, metrics={}'.format(round_num, metrics))
I am running this code in a Google Colab notebook. I have TensorFlow Federated version 0.59.0 and TensorFlow version 2.5.0 installed. I have tried reinstalling TensorFlow Federated, resetting my runtime, and switching to a different runtime, but none of these solutions have worked.
Could you please help me understand why I am encountering this error and how I can resolve it?
The tff.learning.build_federated_averaging_process
symbol was removed a while ago. Users should generally use tff.learning.algorithms.build_weighted_fed_avg
instead (docs link).
I'm not sure where you got the code snippet that you pasted from, but I strongly encourage you to take a look at the tutorials which are kept current with the TFF version.