【问题标题】:Grid Search in Multi class classification problems using Neural networks使用神经网络的多类分类问题中的网格搜索
【发布时间】:2018-06-24 14:18:55
【问题描述】:

我正在尝试对神经网络中的多类问题进行网格搜索。 我无法获得最佳参数,内核继续编译。 我的代码有问题吗?请帮忙

import keras

from keras.models import Sequential
from keras.layers import Dense

# defining the baseline model:

def neural(output_dim=10,init_mode='glorot_uniform'):
    model = Sequential()
    model.add(Dense(output_dim=output_dim,
                    input_dim=2,
                    activation='relu',
                    kernel_initializer= init_mode))
    model.add(Dense(output_dim=output_dim,
                    activation='relu',
                    kernel_initializer= init_mode))
    model.add(Dense(output_dim=3,activation='softmax'))

    # Compile model
    model.compile(loss='sparse_categorical_crossentropy', 
                  optimizer='adam', 
                  metrics=['accuracy'])
    return model

from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from sklearn.model_selection import GridSearchCV
estimator = KerasClassifier(build_fn=neural, 
                            epochs=5, 
                            batch_size=5, 
                            verbose=0)

# define the grid search parameters
batch_size = [10, 20, 40, 60, 80, 100]
epochs = [10, 50, 100]
init_mode = ['uniform', 'lecun_uniform', 'normal', 'zero', 
             'glorot_normal', 'glorot_uniform', 'he_normal', 'he_uniform']
output_dim = [10, 15, 20, 25, 30,40]

param_grid = dict(batch_size=batch_size, 
                  epochs=epochs,
                  output_dim=output_dim,
                  init_mode=init_mode)

grid = GridSearchCV(estimator=estimator, 
                    scoring= 'accuracy',
                    param_grid=param_grid, 
                    n_jobs=-1,cv=5)

grid_result = grid.fit(X_train, Y_train)

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

【问题讨论】:

  • 首先总是尝试使用 n_jobs=1 看看它是否工作正常。

标签: neural-network grid-search


【解决方案1】:

您的代码没有错误。

您当前的参数网格可能有 864 种不同的参数组合。

'batch_size' 中的 6 个值 × 'epochs' 中的 3 个值 × 'init_mode' 中的 8 个 × 'output_dim' 中的 6 个值)= 864

GridSearchCV 将遍历所有这些可能性,并且您的估算器将被克隆多次。这又重复了 5 次,因为您设置了 cv=5

因此您的模型将被克隆(根据可能性编译和设置参数)总共 864 x 5 = 4320 次。

所以你一直在输出中看到模型被编译了很多次。

要检查 GridSearchCV 是否正常工作,请使用其 verbose 参数。

grid = GridSearchCV(estimator=estimator, 
                    scoring= 'accuracy',
                    param_grid=param_grid, 
                    n_jobs=1,cv=5, verbose=3)

这将打印当前可能尝试的参数、cv 迭代、适应它所需的时间、当前精度等。

【讨论】:

  • 嗨 Vivek,非常感谢您的回复。我正在使用目标变量 Y 而不对其进行一次热编码,因为我曾尝试对目标变量进行一次热编码并尝试适应网格搜索,但显示错误。我现在已经用 Target 变量拟合了网格搜索模型,而不是一个热编码。我做对了吗?
  • @arjunanil705 这取决于损失。由于您使用的是loss='sparse_categorical_crossentropy',因此无需进行一次热编码,在没有一次热编码的情况下传递它们是正确的方法。
猜你喜欢
  • 2019-03-28
  • 2015-04-18
  • 2019-07-17
  • 2021-10-23
  • 2021-05-31
  • 2018-10-22
  • 2013-11-20
  • 2018-10-15
  • 2019-01-30
相关资源
最近更新 更多