Skip to content

Commit 900df55

Browse files
committed
Replace format() str method by f-string
1 parent aab09bd commit 900df55

26 files changed

+141
-141
lines changed

examples/KerasGA/XOR_classification.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ def fitness_func(ga_instanse, solution, sol_idx):
1616
return solution_fitness
1717

1818
def on_generation(ga_instance):
19-
print("Generation = {generation}".format(generation=ga_instance.generations_completed))
20-
print("Fitness = {fitness}".format(fitness=ga_instance.best_solution()[1]))
19+
print(f"Generation = {ga_instance.generations_completed}")
20+
print(f"Fitness = {ga_instance.best_solution()[1]}")
2121

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

6363
# Returning the details of the best solution.
6464
solution, solution_fitness, solution_idx = ga_instance.best_solution()
65-
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))
66-
print("Index of the best solution : {solution_idx}".format(solution_idx=solution_idx))
65+
print(f"Fitness value of the best solution = {solution_fitness}")
66+
print(f"Index of the best solution : {solution_idx}")
6767

6868
predictions = pygad.kerasga.predict(model=model,
6969
solution=solution,
7070
data=data_inputs)
71-
print("Predictions : \n", predictions)
71+
print(f"Predictions : \n{predictions}")
7272

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

7777
# Calculate the classification accuracy for the trained model.
7878
ba = tensorflow.keras.metrics.BinaryAccuracy()
7979
ba.update_state(data_outputs, predictions)
8080
accuracy = ba.result().numpy()
81-
print("Accuracy : ", accuracy)
81+
print(f"Accuracy : {accuracy}")
8282

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

examples/KerasGA/cancer_dataset.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ def fitness_func(ga_instanse, solution, sol_idx):
1717
return solution_fitness
1818

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

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

7272
# Returning the details of the best solution.
7373
solution, solution_fitness, solution_idx = ga_instance.best_solution(ga_instance.last_generation_fitness)
74-
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))
75-
print("Index of the best solution : {solution_idx}".format(solution_idx=solution_idx))
74+
print(f"Fitness value of the best solution = {solution_fitness}")
75+
print(f"Index of the best solution : {solution_idx}")
7676

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

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

8686
# Calculate the classification accuracy for the trained model.
8787
ca = tensorflow.keras.metrics.CategoricalAccuracy()
8888
ca.update_state(data_outputs, predictions)
8989
accuracy = ca.result().numpy()
90-
print("Accuracy : ", accuracy)
90+
print(f"Accuracy : {accuracy}")
9191

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

examples/KerasGA/cancer_dataset_generator.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ def fitness_func(ga_instanse, solution, sol_idx):
1616
return solution_fitness
1717

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

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

6666
# Returning the details of the best solution.
6767
solution, solution_fitness, solution_idx = ga_instance.best_solution(ga_instance.last_generation_fitness)
68-
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))
69-
print("Index of the best solution : {solution_idx}".format(solution_idx=solution_idx))
68+
print(f"Fitness value of the best solution = {solution_fitness}")
69+
print(f"Index of the best solution : {solution_idx}")
7070

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

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

8080
# Calculate the classification accuracy for the trained model.
8181
ca = tensorflow.keras.metrics.CategoricalAccuracy()
8282
ca.update_state(data_outputs, predictions)
8383
accuracy = ca.result().numpy()
84-
print("Accuracy : ", accuracy)
84+
print(f"Accuracy : {accuracy}")
8585

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

examples/KerasGA/image_classification_CNN.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ def fitness_func(ga_instanse, solution, sol_idx):
1616
return solution_fitness
1717

1818
def on_generation(ga_instance):
19-
print("Generation = {generation}".format(generation=ga_instance.generations_completed))
20-
print("Fitness = {fitness}".format(fitness=ga_instance.best_solution()[1]))
19+
print(f"Generation = {ga_instance.generations_completed}")
20+
print(f"Fitness = {ga_instance.best_solution()[1]}")
2121

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

6767
# Returning the details of the best solution.
6868
solution, solution_fitness, solution_idx = ga_instance.best_solution()
69-
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))
70-
print("Index of the best solution : {solution_idx}".format(solution_idx=solution_idx))
69+
print(f"Fitness value of the best solution = {solution_fitness}")
70+
print(f"Index of the best solution : {solution_idx}")
7171

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

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

8181
# Calculate the classification accuracy for the trained model.
8282
ca = tensorflow.keras.metrics.CategoricalAccuracy()
8383
ca.update_state(data_outputs, predictions)
8484
accuracy = ca.result().numpy()
85-
print("Accuracy : ", accuracy)
85+
print(f"Accuracy : {accuracy}")
8686

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

examples/KerasGA/image_classification_Dense.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ def fitness_func(ga_instanse, solution, sol_idx):
1616
return solution_fitness
1717

1818
def on_generation(ga_instance):
19-
print("Generation = {generation}".format(generation=ga_instance.generations_completed))
20-
print("Fitness = {fitness}".format(fitness=ga_instance.best_solution()[1]))
19+
print(f"Generation = {ga_instance.generations_completed}")
20+
print(f"Fitness = {ga_instance.best_solution()[1]}")
2121

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

5858
# Returning the details of the best solution.
5959
solution, solution_fitness, solution_idx = ga_instance.best_solution()
60-
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))
61-
print("Index of the best solution : {solution_idx}".format(solution_idx=solution_idx))
60+
print(f"Fitness value of the best solution = {solution_fitness}")
61+
print(f"Index of the best solution : {solution_idx}")
6262

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

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

7373
# Calculate the classification accuracy for the trained model.
7474
ca = tensorflow.keras.metrics.CategoricalAccuracy()
7575
ca.update_state(data_outputs, predictions)
7676
accuracy = ca.result().numpy()
77-
print("Accuracy : ", accuracy)
77+
print(f"Accuracy : {accuracy}")
7878

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

examples/KerasGA/regression_example.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ def fitness_func(ga_instanse, solution, sol_idx):
1717
return solution_fitness
1818

1919
def on_generation(ga_instance):
20-
print("Generation = {generation}".format(generation=ga_instance.generations_completed))
21-
print("Fitness = {fitness}".format(fitness=ga_instance.best_solution()[1]))
20+
print(f"Generation = {ga_instance.generations_completed}")
21+
print(f"Fitness = {ga_instance.best_solution()[1]}")
2222

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

6262
# Returning the details of the best solution.
6363
solution, solution_fitness, solution_idx = ga_instance.best_solution()
64-
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))
65-
print("Index of the best solution : {solution_idx}".format(solution_idx=solution_idx))
64+
print(f"Fitness value of the best solution = {solution_fitness}")
65+
print(f"Index of the best solution : {solution_idx}")
6666

6767
predictions = pygad.kerasga.predict(model=model,
6868
solution=solution,
6969
data=data_inputs)
70-
print("Predictions : \n", predictions)
70+
print(f"Predictions : \n{predictions}")
7171

7272
mae = tensorflow.keras.losses.MeanAbsoluteError()
7373
abs_error = mae(data_outputs, predictions).numpy()
74-
print("Absolute Error : ", abs_error)
74+
print(f"Absolute Error : {abs_error}")
7575

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

examples/TorchGA/XOR_classification.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ def fitness_func(ga_instanse, solution, sol_idx):
1414
return solution_fitness
1515

1616
def on_generation(ga_instance):
17-
print("Generation = {generation}".format(generation=ga_instance.generations_completed))
18-
print("Fitness = {fitness}".format(fitness=ga_instance.best_solution()[1]))
17+
print(f"Generation = {ga_instance.generations_completed}")
18+
print(f"Fitness = {ga_instance.best_solution()[1]}")
1919

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

6969
# Returning the details of the best solution.
7070
solution, solution_fitness, solution_idx = ga_instance.best_solution()
71-
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))
72-
print("Index of the best solution : {solution_idx}".format(solution_idx=solution_idx))
71+
print(f"Fitness value of the best solution = {solution_fitness}")
72+
print(f"Index of the best solution : {solution_idx}")
7373

7474
predictions = pygad.torchga.predict(model=model,
7575
solution=solution,
7676
data=data_inputs)
77-
print("Predictions : \n", predictions.detach().numpy())
77+
print(f"Predictions : \n{predictions.detach().numpy()}")
7878

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

8282
# Calculate the classification accuracy of the trained model.
8383
a = torch.max(predictions, axis=1)
8484
b = torch.max(data_outputs, axis=1)
8585
accuracy = torch.true_divide(torch.sum(a.indices == b.indices), len(data_outputs))
86-
print("Accuracy : ", accuracy.detach().numpy())
86+
print(f"Accuracy : {accuracy.detach().numpy()}")

examples/TorchGA/image_classification_CNN.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ def fitness_func(ga_instanse, solution, sol_idx):
1515
return solution_fitness
1616

1717
def on_generation(ga_instance):
18-
print("Generation = {generation}".format(generation=ga_instance.generations_completed))
19-
print("Fitness = {fitness}".format(fitness=ga_instance.best_solution()[1]))
18+
print(f"Generation = {ga_instance.generations_completed}")
19+
print(f"Fitness = {ga_instance.best_solution()[1]}")
2020

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

7979
# Returning the details of the best solution.
8080
solution, solution_fitness, solution_idx = ga_instance.best_solution()
81-
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))
82-
print("Index of the best solution : {solution_idx}".format(solution_idx=solution_idx))
81+
print(f"Fitness value of the best solution = {solution_fitness}")
82+
print(f"Index of the best solution : {solution_idx}")
8383

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

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

9292
# Calculate the classification accuracy for the trained model.
9393
accuracy = torch.true_divide(torch.sum(torch.max(predictions, axis=1).indices == data_outputs), len(data_outputs))
94-
print("Accuracy : ", accuracy.detach().numpy())
94+
print(f"Accuracy : {accuracy.detach().numpy()}")

examples/TorchGA/image_classification_Dense.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ def fitness_func(ga_instanse, solution, sol_idx):
1515
return solution_fitness
1616

1717
def on_generation(ga_instance):
18-
print("Generation = {generation}".format(generation=ga_instance.generations_completed))
19-
print("Fitness = {fitness}".format(fitness=ga_instance.best_solution()[1]))
18+
print(f"Generation = {ga_instance.generations_completed}")
19+
print(f"Fitness = {ga_instance.best_solution()[1]}")
2020

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

6565
# Returning the details of the best solution.
6666
solution, solution_fitness, solution_idx = ga_instance.best_solution()
67-
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))
68-
print("Index of the best solution : {solution_idx}".format(solution_idx=solution_idx))
67+
print(f"Fitness value of the best solution = {solution_fitness}")
68+
print(f"Index of the best solution : {solution_idx}")
6969

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

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

7878
# Calculate the classification accuracy for the trained model.
7979
accuracy = torch.true_divide(torch.sum(torch.max(predictions, axis=1).indices == data_outputs), len(data_outputs))
80-
print("Accuracy : ", accuracy.detach().numpy())
80+
print(f"Accuracy : {accuracy.detach().numpy()}")

examples/TorchGA/regression_example.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ def fitness_func(ga_instanse, solution, sol_idx):
1515
return solution_fitness
1616

1717
def on_generation(ga_instance):
18-
print("Generation = {generation}".format(generation=ga_instance.generations_completed))
19-
print("Fitness = {fitness}".format(fitness=ga_instance.best_solution()[1]))
18+
print(f"Generation = {ga_instance.generations_completed}")
19+
print(f"Fitness = {ga_instance.best_solution()[1]}")
2020

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

6565
# Returning the details of the best solution.
6666
solution, solution_fitness, solution_idx = ga_instance.best_solution()
67-
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))
68-
print("Index of the best solution : {solution_idx}".format(solution_idx=solution_idx))
67+
print(f"Fitness value of the best solution = {solution_fitness}")
68+
print(f"Index of the best solution : {solution_idx}")
6969

7070
predictions = pygad.torchga.predict(model=model,
7171
solution=solution,
7272
data=data_inputs)
73-
print("Predictions : \n", predictions.detach().numpy())
73+
print("Predictions : \n{predictions.detach().numpy()}")
7474

7575
abs_error = loss_function(predictions, data_outputs)
76-
print("Absolute Error : ", abs_error.detach().numpy())
76+
print("Absolute Error : {abs_error.detach().numpy()}")

examples/clustering/example_clustering_2.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ def fitness_func(ga_instance, solution, solution_idx):
107107
ga_instance.run()
108108

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

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

examples/clustering/example_clustering_3.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,9 @@ def fitness_func(ga_instance, solution, solution_idx):
119119
ga_instance.run()
120120

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

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

examples/cnn/example_image_classification.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,6 @@
6767
num_wrong = numpy.where(predictions != train_outputs)[0]
6868
num_correct = train_outputs.size - num_wrong.size
6969
accuracy = 100 * (num_correct/train_outputs.size)
70-
print("Number of correct classifications : {num_correct}.".format(num_correct=num_correct))
71-
print("Number of wrong classifications : {num_wrong}.".format(num_wrong=num_wrong.size))
72-
print("Classification accuracy : {accuracy}.".format(accuracy=accuracy))
70+
print(f"Number of correct classifications : {num_correct}.")
71+
print(f"Number of wrong classifications : {num_wrong.size}.")
72+
print(f"Classification accuracy : {accuracy}.")

0 commit comments

Comments
 (0)