Skip to content

Commit

Permalink
Replace format() str method by f-string
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedfgad committed Sep 7, 2023
1 parent aab09bd commit 900df55
Show file tree
Hide file tree
Showing 26 changed files with 141 additions and 141 deletions.
14 changes: 7 additions & 7 deletions examples/KerasGA/XOR_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ def fitness_func(ga_instanse, solution, sol_idx):
return solution_fitness

def on_generation(ga_instance):
print("Generation = {generation}".format(generation=ga_instance.generations_completed))
print("Fitness = {fitness}".format(fitness=ga_instance.best_solution()[1]))
print(f"Generation = {ga_instance.generations_completed}")
print(f"Fitness = {ga_instance.best_solution()[1]}")

# Build the keras model using the functional API.
input_layer = tensorflow.keras.layers.Input(2)
Expand Down Expand Up @@ -62,23 +62,23 @@ def on_generation(ga_instance):

# Returning the details of the best solution.
solution, solution_fitness, solution_idx = ga_instance.best_solution()
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))
print("Index of the best solution : {solution_idx}".format(solution_idx=solution_idx))
print(f"Fitness value of the best solution = {solution_fitness}")
print(f"Index of the best solution : {solution_idx}")

predictions = pygad.kerasga.predict(model=model,
solution=solution,
data=data_inputs)
print("Predictions : \n", predictions)
print(f"Predictions : \n{predictions}")

# Calculate the binary crossentropy for the trained model.
bce = tensorflow.keras.losses.BinaryCrossentropy()
print("Binary Crossentropy : ", bce(data_outputs, predictions).numpy())
print(f"Binary Crossentropy : {bce(data_outputs, predictions).numpy()}")

# Calculate the classification accuracy for the trained model.
ba = tensorflow.keras.metrics.BinaryAccuracy()
ba.update_state(data_outputs, predictions)
accuracy = ba.result().numpy()
print("Accuracy : ", accuracy)
print(f"Accuracy : {accuracy}")

# model.compile(optimizer="Adam", loss="mse", metrics=["mae"])

Expand Down
12 changes: 6 additions & 6 deletions examples/KerasGA/cancer_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def fitness_func(ga_instanse, solution, sol_idx):
return solution_fitness

def on_generation(ga_instance):
print("Generation = {generation}".format(generation=ga_instance.generations_completed))
print("Fitness = {fitness}".format(fitness=ga_instance.best_solution(ga_instance.last_generation_fitness)[1]))
print(f"Generation = {ga_instance.generations_completed}")
print(f"Fitness = {ga_instance.best_solution()[1]}")

# The dataset path.
dataset_path = r'../data/Skin_Cancer_Dataset'
Expand Down Expand Up @@ -71,8 +71,8 @@ def on_generation(ga_instance):

# Returning the details of the best solution.
solution, solution_fitness, solution_idx = ga_instance.best_solution(ga_instance.last_generation_fitness)
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))
print("Index of the best solution : {solution_idx}".format(solution_idx=solution_idx))
print(f"Fitness value of the best solution = {solution_fitness}")
print(f"Index of the best solution : {solution_idx}")

predictions = pygad.kerasga.predict(model=model,
solution=solution,
Expand All @@ -81,13 +81,13 @@ def on_generation(ga_instance):

# Calculate the categorical crossentropy for the trained model.
cce = tensorflow.keras.losses.CategoricalCrossentropy()
print("Categorical Crossentropy : ", cce(data_outputs, predictions).numpy())
print(f"Categorical Crossentropy : {cce(data_outputs, predictions).numpy()}")

# Calculate the classification accuracy for the trained model.
ca = tensorflow.keras.metrics.CategoricalAccuracy()
ca.update_state(data_outputs, predictions)
accuracy = ca.result().numpy()
print("Accuracy : ", accuracy)
print(f"Accuracy : {accuracy}")

# model.compile(optimizer="Adam", loss="mse", metrics=["mae"])

Expand Down
12 changes: 6 additions & 6 deletions examples/KerasGA/cancer_dataset_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ def fitness_func(ga_instanse, solution, sol_idx):
return solution_fitness

def on_generation(ga_instance):
print("Generation = {generation}".format(generation=ga_instance.generations_completed))
print("Fitness = {fitness}".format(fitness=ga_instance.best_solution(ga_instance.last_generation_fitness)[1]))
print(f"Generation = {ga_instance.generations_completed}")
print(f"Fitness = {ga_instance.best_solution()[1]}")

# The dataset path.
dataset_path = r'../data/Skin_Cancer_Dataset'
Expand Down Expand Up @@ -65,8 +65,8 @@ def on_generation(ga_instance):

# Returning the details of the best solution.
solution, solution_fitness, solution_idx = ga_instance.best_solution(ga_instance.last_generation_fitness)
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))
print("Index of the best solution : {solution_idx}".format(solution_idx=solution_idx))
print(f"Fitness value of the best solution = {solution_fitness}")
print(f"Index of the best solution : {solution_idx}")

predictions = pygad.kerasga.predict(model=model,
solution=solution,
Expand All @@ -75,13 +75,13 @@ def on_generation(ga_instance):

# Calculate the categorical crossentropy for the trained model.
cce = tensorflow.keras.losses.CategoricalCrossentropy()
print("Categorical Crossentropy : ", cce(data_outputs, predictions).numpy())
print(f"Categorical Crossentropy : {cce(data_outputs, predictions).numpy()}")

# Calculate the classification accuracy for the trained model.
ca = tensorflow.keras.metrics.CategoricalAccuracy()
ca.update_state(data_outputs, predictions)
accuracy = ca.result().numpy()
print("Accuracy : ", accuracy)
print(f"Accuracy : {accuracy}")

# model.compile(optimizer="Adam", loss="mse", metrics=["mae"])

Expand Down
12 changes: 6 additions & 6 deletions examples/KerasGA/image_classification_CNN.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ def fitness_func(ga_instanse, solution, sol_idx):
return solution_fitness

def on_generation(ga_instance):
print("Generation = {generation}".format(generation=ga_instance.generations_completed))
print("Fitness = {fitness}".format(fitness=ga_instance.best_solution()[1]))
print(f"Generation = {ga_instance.generations_completed}")
print(f"Fitness = {ga_instance.best_solution()[1]}")

# Build the keras model using the functional API.
input_layer = tensorflow.keras.layers.Input(shape=(100, 100, 3))
Expand Down Expand Up @@ -66,8 +66,8 @@ def on_generation(ga_instance):

# Returning the details of the best solution.
solution, solution_fitness, solution_idx = ga_instance.best_solution()
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))
print("Index of the best solution : {solution_idx}".format(solution_idx=solution_idx))
print(f"Fitness value of the best solution = {solution_fitness}")
print(f"Index of the best solution : {solution_idx}")

predictions = pygad.kerasga.predict(model=model,
solution=solution,
Expand All @@ -76,13 +76,13 @@ def on_generation(ga_instance):

# Calculate the categorical crossentropy for the trained model.
cce = tensorflow.keras.losses.CategoricalCrossentropy()
print("Categorical Crossentropy : ", cce(data_outputs, predictions).numpy())
print(f"Categorical Crossentropy : {cce(data_outputs, predictions).numpy()}")

# Calculate the classification accuracy for the trained model.
ca = tensorflow.keras.metrics.CategoricalAccuracy()
ca.update_state(data_outputs, predictions)
accuracy = ca.result().numpy()
print("Accuracy : ", accuracy)
print(f"Accuracy : {accuracy}")

# model.compile(optimizer="Adam", loss="mse", metrics=["mae"])

Expand Down
12 changes: 6 additions & 6 deletions examples/KerasGA/image_classification_Dense.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ def fitness_func(ga_instanse, solution, sol_idx):
return solution_fitness

def on_generation(ga_instance):
print("Generation = {generation}".format(generation=ga_instance.generations_completed))
print("Fitness = {fitness}".format(fitness=ga_instance.best_solution()[1]))
print(f"Generation = {ga_instance.generations_completed}")
print(f"Fitness = {ga_instance.best_solution()[1]}")

# Build the keras model using the functional API.
input_layer = tensorflow.keras.layers.Input(360)
Expand Down Expand Up @@ -57,8 +57,8 @@ def on_generation(ga_instance):

# Returning the details of the best solution.
solution, solution_fitness, solution_idx = ga_instance.best_solution()
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))
print("Index of the best solution : {solution_idx}".format(solution_idx=solution_idx))
print(f"Fitness value of the best solution = {solution_fitness}")
print(f"Index of the best solution : {solution_idx}")

# Fetch the parameters of the best solution.
predictions = pygad.kerasga.predict(model=model,
Expand All @@ -68,13 +68,13 @@ def on_generation(ga_instance):

# Calculate the categorical crossentropy for the trained model.
cce = tensorflow.keras.losses.CategoricalCrossentropy()
print("Categorical Crossentropy : ", cce(data_outputs, predictions).numpy())
print(f"Categorical Crossentropy : {cce(data_outputs, predictions).numpy()}")

# Calculate the classification accuracy for the trained model.
ca = tensorflow.keras.metrics.CategoricalAccuracy()
ca.update_state(data_outputs, predictions)
accuracy = ca.result().numpy()
print("Accuracy : ", accuracy)
print(f"Accuracy : {accuracy}")

# model.compile(optimizer="Adam", loss="mse", metrics=["mae"])

Expand Down
12 changes: 6 additions & 6 deletions examples/KerasGA/regression_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def fitness_func(ga_instanse, solution, sol_idx):
return solution_fitness

def on_generation(ga_instance):
print("Generation = {generation}".format(generation=ga_instance.generations_completed))
print("Fitness = {fitness}".format(fitness=ga_instance.best_solution()[1]))
print(f"Generation = {ga_instance.generations_completed}")
print(f"Fitness = {ga_instance.best_solution()[1]}")

# Create the Keras model.
input_layer = tensorflow.keras.layers.Input(3)
Expand Down Expand Up @@ -61,17 +61,17 @@ def on_generation(ga_instance):

# Returning the details of the best solution.
solution, solution_fitness, solution_idx = ga_instance.best_solution()
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))
print("Index of the best solution : {solution_idx}".format(solution_idx=solution_idx))
print(f"Fitness value of the best solution = {solution_fitness}")
print(f"Index of the best solution : {solution_idx}")

predictions = pygad.kerasga.predict(model=model,
solution=solution,
data=data_inputs)
print("Predictions : \n", predictions)
print(f"Predictions : \n{predictions}")

mae = tensorflow.keras.losses.MeanAbsoluteError()
abs_error = mae(data_outputs, predictions).numpy()
print("Absolute Error : ", abs_error)
print(f"Absolute Error : {abs_error}")

# model.compile(optimizer="Adam", loss="mse", metrics=["mae"])

Expand Down
14 changes: 7 additions & 7 deletions examples/TorchGA/XOR_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ def fitness_func(ga_instanse, solution, sol_idx):
return solution_fitness

def on_generation(ga_instance):
print("Generation = {generation}".format(generation=ga_instance.generations_completed))
print("Fitness = {fitness}".format(fitness=ga_instance.best_solution()[1]))
print(f"Generation = {ga_instance.generations_completed}")
print(f"Fitness = {ga_instance.best_solution()[1]}")

# Create the PyTorch model.
input_layer = torch.nn.Linear(2, 4)
Expand Down Expand Up @@ -68,19 +68,19 @@ def on_generation(ga_instance):

# Returning the details of the best solution.
solution, solution_fitness, solution_idx = ga_instance.best_solution()
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))
print("Index of the best solution : {solution_idx}".format(solution_idx=solution_idx))
print(f"Fitness value of the best solution = {solution_fitness}")
print(f"Index of the best solution : {solution_idx}")

predictions = pygad.torchga.predict(model=model,
solution=solution,
data=data_inputs)
print("Predictions : \n", predictions.detach().numpy())
print(f"Predictions : \n{predictions.detach().numpy()}")

# Calculate the binary crossentropy for the trained model.
print("Binary Crossentropy : ", loss_function(predictions, data_outputs).detach().numpy())
print(f"Binary Crossentropy : {loss_function(predictions, data_outputs).detach().numpy()}")

# Calculate the classification accuracy of the trained model.
a = torch.max(predictions, axis=1)
b = torch.max(data_outputs, axis=1)
accuracy = torch.true_divide(torch.sum(a.indices == b.indices), len(data_outputs))
print("Accuracy : ", accuracy.detach().numpy())
print(f"Accuracy : {accuracy.detach().numpy()}")
12 changes: 6 additions & 6 deletions examples/TorchGA/image_classification_CNN.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def fitness_func(ga_instanse, solution, sol_idx):
return solution_fitness

def on_generation(ga_instance):
print("Generation = {generation}".format(generation=ga_instance.generations_completed))
print("Fitness = {fitness}".format(fitness=ga_instance.best_solution()[1]))
print(f"Generation = {ga_instance.generations_completed}")
print(f"Fitness = {ga_instance.best_solution()[1]}")

# Build the PyTorch model.
input_layer = torch.nn.Conv2d(in_channels=3, out_channels=5, kernel_size=7)
Expand Down Expand Up @@ -78,17 +78,17 @@ def on_generation(ga_instance):

# Returning the details of the best solution.
solution, solution_fitness, solution_idx = ga_instance.best_solution()
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))
print("Index of the best solution : {solution_idx}".format(solution_idx=solution_idx))
print(f"Fitness value of the best solution = {solution_fitness}")
print(f"Index of the best solution : {solution_idx}")

predictions = pygad.torchga.predict(model=model,
solution=solution,
data=data_inputs)
# print("Predictions : \n", predictions)

# Calculate the crossentropy for the trained model.
print("Crossentropy : ", loss_function(predictions, data_outputs).detach().numpy())
print(f"Crossentropy : {loss_function(predictions, data_outputs).detach().numpy()}")

# Calculate the classification accuracy for the trained model.
accuracy = torch.true_divide(torch.sum(torch.max(predictions, axis=1).indices == data_outputs), len(data_outputs))
print("Accuracy : ", accuracy.detach().numpy())
print(f"Accuracy : {accuracy.detach().numpy()}")
12 changes: 6 additions & 6 deletions examples/TorchGA/image_classification_Dense.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def fitness_func(ga_instanse, solution, sol_idx):
return solution_fitness

def on_generation(ga_instance):
print("Generation = {generation}".format(generation=ga_instance.generations_completed))
print("Fitness = {fitness}".format(fitness=ga_instance.best_solution()[1]))
print(f"Generation = {ga_instance.generations_completed}")
print(f"Fitness = {ga_instance.best_solution()[1]}")

# Build the PyTorch model using the functional API.
input_layer = torch.nn.Linear(360, 50)
Expand Down Expand Up @@ -64,17 +64,17 @@ def on_generation(ga_instance):

# Returning the details of the best solution.
solution, solution_fitness, solution_idx = ga_instance.best_solution()
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))
print("Index of the best solution : {solution_idx}".format(solution_idx=solution_idx))
print(f"Fitness value of the best solution = {solution_fitness}")
print(f"Index of the best solution : {solution_idx}")

predictions = pygad.torchga.predict(model=model,
solution=solution,
data=data_inputs)
# print("Predictions : \n", predictions)

# Calculate the crossentropy loss of the trained model.
print("Crossentropy : ", loss_function(predictions, data_outputs).detach().numpy())
print(f"Crossentropy : {loss_function(predictions, data_outputs).detach().numpy()}")

# Calculate the classification accuracy for the trained model.
accuracy = torch.true_divide(torch.sum(torch.max(predictions, axis=1).indices == data_outputs), len(data_outputs))
print("Accuracy : ", accuracy.detach().numpy())
print(f"Accuracy : {accuracy.detach().numpy()}")
12 changes: 6 additions & 6 deletions examples/TorchGA/regression_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def fitness_func(ga_instanse, solution, sol_idx):
return solution_fitness

def on_generation(ga_instance):
print("Generation = {generation}".format(generation=ga_instance.generations_completed))
print("Fitness = {fitness}".format(fitness=ga_instance.best_solution()[1]))
print(f"Generation = {ga_instance.generations_completed}")
print(f"Fitness = {ga_instance.best_solution()[1]}")

# Create the PyTorch model.
input_layer = torch.nn.Linear(3, 2)
Expand Down Expand Up @@ -64,13 +64,13 @@ def on_generation(ga_instance):

# Returning the details of the best solution.
solution, solution_fitness, solution_idx = ga_instance.best_solution()
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))
print("Index of the best solution : {solution_idx}".format(solution_idx=solution_idx))
print(f"Fitness value of the best solution = {solution_fitness}")
print(f"Index of the best solution : {solution_idx}")

predictions = pygad.torchga.predict(model=model,
solution=solution,
data=data_inputs)
print("Predictions : \n", predictions.detach().numpy())
print("Predictions : \n{predictions.detach().numpy()}")

abs_error = loss_function(predictions, data_outputs)
print("Absolute Error : ", abs_error.detach().numpy())
print("Absolute Error : {abs_error.detach().numpy()}")
6 changes: 3 additions & 3 deletions examples/clustering/example_clustering_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ def fitness_func(ga_instance, solution, solution_idx):
ga_instance.run()

best_solution, best_solution_fitness, best_solution_idx = ga_instance.best_solution()
print("Best solution is {bs}".format(bs=best_solution))
print("Fitness of the best solution is {bsf}".format(bsf=best_solution_fitness))
print("Best solution found after {gen} generations".format(gen=ga_instance.best_solution_generation))
print(f"Best solution is {best_solution}")
print(f"Fitness of the best solution is {best_solution_fitness}")
print(f"Best solution found after {ga_instance.best_solution_generation} generations")

cluster_centers, all_clusters_dists, cluster_indices, clusters, clusters_sum_dist = cluster_data(best_solution, best_solution_idx)

Expand Down
6 changes: 3 additions & 3 deletions examples/clustering/example_clustering_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ def fitness_func(ga_instance, solution, solution_idx):
ga_instance.run()

best_solution, best_solution_fitness, best_solution_idx = ga_instance.best_solution()
print("Best solution is {bs}".format(bs=best_solution))
print("Fitness of the best solution is {bsf}".format(bsf=best_solution_fitness))
print("Best solution found after {gen} generations".format(gen=ga_instance.best_solution_generation))
print(f"Best solution is {best_solution}")
print(f"Fitness of the best solution is {best_solution_fitness}")
print(f"Best solution found after {ga_instance.best_solution_generation} generations")

cluster_centers, all_clusters_dists, cluster_indices, clusters, clusters_sum_dist = cluster_data(best_solution, best_solution_idx)

Expand Down
6 changes: 3 additions & 3 deletions examples/cnn/example_image_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@
num_wrong = numpy.where(predictions != train_outputs)[0]
num_correct = train_outputs.size - num_wrong.size
accuracy = 100 * (num_correct/train_outputs.size)
print("Number of correct classifications : {num_correct}.".format(num_correct=num_correct))
print("Number of wrong classifications : {num_wrong}.".format(num_wrong=num_wrong.size))
print("Classification accuracy : {accuracy}.".format(accuracy=accuracy))
print(f"Number of correct classifications : {num_correct}.")
print(f"Number of wrong classifications : {num_wrong.size}.")
print(f"Classification accuracy : {accuracy}.")
Loading

0 comments on commit 900df55

Please sign in to comment.