没有numpy但是python中有numpy错误

问题描述 投票:0回答:3

大家好,我是一名新程序员,很抱歉,如果这是如此简单的错误,但我需要此代码的帮助

# 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)

所以运行后我遇到了奇怪的错误

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

AttributeError:模块“numpy”没有属性“int”。

np.int
是内置
int
的已弃用别名。为了避免现有代码中出现此错误,请单独使用
int
。这样做不会改变任何行为并且是安全的。替换
np.int
时,您可能希望使用例如
np.int64
np.int32
指定精度。如果您想查看当前的使用情况,请查看发行说明链接以获取更多信息。 别名最初在 NumPy 1.20 中已弃用;有关更多详细信息和指导,请参阅原始发行说明: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations

正如你所看到的,我在这部分之前或之后的代码中没有任何类型的 numpy,但遇到此错误有任何解决方案吗?

我尝试安装、卸载和升级 numpy 但不起作用

python numpy attributeerror
3个回答
0
投票

不过你确实有 Numpy。 来自 scikit-optimize 文档:

scikit 优化需要:

Python >= 3.6

NumPy (>= 1.13.3)

您看到的错误来自您正在导入的 skopt 模块,这就是需要修复的内容(由库维护人员)


0
投票

Numpy 可以是您正在使用的库的依赖项。

这个问题也是众所周知的,参见: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_

0
投票

现在我明白了,因为 scikit-optimize 是一个死项目。 ty 用户2357112

那么超参数优化的最佳替代方案是什么?

© www.soinside.com 2019 - 2024. All rights reserved.