[Python] Federated averaging implementation in python

Discussão em 'Python' iniciado por Stack, Março 14, 2025.

  1. Stack

    Stack Membro Participativo

    I am working with federated learning. I am using a global server where I defined a cnn based classifier. The global server compiles the model with hyper-parameters and send it to the edge(clients), currently I am using two clients. Each client uses its local data (for now I am using same data, and model on each client). After training model, each client has above 95 percent accuracy, precision and recall in their local models. clients sends their trained local model to the server. The server gets the model and and gets the weights from each received model and computes average according to this formula. Below is the code I wrote to implement this formula in python. when I set the average weights to models and try to predict, the accuracy, recall and precision fall below 20%.

    Am I doing something wrong in implementation?

    # initial weights of global model, set to zer0.
    ave_weights=model.get_weights()
    ave_weights=[i * 0 for i in ave_weights]
    count=0
    # Multithreaded Python server : TCP Server Socket Thread Pool
    def ClientThread_send(conn,address,weights):
    # send model to client
    conn.send(model)

    print("Model Sent to :",address)
    print("waiting for weights")
    model_recv=conn.recv(1024)
    print("weights received from:",address)
    global count
    global ave_weights


    #receive weights from clients
    rec_weight=model.get_weights()
    #multiply the client weights by number of local data samples in client local data
    rec_weight= [i * 100000 for i in rec_weight]
    # divide the weights by total number of samples of all participants
    rec_weight= [i / 200000 for i in rec_weight]

    #sum the weights of all clients
    ave_weights=[x + y for x, y in zip(ave_weights,rec_weight)]

    count=count+1
    conn.close()
    if count==2:
    # set the global model weights if the count(number of clients is two)
    model.set_weights(ave_weights)


    while True:
    conn, address = s.accept()
    start_new_thread(ClientThread_send,(conn,address,ave_weights))

    Continue reading...

Compartilhe esta Página