[Python] How can I combine 5 trained models into a single one, and use the combined model for...

Discussão em 'Python' iniciado por Stack, Janeiro 26, 2025.

  1. Stack

    Stack Membro Participativo

    I have to implement federated learning. So I trained a list of 5 models, and trained each model on different datasets. Now I have to combine the trained models in the list but I have no Idea on how to do so. After that I have to use the combined model to predict on test set.

    class MulticlassClassification:
    def __init__(self, input_dims, layers, num_classes):
    self.model = Sequential(name="server_model")
    self.model.add(Dense(
    layers[0],
    input_shape=(input_dims,),
    activation='relu'
    ))
    for l in layers[1:]:
    self.model.add(Dense(l, activation='relu'))
    self.model.add(Dense(num_classes, activation='softmax'))
    self.model.compile(
    loss='sparse_categorical_crossentropy',
    optimizer='adam',
    metrics=['accuracy']
    )
    self.model.summary()
    def set_avg_weights(self, models):
    all_weights = np.array([
    model.model.get_weights()
    for model in models
    ])
    avg_weights = np.mean(all_weights, axis=0)
    self.model.set_weights(avg_weights)

    all_weights = np.array([
    model.model.get_weights()
    for model in models
    ])
    avg_weights = np.mean(all_weights, axis=0)
    self.model.set_weights(avg_weights)
    def fit(self, X, Y, epochs, batch_size):
    self.model.fit(X, Y.to_numpy(), epochs=epochs, batch_size=batch_size)

    def predict(self, X, Y):
    preds = self.model.predict(X)
    preds = [np.argmax(p) for p in preds]
    print(classification_report(Y, preds))
    ConfusionMatrixDisplay.from_predictions(Y, preds)


    #Preprocessing and Training Simultaneously
    label_encoder = preprocessing.LabelEncoder()
    model = []
    for i in range(5):
    model.append(MulticlassClassification(27, [20, 20, 20], num_classes=5))
    for i in range(5):
    file_path = os.path.join(folder_path, nodes_list)
    df = pd.read_csv(file_path)
    df=df.drop(['FLAGS'],axis=1)
    df['PKT_TYPE']=label_encoder.fit_transform(df['PKT_TYPE'])
    df['PKT_CLASS']=label_encoder.fit_transform(df['PKT_CLASS'])
    df['NODE_NAME_TO']=label_encoder.fit_transform(df['NODE_NAME_TO'])
    df['NODE_NAME_FROM']=label_encoder.fit_transform(df['NODE_NAME_FROM'])
    print(df['PKT_TYPE'].unique())
    print(df['NODE_NAME_FROM'].unique())
    print(df['NODE_NAME_TO'].unique())
    print(df['PKT_CLASS'].unique())
    X=df.drop(['PKT_CLASS'],axis=1)
    Y=df['PKT_CLASS']
    X1, X2, Y1, Y2= train_test_split(X, Y, train_size=0.9984375, random_state=1)
    model.fit(X1, Y1, epochs=20, batch_size=32)

    #Now I have to merge all models in the list model[] into a single one. How to do so??
    #Then I have to predict the test set using the new model.```

    Continue reading...

Compartilhe esta Página