-
Notifications
You must be signed in to change notification settings - Fork 187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG]: TypeError: Problem.init() missing 1 required positional argument: 'bounds' #167
Comments
@Fa20 Could you please put your code into a good format of "coding style". I can help you but with this format I don't know what are you doing with this code. |
@thieu1995 Thank you so much I have updated the code but got differen error which I can not fix it error I got ``` { File c:\Users\FI\AppData\Local\Programs\Python\Python310\lib\site-packages\mealpy\optimizer.py:223, in Optimizer.solve(self, problem, mode, n_workers, termination, starting_solutions, seed) File c:\Users\FI\AppData\Local\Programs\Python\Python310\lib\site-packages\mealpy\optimizer.py:159, in Optimizer.check_problem(self, problem, seed) ValueError: problem needs to be a dict or an instance of Problem class."
|
@Fa20 , Hope that helps! import numpy as np
from tensorflow import keras
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from mealpy import Problem, IntegerVar, FloatVar, OriginalGWO
# Create the data generators with validation split
datagen_train = ImageDataGenerator(
samplewise_center=True,
rotation_range=10,
zoom_range=0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=True,
validation_split=0.2
)
# Load the training data and split into train and validation sets
train_it = datagen_train.flow_from_directory(
"data/train/",
target_size=(224, 224),
color_mode="rgb",
class_mode="categorical",
subset='training'
)
valid_it = datagen_train.flow_from_directory(
"data/train/",
target_size=(224, 224),
color_mode="rgb",
class_mode="categorical",
subset='validation'
)
# Define the model as a function
def create_model(learning_rate, num_neurons):
base_model = keras.applications.VGG16(
weights='imagenet',
input_shape=(224, 224, 3),
include_top=False)
base_model.trainable = False
inputs = keras.Input(shape=(224, 224, 3))
x = base_model(inputs, training=False)
x = keras.layers.GlobalAveragePooling2D()(x)
x = keras.layers.Dense(num_neurons, activation='relu')(x)
outputs = keras.layers.Dense(6, activation='softmax')(x)
model = keras.Model(inputs, outputs)
model.compile(optimizer=keras.optimizers.RMSprop(learning_rate=learning_rate),
loss='categorical_crossentropy',
metrics=['accuracy'])
return model
# Define the objective (fitness) function
class NeuronOptimization(Problem):
def __init__(self, bounds=None, minmax="max", **kwargs):
super().__init__(bounds, minmax, **kwargs)
def obj_func(self, solution):
x = self.decode_solution(solution)
lr, n_nodes = x["lr"], x["n_nodes"]
# Create and train the model
model = create_model(lr, n_nodes)
history = model.fit(train_it, validation_data=valid_it,
steps_per_epoch=train_it.samples // train_it.batch_size,
validation_steps=valid_it.samples // valid_it.batch_size,
epochs=1, verbose=1)
# Return the validation accuracy as fitness
val_accuracy = history.history['val_accuracy'][-1]
return val_accuracy # Maximizing accuracy, so returning as-is
# Define the search space for neurons
bounds = [
FloatVar(lb=0.0001, ub=0.5, name="lr"),
IntegerVar(lb=5, ub=100, name="n_nodes")
]
# Create the optimization problem instance
problem = NeuronOptimization(bounds=bounds, minmax="max", name="Neurons_Optimization")
# Set up the GWO optimizer
gwo = OriginalGWO(problem=problem, epoch=10, pop_size=20)
# Solve the optimization problem
best_position, best_fitness = gwo.solve()
# Output the result
print(f"Best number of neurons: {int(best_position[0])}, Best validation accuracy: {best_fitness}") |
@thieu1995 thanks for your answer . I'm still getting the same error as before .I rung the above code you provied me but I replaced
ValueError Traceback (most recent call last) File c:\Users\FI\AppData\Local\Programs\Python\Python310\lib\site-packages\mealpy\optimizer.py:223, in Optimizer.solve(self, problem, mode, n_workers, termination, starting_solutions, seed) File c:\Users\FI\AppData\Local\Programs\Python\Python310\lib\site-packages\mealpy\optimizer.py:159, in Optimizer.check_problem(self, problem, seed) ValueError: problem needs to be a dict or an instance of Problem class.``` |
@Fa20 , OMG. # Set up the GWO optimizer
gwo = OriginalGWO(epoch=10, pop_size=20)
# Solve the optimization problem
best_position, best_fitness = gwo.solve(problem=problem)
# Output the result
print(f"Best number of neurons: {int(best_position[0])}, Best validation accuracy: {best_fitness}") |
@Fa20, again. Look like your code is from previous version. I correct it again. # Solve the optimization problem
best_agent = gwo.solve(problem=problem)
# Output the result
print(f"Best agent: {model.g_best}")
print(f"Best solution: {model.g_best.solution}")
print(f"Best fitness: {model.g_best.target.fitness}")
print(f"Best real parameter: {model.problem.decode_solution(best_agent.solution)}") |
@thieu1995 Thank you very much . what is model. ? |
@Fa20 , |
@thieu1995 still the code give me error : TypeError: cannot unpack non-iterable Agent object |
Description of the bug
TypeError Traceback (most recent call last)
Cell In[34], line 48
41 bounds = [
42
43 FloatVar(lb=0., ub=1.0, name="learning-rate")]
47 gwo= OriginalGWO(epoch, pop_size)
---> 48 best_position, best_fitness = gwo.solve(svr_problem)
49 print(f"Solution: {best_position}, Fitness: {best_fitness}")
File c:\Users\FI\AppData\Local\Programs\Python\Python310\lib\site-packages\mealpy\optimizer.py:223, in Optimizer.solve(self, problem, mode, n_workers, termination, starting_solutions, seed)
202 def solve(self, problem: Union[Dict, Problem] = None, mode: str = 'single', n_workers: int = None,
203 termination: Union[Dict, Termination] = None, starting_solutions: Union[List, np.ndarray, Tuple] = None,
204 seed: int = None) -> Agent:
205 """
206 Args:
207 problem: an instance of Problem class or a dictionary
(...)
221 g_best: g_best, the best found agent, that hold the best solution and the best target. Access by: .g_best.solution, .g_best.target
222 """
--> 223 self.check_problem(problem, seed)
224 self.check_mode_and_workers(mode, n_workers)
225 self.check_termination("start", termination, None)
File c:\Users\FI\AppData\Local\Programs\Python\Python310\lib\site-packages\mealpy\optimizer.py:157, in Optimizer.check_problem(self, problem, seed)
155 elif type(problem) == dict:
156 problem["seed"] = seed
--> 157 self.problem = Problem(**problem)
158 else:
159 raise ValueError("problem needs to be a dict or an instance of Problem class.")
TypeError: Problem.init() missing 1 required positional argument: 'bounds'
Steps To Reproduce
Define the model
def create_model(learning_rate, num_neurons):
base_model = keras.applications.VGG16(
weights='imagenet',
input_shape=(224, 224, 3),
include_top=False)
2.# Fitness function for optimization
def fitness_function(solution):
learning_rate= 0.01
num_neurons= solution
3.# number of neurons
problem_size = 2
lb = [10] # lower bounds: num_neurons
ub = [ 512] # upper bounds: num_neurons
epoch = 1
pop_size = 5 # Size of the population in each generation
pc = 0.95 # Crossover probability
pm = 0.05 # Mutation probability
svr_problem = {
"fit_func": fitness_function,
'lb':[1], # Lower bound of our parameters
'ub':[6], # upper bound of our parameters
"minmax": "min"
}
gwo= OriginalGWO(epoch, pop_size)
best_position, best_fitness = gwo.solve(svr_problem)
print(f"Solution: {best_position}, Fitness: {best_fitness}")
Additional Information
Here is my code to use meaply with pretrained model to find best values of hyperpramters:
from mealpy.evolutionary_based.GA import BaseGA
from mealpy.evolutionary_based.GA import BaseGA
from mealpy.utils.problem import Problem
from mealpy.utils.space import FloatVar, IntegerVar
from mealpy.swarm_based.GWO import OriginalGWO
from tensorflow.keras.optimizers import Adam
from tensorflow import keras
from mealpy.evolutionary_based import GA
Define the model as a function
def create_model(learning_rate, num_neurons):
base_model = keras.applications.VGG16(
weights='imagenet',
input_shape=(224, 224, 3),
include_top=False)
Fitness function for optimization
def fitness_function(solution):
learning_rate= 0.01
num_neurons= solution
Define boundaries for learning rate and number of neurons
problem_size = 2
lb = [10] # lower bounds: num_neurons
ub = [ 512] # upper bounds: num_neurons
epoch = 1
pop_size = 5 # Size of the population in each generation
pc = 0.95 # Crossover probability
pm = 0.05 # Mutation probability
param_bounds = {
}
svr_problem = {
"fit_func": fitness_function,
'lb':[1], # Lower bound of our parameters
'ub':[6], # upper bound of our parameters
"minmax": "min"
}
bounds = [
gwo= OriginalGWO(epoch, pop_size)
best_position, best_fitness = gwo.solve(svr_problem)
print(f"Solution: {best_position}, Fitness: {best_fitness}")
The text was updated successfully, but these errors were encountered: