0

Hi guys I'm a new programmer so sorry if this is so simple error but I need help in this code

# Step 6: Hyperparameter Optimization

from skopt import gp_minimize
from skopt.space import Real, Integer

# Define hyperparameter search space
space = [
    Real(0.001, 0.1, name='learning_rate'),
    Integer(32, 128, name='batch_size'),
    Integer(50, 150, name='lstm_units'),
    Integer(1, 3, name='lstm_layers')
]

# Define objective function
def lstm_objective(params):
    
    # Unpack hyperparameters
    learning_rate = params[0]
    batch_size = params[1]
    lstm_units = params[2]
    lstm_layers = params[3]
    
    # Update LSTM model architecture
    model = Sequential()
    for i in range(lstm_layers):
        model.add(LSTM(units=lstm_units, return_sequences=True)) 
    model.add(Dense(1))

    # Compile and fit 
    model.compile(optimizer=Adam(learning_rate), loss='mse') 
    model.fit(X_train, y_train, batch_size=batch_size, epochs=10)

    # Evaluate MSE on validation set
    mse = model.evaluate(X_val, y_val, verbose=0)
    return mse

# Run optimization
best_params = gp_minimize(lstm_objective, space, n_calls=15, random_state=0)

print(best_params.x)

so after run i got strange error in line best_params = gp_minimize(lstm_objective, space, n_calls=15, random_state=0)

AttributeError: module 'numpy' has no attribute 'int'. np.int was a deprecated alias for the builtin int. To avoid this error in existing code, use int by itself. Doing this will not modify any behavior and is safe. When replacing np.int, you may wish to use e.g. np.int64 or np.int32 to specify the precision. If you wish to review your current use, check the release note link for additional information. The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations

as you can see I don't have any kind of numpy in this code befor or even after this part but got this error any solution ?

I try install and uninstall and upgrade numpy but don't work

3
  • 2
    You're not using NumPy directly, but you're using a library that uses NumPy, and that library needs to be fixed. (It won't be, though, because scikit-optimize is a dead project.) Commented Aug 10, 2023 at 9:57
  • so what is best alternative for that ? Commented Aug 10, 2023 at 10:00
  • 1
    github.com/scikit-optimize/scikit-optimize/issues/1171 Commented Aug 10, 2023 at 10:01

2 Answers 2

1

Numpy can be a dependency of the library you are using.

Also this issue is well known, cf: https://pypi.org/project/scikit-optimizer/

This repository is forked from the official implementation of the Scikit-Optimize. Please refer to the official documentation for more details.

There is version incompetibility between skopt and numpy module, therefore, some minor changes have been made to furhter working on the BayesSearchCV. Here are the list of modification.

    Replacing np.int to np.int_
Sign up to request clarification or add additional context in comments.

Comments

0

You do have Numpy though. From scikit-optimize docs:

scikit-optimize requires:

Python >= 3.6

NumPy (>= 1.13.3)

The error you are seeing comes from skopt module you are importing and thats what needs to be fixed (by library maintainers)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.