代码之家  ›  专栏  ›  技术社区  ›  arjunanil705

神经网络在多类分类问题中的网格搜索

  •  0
  • arjunanil705  · 技术社区  · 7 年前

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

    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))
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Vivek Kumar    7 年前

    您的代码中没有错误。

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

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

    GridSearchCV将迭代所有这些可能性,您的估计器将被克隆多次。因为你设置了 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迭代、适应时间、当前精度等。