---> 79 nextposition = np.random.choice(len(lb), p=probabilities) 80…
- User
---> 79 next_position = np.random.choice(len(lb), p=probabilities) 80 current_position[next_position] = 1 82 # Evaluate the fitness of the ant solution
File mtrand.pyx:920, in numpy.random.mtrand.RandomState.choice()
ValueError: probabilities contain NaN
- Assistant
The error is because the "probabilities" array contain NaN (not a number) values. The np.random.choice() function doesn't work with NaN values in the probability array. This may be caused by a division by zero or any operation that results in an undefined value.
To fix this issue, you need to find out the cause of the NaN values in the "probabilities" array and handle it appropriately.
As a quick fix, you can replace NaN values with a uniform distribution as follows:
# For the case when probabilities array contains NaN values if np.isnan(probabilities).any(): probabilities = [1 / len(probabilities)] * len(probabilities)Add this code right before the line that is causing the error:
nextInt = np.random.choice(len(ints), p=probabilities)Keep in mind that you should also investigate the root cause of the NaN values in the "probabilities" array and handle it appropriately.
- User
Code import pandas as pd import numpy as np from tensorflow.keras.layers import Dense, Dropout from tensorflow.keras.models import Sequential from tensorflow.keras.optimizers import Adam from tensorflow.keras import backend as K from sklearn.metrics import f1_score from collections import defaultdict from sklearn.model_selection import train_test_split from pyswarms.single.global_best import GlobalBestPSO from pyswarms.utils.functions import single_obj as fx from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.preprocessing import OneHotEncoder, StandardScaler import warnings warnings.filterwarnings("ignore") import nest_asyncio nest_asyncio.apply() import random from sklearn.model_selection import train_test_split from sklearn.preprocessing import MinMaxScaler import tensorflow as tf import tensorflow_federated as tff from tensorflow.keras.metrics import BinaryAccuracy, Precision, Recall from imblearn.over_sampling import RandomOverSampler,SMOTE from sklearn import preprocessing data = pd.read_csv("bs140513_032310.csv") #print (data.isnull().sum()) ############################################ Simple_Model ###################################### #data = pd.read_csv("bs140513_032310.csv") #data["gender"].fillna("M",inplace=True) #data["age"].fillna(5,inplace=True) data["gender"] = data["gender"].map({"M": 1, "F": 2, "E":3,"U":-1}) data["gender"].fillna(-1,inplace=True) #data.fillna(-999,inplace=True)
Create two dataframes with fraud and non-fraud data
df_fraud = data.loc[data.fraud == 1] df_non_fraud = data.loc[data.fraud == 0] pd.concat([df_fraud.groupby('category')['amount'].mean(),df_non_fraud.groupby('category')['amount'].mean(),
data.groupby('category')['fraud'].mean()*100],keys=["Fraudulent","Non-Fraudulent","Percent(%)"],axis=1,
sort=False).sort_values(by=['Non-Fraudulent']) dropping zipcodeori and zipMerchant since they have only one unique value
data_reduced = data.drop(['zipcodeOri','zipMerchant'],axis=1)
turning object columns type to categorical for easing the transformation process
col_categorical = data_reduced.select_dtypes(include= ['object']).columns for col in col_categorical: data_reduced[col] = data_reduced[col].astype('category')
categorical values ==> numeric values
data_reduced[col_categorical] = data_reduced[col_categorical].apply(lambda x: x.cat.codes) le = preprocessing.LabelEncoder() #data_reduced["merchant"]= le.fit_transform(data_reduced["merchant"].values) data_reduced["customer"]= le.fit_transform(data_reduced["customer"].values) alice_df = data_reduced[:len(data.index)//2] bob_df = data_reduced[len(data.index)//2:] X = data_reduced.drop(['fraud'],axis=1) y = data['fraud'] #(np.isnan(X)) print (X.isnull().sum()) step 0 customer 0 age 0 gender 0 merchant 0 category 0 amount 0 dtype: int64 sm = SMOTE(random_state=42) X_res, y_res = sm.fit_resample(X, y) y_res = pd.DataFrame(y_res)
I won't do cross validation since we have a lot of instances
X_train, X_test, y_train, y_test = train_test_split(X_res,y_res,test_size=0.3,random_state=42,shuffle=True,stratify=y_res) compile_model() def input_spec(): return ( tf.TensorSpec([None, X_train.shape[1]], tf.float64), tf.TensorSpec([None, 1], tf.int64) ) def build_model():
final_model = tf.keras.models.Sequential([ tf.keras.layers.InputLayer(input_shape=(X_train.shape[1],)), tf.keras.layers.Dense(32, activation='relu'), tf.keras.layers.Dense(64, activation='relu'), #tf.keras.layers.Dense(32, activation='relu'), #tf.keras.layers.Dense(3, activation='relu'), tf.keras.layers.Dense(1, activation='sigmoid'), ]) return final_model def compile_model(): """ Compile a given keras model using SparseCategoricalCrossentropy loss and the Adam optimizer with set evaluation metrics. """ keras_model=build_model() keras_model.compile( loss=tf.keras.losses.BinaryCrossentropy(), #optimizer=optimizer.optimize(fx,10), optimizer=tf.keras.optimizers.Adam(), metrics=[BinaryAccuracy()] ) return keras_model def model_fn(): """ Create TFF model from compiled Keras model and a sample batch. """ model = compile_model() final_keras_model =model keras_model_clone = tf.keras.models.clone_model(final_keras_model) return tff.learning.from_keras_model(keras_model_clone, input_spec=input_spec(), loss=tf.keras.losses.BinaryCrossentropy(),metrics=[BinaryAccuracy(), Precision(), Recall()]) X = input_data.drop(['fraud'],axis=1) y =input_data['fraud'] sm = SMOTE(random_state=42) X_res, y_res = sm.fit_resample(X, y) y_res = pd.DataFrame(y_res) #X=input_data.drop('isFraud',axis=1) # I won't do cross validation since we have a lot of instances X_train, X_test, y_train, y_test = train_test_split(X_res,y_res,test_size=0.2,random_state=42) from sklearn import preprocessing def make_tf_dataset(input_data, batch_size=None): # Create two dataframes with fraud and non-fraud data
dropping zipcodeori and zipMerchant since they have only one unique value
#data_reduced = input_data.drop(['zipcodeOri','zipMerchant','customer','gender',"merchant"],axis=1) X = input_data.drop(['fraud'],axis=1) y =input_data['fraud'] sm = SMOTE(random_state=42) X_res, y_res = sm.fit_resample(X, y) y_res = pd.DataFrame(y_res) #X=input_data.drop('isFraud',axis=1) # I won't do cross validation since we have a lot of instances X_train, X_test, y_train, y_test = train_test_split(X_res,y_res,test_size=0.2,random_state=42)x = (data[["type", "amount", "oldbalanceOrg", "newbalanceOrig"]])
#y = (data[["isFraud"]]) #x = np.asarray(x).astype('float32') #y = np.asarray(y)
X_train =tf.convert_to_tensor(X_train, dtype=tf.float64)
#y_train = tf.convert_to_tensor(y_train, dtype=tf.int64) #X_train = np.asarray(X_train).astype(np.float32) #y_train = np.asarray(y_train).astype(np.float32) #tf.convert_to_tensor(X_train, dtype=tf.float32) #scaler = MinMaxScaler() #X_train = scaler.fit_transform(X_train) #sm = SMOTE(random_state=42) #X_train, y_train = sm.fit_resample(X_train, y_train) # Dataset creation dataset = tf.data.Dataset.from_tensor_slices((X_train, y_train)) dataset = dataset.shuffle(2048, seed=SEED) if batch_size: dataset = dataset.batch(batch_size) return dataset EPOCHS = 100 BATCH_SIZE = 64 SEED = 1337 train_data, val_data = [], [] for client_data in [alice_df, bob_df]: train_df, val_df = train_test_split(client_data, test_size=0.2, random_state=SEED) # TF Datasets train_data.append(make_tf_dataset(train_df, batch_size=BATCH_SIZE)) val_data.append(make_tf_dataset(val_df, batch_size=1)) trainer = tff.learning.build_federated_averaging_process( model_fn, client_optimizer_fn=lambda: tf.keras.optimizers.Adam(learning_rate=0.001), server_optimizer_fn=lambda: tf.keras.optimizers.Adam(learning_rate=0.001) ) EPOCHS=100 import time start = time.time() state = trainer.initialize() #state.model.assign_weights_to(model_fn) train_hist = [] for i in range(EPOCHS): state, metrics = trainer.next(state, train_data) train_hist.append(metrics) print(f"\rRun {i+1}/{EPOCHS}", end="") end=time.time() print(f"Runtime of the program is {end - start}") evaluator = tff.learning.build_federated_evaluation(model_fn) federated_metrics = evaluator(state.model, val_data) federated_metrics ####################################### HHO_FL_CCFD #################################################### def evaluate_model(model, X, y): # Evaluate the model on the given data y_pred = model.predict(X) y_pred = (y_pred > 0.5).astype(int) f1 = f1_score(y, y_pred) return f1 def objective_function(w): # Set the weights of the local model shape = get_shape(model) model.set_weights((set_shape(w,shape))) # Evaluate the local model on the local data f1_local = evaluate_model(model, X_train, y_train) return -f1_local def hho_federated_learning(f, n, m, alpha, beta, gamma, lb, ub, iter_max): """ Harris Hawks Optimization for Federated Learning Parameters: f (function): Objective function to optimize n (int): Number of Harris hawks m (int): Number of weights in each local model alpha (float): Harris hawks' step size beta (float): Harris hawks' switching probability gamma (float): Harris hawks' shrinking coefficient lb (ndarray): Lower bounds of the search space ub (ndarray): Upper bounds of the search space iter_max (int): Maximum number of iterations Returns: g_best (float): Global best objective value x_best (ndarray): Global best solution """ # Initialize Harris hawks x = np.random.uniform(lb, ub, (n, m)) f_x = np.apply_along_axis(f, 1, x) g_best = np.min(f_x) x_best = x[np.argmin(f_x)] # Main loop for t in range(iter_max): # Calculate fitness values fitness = 1 / (1 + f_x) # Sort Harris hawks by fitness values idx = np.argsort(fitness)[::-1] x = x[idx] fitness = fitness[idx] # Update Harris hawks' positions for i in range(n): if np.random.rand() < beta: # Exploration: move towards the global best solution x[i] = x[i] + alpha * (x_best - x[i]) + gamma * np.random.randn(m) else: # Exploitation: move towards the best solution in the neighborhood j = np.random.choice(np.delete(np.arange(n), i)) x[i] = x[i] + alpha * (x[j] - x[i]) + gamma * np.random.randn(m) # Apply bounds to the positions x[i] = np.clip(x[i], lb, ub) # Evaluate new positions f_x = np.apply_along_axis(f, 1, x) g_best_new = np.min(f_x) x_best_new = x[np.argmin(f_x)] # Update global best solution if g_best_new < g_best: g_best = g_best_new x_best = x_best_new return g_best, x_best
Build a neural network model
model = compile_model()
Set HHO parameters
n = 10 m = model.count_params() alpha = 0.01 beta = 0.5 gamma = 0.1 lb = np.full(m, -1) ub = np.full(m, 1) def get_shape(model): weights_layer = model.get_weights() shapes = [] for weights in weights_layer: shapes.append(weights.shape) return shapes def set_shape(weights,shapes): new_weights = [] index=0 for shape in shapes: if(len(shape)>1): n_nodes = np.prod(shape)+index else: n_nodes=shape[0]+index tmp = np.array(weights[index:n_nodes]).reshape(shape) new_weights.append(tmp) index=n_nodes return new_weights hho_federated_learning(objective_function, n, m, alpha, beta, gamma, lb, ub, 10) (-0.6666666666666666, array([-0.26523277, -0.26818788, -0.27362061, ..., 0.83882503, -0.12987724, 0.08036423])) def model_fn(): """ Create TFF model from compiled Keras model and a sample batch. """
keras_model = build_model()
final_keras_model =model keras_model_clone = tf.keras.models.clone_model(final_keras_model) return tff.learning.from_keras_model(keras_model_clone, input_spec=input_spec(), loss=tf.keras.losses.BinaryCrossentropy(),metrics=[BinaryAccuracy(), Precision(), Recall()]) trainer = tff.learning.build_federated_averaging_process( model_fn, client_optimizer_fn=lambda: tf.keras.optimizers.Adam(learning_rate=0.001), server_optimizer_fn=lambda: tf.keras.optimizers.Adam(learning_rate=0.001) ) EPOCHS=5 import time start = time.time() state = trainer.initialize() #state.model.assign_weights_to(model_fn) train_hist = [] for i in range(EPOCHS): state, metrics = trainer.next(state, train_data) train_hist.append(metrics) print(f"\rRun {i+1}/{EPOCHS}", end="") end=time.time() print(f"Runtime of the program is {end - start}") Run 5/5Runtime of the program is 23.75927448272705 evaluator = tff.learning.build_federated_evaluation(model_fn) federated_metrics = evaluator(state.model, val_data) federated_metrics OrderedDict([('binary_accuracy', 0.6251889), ('precision', 0.9849844), ('recall', 0.25723067), ('loss', 5.333934)])
def get_shape(model): weights_layer = model.get_weights() shapes = [] for weights in weights_layer: shapes.append(weights.shape) return shapes def set_shape(weights,shapes): new_weights = [] index=0 for shape in shapes: if(len(shape)>1): n_nodes = np.prod(shape)+index else: n_nodes=shape[0]+index tmp = np.array(weights[index:n_nodes]).reshape(shape) new_weights.append(tmp) index=n_nodes return new_weights def evaluate_model(model, X, y): # Evaluate the model on the given data y_pred = model.predict(X) y_pred = (y_pred > 0.5).astype(int) f1 = f1_score(y, y_pred) return f1 def objective_function(w,model): # Set the weights of the local model shape = get_shape(model) model.set_weights((set_shape(w,shape))) # Evaluate the local model on the local data f1_local = evaluate_model(model, X_train, y_train) return -f1_local def normalize(probabilities): total = sum(probabilities) return [p / total for p in probabilities] def aco_fl(objective_function, num_ants, num_iterations, q0, alpha, beta, rho, lb, ub, global_model): # Initialize the pheromone matrix and the probability matrix pheromone = np.ones_like(lb) probability = np.ones_like(lb)/len(lb) best_position = np.zeros_like(lb) best_fitness = objective_function(best_position, global_model)
# Main loop of the ACO algorithm for i in range(num_iterations): # Initialize the ant solutions print('“---- Iteration {} ----”'.format(i + 1)) print('“Pheromone:”', pheromone) print('“Probability:”', probability) print('“Current position:”', best_position) solutions = np.zeros((num_ants, len(lb))) fitness = np.zeros(num_ants) # Construct the ant solutions for j in range(num_ants): current_position = np.zeros_like(lb) for k in range(len(lb)): # Choose the next hyperparameter using the probability matrix and the pheromone matrix if np.random.rand() < q0: next_position = np.argmax(probability) else: def normalize(probabilities): total = sum(probabilities) return [p / total for p in probabilities] probabilities = pheromone**alpha * probability**beta if np.isnan(probabilities).any(): probabilities = [1 / len(probabilities)] * len(probabilities) #probabilities = probabilities/np.sum(probabilities) #probabilities = np.divide(probabilities, np.sum(probabilities), out=np.zeros_like(probabilities), where=np.sum(probabilities) != 0) #probabilities =np.nan_to_num(probabilities ,nan=0) probabilities = np.nan_to_num(probabilities) #print('“Before normalization:”', probabilities) # Add this print statement probabilities = normalize(probabilities) #print('“After normalization:”', probabilities) # Add this print statement # probabilities = normalize(probabilities) next_position = np.random.choice(len(lb), p=probabilities) current_position[next_position] = 1 # Evaluate the fitness of the ant solution solutions[j] = lb + (np.array(ub)-np.array(lb))*current_position fitness[j] = objective_function(solutions[j], global_model) # Update the global best solution if fitness[j] < best_fitness: best_fitness = fitness[j] best_position = solutions[j] # Update the pheromone matrix delta_pheromone = np.zeros_like(pheromone) for j in range(num_ants): for k in range(len(lb)): if solutions[j][k] == 1: epsilon = 1e-9 #delta_pheromone=pd.DataFrame(delta_pheromone) #fitness = fitness.replace([np.inf, -np.inf, -0], 0) delta_pheromone[k] += 1/(fitness[j]+epsilon) pheromone = (1-rho)pheromone + rhodelta_pheromone
# Update the probability matrix probability = pheromone**alpha * probability**beta probability = probability/np.sum(probability) return best_position, best_fitness
Define the global model
#global_model = build_model() #global_model.compile(loss='binary_crossentropy', optimizer=Adam(), metrics=['accuracy']) q0=0.4 alpha=0.9 beta=0.3 rho=0.2
Run the ACO algorithm
num_ants = 2 num_iterations = 10 global_model=compile_model() m = global_model.count_params() lb = np.full(m, -1) ub = np.full(m, 1) best_position, best_fitness = aco_fl(objective_function, num_ants, num_iterations, q0, alpha, beta, rho, lb, ub,global_model) “---- Iteration 1 ----” “Pheromone:” [1 1 1 ... 1 1 1] “Probability:” [0.00041102 0.00041102 0.00041102 ... 0.00041102 0.00041102 0.00041102] “Current position:” [0 0 0 ... 0 0 0] “---- Iteration 2 ----” “Pheromone:” [1.99999991e+08 2.00000001e+08 2.00000001e+08 ... 8.00000000e-01 8.00000000e-01 1.99999991e+08] “Probability:” [nan nan nan ... nan nan nan] “Current position:” [ 1. -1. -1. ... -1. -1. 1.] “---- Iteration 3 ----” “Pheromone:” [3.59999992e+08 1.60000000e+08 1.60000000e+08 ... 3.40000000e-01 3.40000000e-01 1.59999993e+08] “Probability:” [nan nan nan ... nan nan nan] “Current position:” [ 1. -1. -1. ... 1. 1. -1.] “---- Iteration 4 ----” “Pheromone:” [ 4.87999994e+08 3.28000000e+08 1.28000000e+08 ... -2.80000008e-02 2.72000000e-01 3.27999994e+08] “Probability:” [nan nan nan ... nan nan nan] “Current position:” [ 1. -1. -1. ... 1. 1. -1.] “---- Iteration 5 ----” “Pheromone:” [ 5.90398526e+08 2.62400000e+08 1.02400000e+08 ... -1.46889747e+03 -1.46865747e+03 2.62399995e+08] “Probability:” [nan nan nan ... nan nan nan] “Current position:” [ 1. -1. -1. ... 1. 1. -1.] “---- Iteration 6 ----” “Pheromone:” [ 4.72318818e+08 2.09919998e+08 8.19200002e+07 ... -1.17541798e+03 -1.17750732e+03 2.09919994e+08] “Probability:” [nan nan nan ... nan nan nan] “Current position:” [ 1. -1. -1. ... 1. 1. -1.] “---- Iteration 7 ----” “Pheromone:” [5.77855053e+08 3.67935998e+08 6.55360002e+07 ... 1.99999060e+08 1.99999058e+08 3.67935995e+08] “Probability:” [nan nan nan ... nan nan nan] “Current position:” [ 1. -1. -1. ... 1. 1. -1.] “---- Iteration 8 ----” “Pheromone:” [6.62283992e+08 2.94348799e+08 5.24288002e+07 ... 3.59999197e+08 3.59999246e+08 4.94348746e+08] “Probability:” [nan nan nan ... nan nan nan] “Current position:” [ 1. -1. -1. ... 1. 1. -1.] “---- Iteration 9 ----” “Pheromone:” [7.29827193e+08 2.35479039e+08 4.19430401e+07 ... 4.87999358e+08 4.87999397e+08 3.95478996e+08] “Probability:” [nan nan nan ... nan nan nan] “Current position:” [ 1. 1. -1. ... -1. -1. -1.] “---- Iteration 10 ----” “Pheromone:” [7.83861754e+08 3.88383231e+08 3.35544318e+07 ... 5.90399486e+08 5.90399518e+08 5.16383197e+08] “Probability:” [nan nan nan ... nan nan nan] “Current position:” [ 1. 1. -1. ... -1. -1. -1.]
- Assistant
As I mentioned earlier, the error is due to NaN values in the "probabilities" array.
You can replace the NaN values with uniform distribution as suggested before.
But, in your code, I see that the NaN values are actually coming from a division because of the exceedingly large values of pheromone in the ACO algorithm implementation:
probabilities = pheromone**alpha * probability**betaBefore normalization, the probabilities contain extremely high numbers. And when you normalize them, you are dividing by extremely high values which might lead to NaN values. In addition, raising the pheromone values to the power of alpha plays an important role in this happening.
To solve this problem, you can try the following:
- Clip the pheromone values before running the normalization to prevent extremely large values. For example, set a maximum value for the pheromone and clip it accordingly.
max_pheromone = 1e6 pheromone = np.clip(pheromone, 0, max_pheromone)- Adjust the parameters of the ACO algorithm (alpha, beta, rho, etc.) to prevent such imbalances between the values in the pheromone matrix and the probability matrix.
Remember, these suggestions might not lead to a perfect solution directly, and you may need to tune the parameters and experiment with different settings to find the best balance between exploration and exploitation in the ACO algorithm for this problem.
- User
“---- Iteration 1 ----” “Pheromone:” [1 1 1 ... 1 1 1] “Probability:” [0.00041102 0.00041102 0.00041102 ... 0.00041102 0.00041102 0.00041102] “Current position:” [0 0 0 ... 0 0 0] “---- Iteration 2 ----” “Pheromone:” [0.12644257 0.12644257 0.50004256 ... 0.50004256 0.8 0.12644257] “Probability:” [0.00012105 0.00012105 0.00041722 ... 0.00041722 0.00063685 0.00012105] “Current position:” [ 1. 1. 1. ... 1. -1. 1.] “---- Iteration 3 ----” “Pheromone:” [1.01154058e-01 1.01154058e-01 4.24946252e-02 ... 2.00000000e+08 2.82460580e-01 1.01154058e-01] “Probability:” [nan nan nan ... nan nan nan] “Current position:” [ 1. 1. 1. ... 1. -1. 1.] “---- Iteration 4 ----” “Pheromone:” [2.00000000e+08 8.09232462e-02 3.39957001e-02 ... 7.99999547e+05 2.00000000e+08 2.00000000e+08] “Probability:” [nan nan nan ... nan nan nan] “Current position:” [ 1. 1. 1. ... 1. -1. 1.]
- Assistant
It seems that you still have NaN values in the probability matrix in iterations 3 and 4. This is caused by the exceedingly large values in the pheromone matrix after a certain iteration (specifically at index 2).
First, let's change the normalization function while handling zero total probabilities to avoid division by zero:
def normalize(probabilities): total = sum(probabilities) if total == 0: return [1 / len(probabilities)] * len(probabilities) return [p / total for p in probabilities]In addition, your constraints on pheromone values are not sufficient as they exceed the maximum value allowed. To solve this problem, you can introduce two additional parameters:
- Introduce a
min_pheromoneparameter and clip the pheromone values after each update with both min and max constraints. - Introduce a scaling factor
scale_pheromoneto control the magnitude of pheromone values.
Here's an updated version of your hho_fl function with these additional parameters:
def hho_federated_learning(objective_function, num_ants, num_iterations, q0, alpha, beta, rho, lb, ub, global_model, min_pheromone=1e-12, max_pheromone=1e6, scale_pheromone=1): # Initialize the pheromone matrix and the probability matrix pheromone = np.ones_like(lb) * scale_pheromone probability = np.ones_like(lb) / len(lb) best_position = np.zeros_like(lb) best_fitness = objective_function(best_position, global_model) # ... (rest of the hho_fl implementation) # Update the pheromone matrix delta_pheromone = np.zeros_like(pheromone) for j in range(num_ants): for k in range(len(lb)): if solutions[j][k] == 1: epsilon = 1e-9 delta_pheromone[k] += 1 / (fitness[j] + epsilon) pheromone = (1 - rho) * pheromone + rho * delta_pheromone pheromone = np.clip(pheromone, min_pheromone, max_pheromone)With these changes, you should have more control over the pheromone values and avoid NaN probabilities. You might need to adjust the
min_pheromone,max_pheromone, andscale_pheromonevalues for better results. - Introduce a
- User
“---- Iteration 1 ----” “Pheromone:” [1 1 1 ... 1 1 1] “Probability:” [0.00041102 0.00041102 0.00041102 ... 0.00041102 0.00041102 0.00041102] “Current position:” [0 0 0 ... 0 0 0] “---- Iteration 2 ----” “Pheromone:” [1.e-12 6.e-01 1.e-12 ... 6.e-01 1.e-12 1.e-12] “Probability:” [1.62285933e-14 6.46572161e-04 1.62285933e-14 ... 6.46572161e-04 1.62285933e-14 1.62285933e-14] “Current position:” [ 1. 1. -1. ... 1. 1. -1.] “---- Iteration 3 ----” “Pheromone:” [1.0e-12 4.8e-01 1.0e-12 ... 1.0e-12 1.0e-12 1.0e-12] “Probability:” [1.24941925e-16 6.16490155e-03 1.24941925e-16 ... 1.89151170e-13 1.24941925e-16 1.24941925e-16] “Current position:” [ 1. 1. -1. ... 1. 1. -1.] “---- Iteration 4 ----” “Pheromone:” [1.e-12 1.e+06 1.e-12 ... 1.e-12 1.e-12 1.e-12] “Probability:” [3.35589213e-23 6.81982732e-03 3.35589213e-23 ... 3.01882713e-22 3.35589213e-23 3.35589213e-23] “Current position:” [ 1. 1. -1. ... 1. 1. -1.] “---- Iteration 5 ----” “Pheromone:” [1.e-12 1.e+06 1.e-12 ... 1.e-12 1.e-12 1.e-12] “Probability:” [3.57601414e-25 7.01114328e-03 3.57601414e-25 ... 6.91207278e-25 3.57601414e-25 3.57601414e-25] “Current position:” [ 1. 1. -1. ... 1. 1. -1.]
- Assistant
It seems that the pheromone values have been controlled better with the addition of the
min_pheromone,max_pheromone, andscale_pheromoneparameters. As a result, there are no NaN probabilities anymore.Now that you have better control over the pheromone values and probabilities, you can proceed with tuning the parameters of the ACO and HHO algorithms to find a good balance between exploration and exploitation to achieve better solutions.
Additionally, you can also consider experimenting with different neural network architectures, learning rates, and optimization techniques to achieve better convergence in the neural network training process. The combination of optimization algorithms, such as ACO and HHO, together with federated learning and the neural network model, can be a complicated system to optimize, but proper parameter selection and experimentation should lead to better results.
- User
'binary_accuracy', 0.5020003), ('precision', 0.50198174), ('recall', 1.0), ('loss', 7.577937)])
- Assistant
The results show that the binary accuracy of the model is 0.5020003, the precision is 0.50198174, and the recall is 1.0. The binary accuracy is close to 0.5, which means that the model performs almost as well as a random classifier. The recall is 1.0, indicating that the model is overfitting and capturing all the positive cases, but at the same time, its precision is 0.50198174, showing that its ability to discriminate between the positive and negative cases is low.
Given the complexity of optimizing the ACO, HHO, and neural network model, it's not surprising that the obtained results are not very good with the current settings. You can try the following approaches to improve the performance