I am running different datasets with different numbers of clients on federated learning code. However, when working with some datasets, I can set the value of NUM_CLIENTS to a maximum of 20. When the client value exceeds 20, I encounter the error below. Where am I making a mistake?
ValueError Traceback (most recent call last)
<ipython-input-19-3e647171f427> in <cell line: 18>()
35
36
---> 37 client_model.fit(datagen(client[0],client[1], batch_size=batch_size, epochs=epochs),
38 epochs=5, steps_per_epoch=steps_per_epoch)
39
1 frames
/usr/local/lib/python3.10/dist-packages/keras/src/engine/data_adapter.py in __init__(self, x, y, sample_weight, batch_size, steps_per_epoch, initial_epoch, epochs, shuffle, class_weight, max_queue_size, workers, use_multiprocessing, model, steps_per_execution, distribute, pss_evaluation_shards)
1273
1274 if steps_per_epoch == 0:
-> 1275 raise ValueError(
1276 "Unexpected value for `steps_per_epoch`. Received value is 0. "
1277 "Please check the docstring for `model.fit()` for supported "
ValueError: Unexpected value for `steps_per_epoch`. Received value is 0. Please check the docstring for `model.fit()` for supported values.
import tensorflow as tf
import numpy as np
from tensorflow.keras.preprocessing.image import ImageDataGenerator
# Define the number of clients and the number of training rounds
NUM_CLIENTS = 10
NUM_ROUNDS = 3
# Define the clients and their data
clients = []
for i in range(NUM_CLIENTS):
client_data = train_paths[i * (len(train_paths) // NUM_CLIENTS):(i + 1) * (len(train_paths) // NUM_CLIENTS)]
client_labels = train_labels[i * (len(train_labels) // NUM_CLIENTS):(i + 1) * (len(train_labels) // NUM_CLIENTS)]
clients.append((client_data, client_labels))
# Federated learning loop
for round_num in range(NUM_ROUNDS):
# Select clients
selected_client_indices = np.random.choice(len(clients), size=int(NUM_CLIENTS * 0.5), replace=False)
selected_clients = [clients[i] for i in selected_client_indices]
# Transmit the global model to the selected clients
for client in selected_clients:
client_model = tf.keras.models.clone_model(model)
client_model.set_weights(model.get_weights())
# Compile the client model
client_model.compile(optimizer=Adam(learning_rate=0.0001),
loss='sparse_categorical_crossentropy',
metrics=['sparse_categorical_accuracy'])
steps_per_epoch = int(len(client[0]) / 20)
client_model.fit(datagen(client[0],client[1], batch_size=batch_size, epochs=epochs),
epochs=5, steps_per_epoch=steps_per_epoch)
# Train locally
steps_per_epoch = int(len(client[0]) / 20)
# Aggregate the model
new_weights = []
for layer_index in range(len(model.get_weights())):
new_layer_weights = np.mean([client_model.get_weights()[layer_index], model.get_weights()[layer_index]], axis=0)
new_weights.append(new_layer_weights)
model.set_weights(new_weights)
batch_size = 32
steps = int(len(test_paths)/batch_size)
y_pred = []
y_true = []
for x,y in tqdm(datagen(test_paths, test_labels, batch_size=batch_size, epochs=1), total=steps):
pred = model.predict(x)
pred = np.argmax(pred, axis=-1)
for i in decode_label(pred):
y_pred.append(i)
for i in decode_label(y):
y_true.append(i)
# # Evaluate the global model
# test_loss, test_acc = model.evaluate(test_paths, test_labels,)
# print('Round {}: Test accuracy = {}'.format(round_num, test_acc))
# # Fine-tune the model
# model.fit(test_paths, test_labels, epochs=1, batch_size=32)
# Deploy the model
model.save('my_model.h5')
I want to increase the number of clients to 50
New contributor
Yaşar Büyüknacar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.