clear keras backend between rounds of cross-validation during GridSearchCV










0















I am using GridSearchCV in an attempt to find the best possible hyperparameters for my CNN to maximise accuracy of my neural network.
However this process is taking an infeasible amount of time despite the fact that I am using Tensorflow GPU. I think my progress would be a lot faster if I could clear the session with keras.backend.clear_session() each time that a cross-validation round was done. However there does not seem to be a clear way to do this...



def create_model(optimizer='adam',activation_last = 'sigmoid',dropout=0.2,kernel_sizes=[200,5,2],filters1=100,filters2=100,filters3=100):
# Initialize the constructor
model = Sequential()
model.add(Conv1D(filters1, kernel_sizes[0],strides=1,padding='same', activation='relu', input_shape=X.shape[1:]))
model.add(MaxPooling1D(kernel_sizes[0]))
model.add(Conv1D(filters2, kernel_sizes[1],strides=1,padding='same', activation='relu'))
model.add(MaxPooling1D(kernel_sizes[1]))
model.add(Conv1D(filters3, kernel_sizes[1],strides=1,padding='same', activation='relu'))
model.add(MaxPooling1D(kernel_sizes[2]))
model.add(Conv1D(filters3, kernel_sizes[2],strides=1,padding='same', activation='relu'))
model.add(Flatten())
model.add(Dropout(dropout))
model.add(Dense(1, activation=activation_last))
model.compile(loss='binary_crossentropy',optimizer='rmsprop',metrics=['accuracy'])
return model

model=create_model()
parameters = 'batch_size': [50, 100, 150],
'epochs': [3,5,10],
'optimizer': ['adam', 'rmsprop'],
'dropout' : [0.2,0.25,0.3],
'activation_last' : ['relu','sigmoid'],
'kernel_sizes' : [[10,5,5],[3, 300, 3],[300, 3, 3],[1000,3,1],[500,3,2], [200,5,2],[10,10,10]],
'filters1' : [60,75,100],
'filters2' : [50,60],
'filters3' : [50]

my_classifier = KerasClassifier(build_fn=create_model, verbose=0) # Create hyperparameter space
grid=GridSearchCV(estimator=my_classifier, param_grid = parameters,scoring = 'accuracy',cv = 10) #inserir param_distributions
# Fit grid search
class_weight = 0: sum(y==0),1: sum(y==1)
grid_result = grid.fit(X, y, class_weight=class_weight)
# View hyperparameters of best neural network
print(grid_result.best_params_)


Can anyone tell me how I might go about clearing the session each time a round of cross-validation is finished? As per the following tutorial there is no way to do this: https://machinelearningmastery.com/use-keras-deep-learning-models-scikit-learn-python/










share|improve this question




























    0















    I am using GridSearchCV in an attempt to find the best possible hyperparameters for my CNN to maximise accuracy of my neural network.
    However this process is taking an infeasible amount of time despite the fact that I am using Tensorflow GPU. I think my progress would be a lot faster if I could clear the session with keras.backend.clear_session() each time that a cross-validation round was done. However there does not seem to be a clear way to do this...



    def create_model(optimizer='adam',activation_last = 'sigmoid',dropout=0.2,kernel_sizes=[200,5,2],filters1=100,filters2=100,filters3=100):
    # Initialize the constructor
    model = Sequential()
    model.add(Conv1D(filters1, kernel_sizes[0],strides=1,padding='same', activation='relu', input_shape=X.shape[1:]))
    model.add(MaxPooling1D(kernel_sizes[0]))
    model.add(Conv1D(filters2, kernel_sizes[1],strides=1,padding='same', activation='relu'))
    model.add(MaxPooling1D(kernel_sizes[1]))
    model.add(Conv1D(filters3, kernel_sizes[1],strides=1,padding='same', activation='relu'))
    model.add(MaxPooling1D(kernel_sizes[2]))
    model.add(Conv1D(filters3, kernel_sizes[2],strides=1,padding='same', activation='relu'))
    model.add(Flatten())
    model.add(Dropout(dropout))
    model.add(Dense(1, activation=activation_last))
    model.compile(loss='binary_crossentropy',optimizer='rmsprop',metrics=['accuracy'])
    return model

    model=create_model()
    parameters = 'batch_size': [50, 100, 150],
    'epochs': [3,5,10],
    'optimizer': ['adam', 'rmsprop'],
    'dropout' : [0.2,0.25,0.3],
    'activation_last' : ['relu','sigmoid'],
    'kernel_sizes' : [[10,5,5],[3, 300, 3],[300, 3, 3],[1000,3,1],[500,3,2], [200,5,2],[10,10,10]],
    'filters1' : [60,75,100],
    'filters2' : [50,60],
    'filters3' : [50]

    my_classifier = KerasClassifier(build_fn=create_model, verbose=0) # Create hyperparameter space
    grid=GridSearchCV(estimator=my_classifier, param_grid = parameters,scoring = 'accuracy',cv = 10) #inserir param_distributions
    # Fit grid search
    class_weight = 0: sum(y==0),1: sum(y==1)
    grid_result = grid.fit(X, y, class_weight=class_weight)
    # View hyperparameters of best neural network
    print(grid_result.best_params_)


    Can anyone tell me how I might go about clearing the session each time a round of cross-validation is finished? As per the following tutorial there is no way to do this: https://machinelearningmastery.com/use-keras-deep-learning-models-scikit-learn-python/










    share|improve this question


























      0












      0








      0








      I am using GridSearchCV in an attempt to find the best possible hyperparameters for my CNN to maximise accuracy of my neural network.
      However this process is taking an infeasible amount of time despite the fact that I am using Tensorflow GPU. I think my progress would be a lot faster if I could clear the session with keras.backend.clear_session() each time that a cross-validation round was done. However there does not seem to be a clear way to do this...



      def create_model(optimizer='adam',activation_last = 'sigmoid',dropout=0.2,kernel_sizes=[200,5,2],filters1=100,filters2=100,filters3=100):
      # Initialize the constructor
      model = Sequential()
      model.add(Conv1D(filters1, kernel_sizes[0],strides=1,padding='same', activation='relu', input_shape=X.shape[1:]))
      model.add(MaxPooling1D(kernel_sizes[0]))
      model.add(Conv1D(filters2, kernel_sizes[1],strides=1,padding='same', activation='relu'))
      model.add(MaxPooling1D(kernel_sizes[1]))
      model.add(Conv1D(filters3, kernel_sizes[1],strides=1,padding='same', activation='relu'))
      model.add(MaxPooling1D(kernel_sizes[2]))
      model.add(Conv1D(filters3, kernel_sizes[2],strides=1,padding='same', activation='relu'))
      model.add(Flatten())
      model.add(Dropout(dropout))
      model.add(Dense(1, activation=activation_last))
      model.compile(loss='binary_crossentropy',optimizer='rmsprop',metrics=['accuracy'])
      return model

      model=create_model()
      parameters = 'batch_size': [50, 100, 150],
      'epochs': [3,5,10],
      'optimizer': ['adam', 'rmsprop'],
      'dropout' : [0.2,0.25,0.3],
      'activation_last' : ['relu','sigmoid'],
      'kernel_sizes' : [[10,5,5],[3, 300, 3],[300, 3, 3],[1000,3,1],[500,3,2], [200,5,2],[10,10,10]],
      'filters1' : [60,75,100],
      'filters2' : [50,60],
      'filters3' : [50]

      my_classifier = KerasClassifier(build_fn=create_model, verbose=0) # Create hyperparameter space
      grid=GridSearchCV(estimator=my_classifier, param_grid = parameters,scoring = 'accuracy',cv = 10) #inserir param_distributions
      # Fit grid search
      class_weight = 0: sum(y==0),1: sum(y==1)
      grid_result = grid.fit(X, y, class_weight=class_weight)
      # View hyperparameters of best neural network
      print(grid_result.best_params_)


      Can anyone tell me how I might go about clearing the session each time a round of cross-validation is finished? As per the following tutorial there is no way to do this: https://machinelearningmastery.com/use-keras-deep-learning-models-scikit-learn-python/










      share|improve this question
















      I am using GridSearchCV in an attempt to find the best possible hyperparameters for my CNN to maximise accuracy of my neural network.
      However this process is taking an infeasible amount of time despite the fact that I am using Tensorflow GPU. I think my progress would be a lot faster if I could clear the session with keras.backend.clear_session() each time that a cross-validation round was done. However there does not seem to be a clear way to do this...



      def create_model(optimizer='adam',activation_last = 'sigmoid',dropout=0.2,kernel_sizes=[200,5,2],filters1=100,filters2=100,filters3=100):
      # Initialize the constructor
      model = Sequential()
      model.add(Conv1D(filters1, kernel_sizes[0],strides=1,padding='same', activation='relu', input_shape=X.shape[1:]))
      model.add(MaxPooling1D(kernel_sizes[0]))
      model.add(Conv1D(filters2, kernel_sizes[1],strides=1,padding='same', activation='relu'))
      model.add(MaxPooling1D(kernel_sizes[1]))
      model.add(Conv1D(filters3, kernel_sizes[1],strides=1,padding='same', activation='relu'))
      model.add(MaxPooling1D(kernel_sizes[2]))
      model.add(Conv1D(filters3, kernel_sizes[2],strides=1,padding='same', activation='relu'))
      model.add(Flatten())
      model.add(Dropout(dropout))
      model.add(Dense(1, activation=activation_last))
      model.compile(loss='binary_crossentropy',optimizer='rmsprop',metrics=['accuracy'])
      return model

      model=create_model()
      parameters = 'batch_size': [50, 100, 150],
      'epochs': [3,5,10],
      'optimizer': ['adam', 'rmsprop'],
      'dropout' : [0.2,0.25,0.3],
      'activation_last' : ['relu','sigmoid'],
      'kernel_sizes' : [[10,5,5],[3, 300, 3],[300, 3, 3],[1000,3,1],[500,3,2], [200,5,2],[10,10,10]],
      'filters1' : [60,75,100],
      'filters2' : [50,60],
      'filters3' : [50]

      my_classifier = KerasClassifier(build_fn=create_model, verbose=0) # Create hyperparameter space
      grid=GridSearchCV(estimator=my_classifier, param_grid = parameters,scoring = 'accuracy',cv = 10) #inserir param_distributions
      # Fit grid search
      class_weight = 0: sum(y==0),1: sum(y==1)
      grid_result = grid.fit(X, y, class_weight=class_weight)
      # View hyperparameters of best neural network
      print(grid_result.best_params_)


      Can anyone tell me how I might go about clearing the session each time a round of cross-validation is finished? As per the following tutorial there is no way to do this: https://machinelearningmastery.com/use-keras-deep-learning-models-scikit-learn-python/







      python memory-management scikit-learn keras grid-search






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 '18 at 9:06









      Vivek Kumar

      16.3k42055




      16.3k42055










      asked Nov 13 '18 at 16:37









      xenopusxenopus

      567




      567






















          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53285618%2fclear-keras-backend-between-rounds-of-cross-validation-during-gridsearchcv%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53285618%2fclear-keras-backend-between-rounds-of-cross-validation-during-gridsearchcv%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

          Syphilis

          Darth Vader #20