gpt4 book ai didi

python - GridSearchCV 神经元数量

转载 作者:太空宇宙 更新时间:2023-11-03 14:35:13 24 4
gpt4 key购买 nike

我正在尝试自学如何在基本的多层神经网络中对神经元的数量进行网格搜索。我正在使用 Python 的 GridSearchCV 和 KerasClasifier 以及 Keras。下面的代码适用于其他数据集非常好,但由于某些原因我无法使其适用于 Iris 数据集,而且我找不到原因,我在这里遗漏了一些东西。我得到的结果是:

最佳:0.000000 使用 {'n_neurons': 3}
0.000000 (0.000000) 与:{'n_neurons':3}
0.000000 (0.000000),其中:{'n_neurons': 5}

from pandas import read_csv

import numpy
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import StandardScaler

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

dataframe=read_csv("iris.csv", header=None)
dataset=dataframe.values
X=dataset[:,0:4].astype(float)
Y=dataset[:,4]

seed=7
numpy.random.seed(seed)

#encode class values as integers
encoder = LabelEncoder()
encoder.fit(Y)
encoded_Y = encoder.transform(Y)

#one-hot encoding
dummy_y = np_utils.to_categorical(encoded_Y)

#scale the data
scaler = StandardScaler()
X = scaler.fit_transform(X)

def create_model(n_neurons=1):
#create model
model = Sequential()
model.add(Dense(n_neurons, input_dim=X.shape[1], activation='relu')) # hidden layer
model.add(Dense(3, activation='softmax')) # output layer
# Compile model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
return model

model = KerasClassifier(build_fn=create_model, epochs=100, batch_size=10, initial_epoch=0, verbose=0)
# define the grid search parameters
neurons=[3, 5]

#this does 3-fold classification. One can change k.
param_grid = dict(n_neurons=neurons)
grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=-1)
grid_result = grid.fit(X, dummy_y)
# summarize results
print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
means = grid_result.cv_results_['mean_test_score']
stds = grid_result.cv_results_['std_test_score']
params = grid_result.cv_results_['params']
for mean, stdev, param in zip(means, stds, params):
print("%f (%f) with: %r" % (mean, stdev, param))

出于说明和计算效率的目的,我仅搜索两个值。对于问这么简单的问题,我深表歉意。顺便说一句,我是 Python 新手,从 R 转过来,因为我意识到深度学习社区正在使用 python。

最佳答案

哈哈,这可能是我在 Stack Overflow 上经历过的最有趣的事情了:) 检查:

grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=-1, cv=5)

您应该会看到不同的行为。您的模型获得满分的原因(就 cross_entropy 而言,0 相当于可能的最佳模型)是因为您没有对数据进行洗牌,并且因为 Iris 由三个平衡的类组成,每个 feed 都有一个类似目标的类:

[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 (first fold ends here) 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 (second fold ends here)2 2 2 2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
2 2]

每个模型都很容易解决此类问题 - 这就是为什么你有一个完美的匹配。

尝试先打乱数据 - 这应该会产生预期的行为。

关于python - GridSearchCV 神经元数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47002177/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com