python - 人工神经网络: Bug with GridSearchCV returns the first parameters each time

标签 python scikit-learn neural-network artificial-intelligence

我正在用 Python 做一个 ANN,并且正在使用 GridSearchCV (sklearn) 为我的 ANN 寻找最佳参数。 问题是,“best_parameters”属性每次都会返回每个参数的第一个元素(因此,如果我更改元素的顺序,则返回会有所不同)。

这是我的代码:

# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import GridSearchCV

# Importing the dataset
dataset = pd.read_csv('data.csv')
X = dataset.iloc[:, 17:27].values
y = dataset.iloc[:, 3].values

# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)

# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

# Find best parameters
def build_classifier(optimizer):
    # Init ANN
    classifier = Sequential()
    # Add input and first hidden layers
    classifier.add(Dense(units=6, activation="relu", kernel_initializer="uniform", input_dim=10))
    # Add another hidder layer
    classifier.add(Dense(units=6, activation="relu", kernel_initializer="uniform"))
    # Add output layer
    classifier.add(Dense(units=1, activation="sigmoid", kernel_initializer="uniform"))
    # Compile ANN
    classifier.compile(optimizer=optimizer, loss="mean_squared_error")
    return classifier

# Looking for best parameters with GridSearchCV
classifier = KerasClassifier(build_fn=build_classifier)
parameters = {"batch_size":[1, 5, 10], "epochs":[100,200], "optimizer": ["rmsprop", "sgd", "adam"]}
grid_search = GridSearchCV(estimator=classifier, param_grid=parameters, scoring="neg_mean_squared_error", cv=10)
grid_search = grid_search.fit(X_train, y_train)

best_parameters = grid_search.best_params_
best_precision = grid_search.best_score_

所以在 parameters = {"batch_size":[1, 5, 10], "epochs":[100,200], "optimizer": ["rmsprop", "sgd", "adam"]} 中我有我想尝试的参数,并且属性“best_parameters”始终返回每个参数的第一个元素(检查我尝试参数的多个顺序的图片)。 best_parameters return according to parameter order

我不明白这是从哪里来的以及如何纠正它。

最佳答案

我已经找到了解决方案,这是我犯的一个错误...... 我使用 KerasClassifier 来找到最佳参数,而我想做回归...所以我猜 KerasClassifier 没有成功完成他必须做的事情,然后返回第一个参数。 由于我正在做回归,所以我必须使用 KerasRegressor 来代替。

关于python - 人工神经网络: Bug with GridSearchCV returns the first parameters each time,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52498544/

相关文章:

matlab - matlab中有离散化的方法吗?

python - 无服务器部署不起作用(Python、Lambda)

Python多处理类型错误: join() takes exactly 1 argument (2 given)

python - 值错误: negative dimensions are not allowed in scikit linear regression CV model with sparse matrices

python - 如何用sklearn得到线性判别分析中的边界线方程

machine-learning - 如果您使用批量归一化,是否需要标准化输入?

python - 与 BeautifulSoup.find 混淆?

python - 验证准确性没有提高

python - 为什么使用 scikit-learn 的 GradientBoostingRegressor 从相同的输入中得到不同的输出?

C# Encog 神经网络——尽管神经网络的整体误差很低,但预期输出与实际误差相去甚远