Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
147 views
in Technique[技术] by (71.8m points)

keras - I was training an Ann machine learning model using GridSearchCV and got stuck with an IndexError in gridSearchCV

My model starts to train and while executing for sometime it gives an error :- IndexError: index 37 is out of bounds for axis 0 with size 37

It executes properly for my model without using gridsearchCV with fixed parameters

Here is my code

    from keras.wrappers.scikit_learn import KerasClassifier
    from sklearn.model_selection import GridSearchCV
    from keras.models import Sequential
    from keras.layers import Dense
    def build_classifier(optimizer, nb_layers,unit):
        classifier = Sequential()
        classifier.add(Dense(units = unit, kernel_initializer = 'uniform', activation = 'relu', input_dim = 14))
        i = 1
        while i <= nb_layers:
            classifier.add(Dense(activation="relu", units=unit, kernel_initializer="uniform"))
            i += 1
        classifier.add(Dense(units = 38, kernel_initializer = 'uniform', activation = 'softmax'))
        classifier.compile(optimizer = optimizer, loss = 'sparse_categorical_crossentropy', metrics = ['accuracy'])
        return classifier
    classifier = KerasClassifier(build_fn = build_classifier)
    parameters = {'batch_size': [10,25],
                  'epochs': [100,200],
                  'optimizer': ['adam'],
                  'nb_layers': [5,6,7],
                  'unit':[48,57,76]
                 }
    grid_search = GridSearchCV(estimator = classifier,
                               param_grid = parameters,
                               scoring = 'accuracy',
                              cv=5,n_jobs=-1)
    grid_search = grid_search.fit(X_train, y_train)
    best_parameters = grid_search.best_params_
    best_accuracy = grid_search.best_score_
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The error IndexError: index 37 is out of bounds for axis 0 with size 37 means that there is no element with index 37 in your object.

In python, if you have an object like array or list, which has elements indexed numerically, if it has n elements, indexes will go from 0 to n-1 (this is the general case, with the exception of reindexing in dataframes).

So, if you ahve 37 elements you can only retrieve elements from 0-36.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...