how to change the regularization parameter in keras layer without rebuild a new model in R









up vote
0
down vote

favorite












I want to fine tuning my L2 parameter in my last keras layer using a for loop approach. My target is build a Extreme Machine Learning model. Now, I'm using the code below:



#possible values for L2... 
k = 2^(seq(-20,-1,1))

#vectors with metrics
acc_vector = vector('numeric',length(k))
loss_vector = vector('numeric',length(k))

for(i in seq_along(k))

model0 = keras_model_sequential() %>%
layer_dense(units = 500,activation = 'relu',input_shape = c(784),
trainable = F,name = 'dense1') %>%
layer_dense(units = 10, activation = 'softmax',
kernel_regularizer = regularizer_l2(k[i]),name='dense2') %>%
compile(loss = 'categorical_crossentropy',optimizer = optimizer_rmsprop(),
metrics = c('accuracy'))

model0 %>% fit(
x_train, y_train,
epochs = 5, batch_size = 512,
validation_split = 0.2,verbose=0)

eval = model0 %>% evaluate(x_test, y_test)

acc_vector[i] = eval$acc
loss_vector[i] = eval$loss

#I don't know why, but without the next 2 lines, my memory usage increase 2 times
rm(model0,eval)
gc()



So, here is my problem. With this aproach (run fast, at least), my weights start by random in each loop and the value of L2 doesn't make any sense. I tried other approachs like include weights = "weights" in the first layer and worked fine, except by the process time... it increased a lot! After this, I tried to pop the last layer and add a new layer with the new L2 as follow:



model0 = keras_model_sequential() %>% 
layer_dense(units = 500,activation = 'relu',input_shape = c(784),
trainable = F,name = 'dense1') %>%
layer_dense(units = 10, activation = 'softmax',
kernel_regularizer = regularizer_l2('any value'),name='dense2')

for(i in seq_along(k))
model0 %>% pop_layer() %>%
layer_dense(units = 10, activation = 'softmax',
kernel_regularizer = regularizer_l2(k[i]),name='dense2')



But doesn't work. The behavior of the last approach makes the models have just the first layer. I just want change the value of L2 to retrain the last layer of my model. How can I do that in a simple way?










share|improve this question

























    up vote
    0
    down vote

    favorite












    I want to fine tuning my L2 parameter in my last keras layer using a for loop approach. My target is build a Extreme Machine Learning model. Now, I'm using the code below:



    #possible values for L2... 
    k = 2^(seq(-20,-1,1))

    #vectors with metrics
    acc_vector = vector('numeric',length(k))
    loss_vector = vector('numeric',length(k))

    for(i in seq_along(k))

    model0 = keras_model_sequential() %>%
    layer_dense(units = 500,activation = 'relu',input_shape = c(784),
    trainable = F,name = 'dense1') %>%
    layer_dense(units = 10, activation = 'softmax',
    kernel_regularizer = regularizer_l2(k[i]),name='dense2') %>%
    compile(loss = 'categorical_crossentropy',optimizer = optimizer_rmsprop(),
    metrics = c('accuracy'))

    model0 %>% fit(
    x_train, y_train,
    epochs = 5, batch_size = 512,
    validation_split = 0.2,verbose=0)

    eval = model0 %>% evaluate(x_test, y_test)

    acc_vector[i] = eval$acc
    loss_vector[i] = eval$loss

    #I don't know why, but without the next 2 lines, my memory usage increase 2 times
    rm(model0,eval)
    gc()



    So, here is my problem. With this aproach (run fast, at least), my weights start by random in each loop and the value of L2 doesn't make any sense. I tried other approachs like include weights = "weights" in the first layer and worked fine, except by the process time... it increased a lot! After this, I tried to pop the last layer and add a new layer with the new L2 as follow:



    model0 = keras_model_sequential() %>% 
    layer_dense(units = 500,activation = 'relu',input_shape = c(784),
    trainable = F,name = 'dense1') %>%
    layer_dense(units = 10, activation = 'softmax',
    kernel_regularizer = regularizer_l2('any value'),name='dense2')

    for(i in seq_along(k))
    model0 %>% pop_layer() %>%
    layer_dense(units = 10, activation = 'softmax',
    kernel_regularizer = regularizer_l2(k[i]),name='dense2')



    But doesn't work. The behavior of the last approach makes the models have just the first layer. I just want change the value of L2 to retrain the last layer of my model. How can I do that in a simple way?










    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I want to fine tuning my L2 parameter in my last keras layer using a for loop approach. My target is build a Extreme Machine Learning model. Now, I'm using the code below:



      #possible values for L2... 
      k = 2^(seq(-20,-1,1))

      #vectors with metrics
      acc_vector = vector('numeric',length(k))
      loss_vector = vector('numeric',length(k))

      for(i in seq_along(k))

      model0 = keras_model_sequential() %>%
      layer_dense(units = 500,activation = 'relu',input_shape = c(784),
      trainable = F,name = 'dense1') %>%
      layer_dense(units = 10, activation = 'softmax',
      kernel_regularizer = regularizer_l2(k[i]),name='dense2') %>%
      compile(loss = 'categorical_crossentropy',optimizer = optimizer_rmsprop(),
      metrics = c('accuracy'))

      model0 %>% fit(
      x_train, y_train,
      epochs = 5, batch_size = 512,
      validation_split = 0.2,verbose=0)

      eval = model0 %>% evaluate(x_test, y_test)

      acc_vector[i] = eval$acc
      loss_vector[i] = eval$loss

      #I don't know why, but without the next 2 lines, my memory usage increase 2 times
      rm(model0,eval)
      gc()



      So, here is my problem. With this aproach (run fast, at least), my weights start by random in each loop and the value of L2 doesn't make any sense. I tried other approachs like include weights = "weights" in the first layer and worked fine, except by the process time... it increased a lot! After this, I tried to pop the last layer and add a new layer with the new L2 as follow:



      model0 = keras_model_sequential() %>% 
      layer_dense(units = 500,activation = 'relu',input_shape = c(784),
      trainable = F,name = 'dense1') %>%
      layer_dense(units = 10, activation = 'softmax',
      kernel_regularizer = regularizer_l2('any value'),name='dense2')

      for(i in seq_along(k))
      model0 %>% pop_layer() %>%
      layer_dense(units = 10, activation = 'softmax',
      kernel_regularizer = regularizer_l2(k[i]),name='dense2')



      But doesn't work. The behavior of the last approach makes the models have just the first layer. I just want change the value of L2 to retrain the last layer of my model. How can I do that in a simple way?










      share|improve this question













      I want to fine tuning my L2 parameter in my last keras layer using a for loop approach. My target is build a Extreme Machine Learning model. Now, I'm using the code below:



      #possible values for L2... 
      k = 2^(seq(-20,-1,1))

      #vectors with metrics
      acc_vector = vector('numeric',length(k))
      loss_vector = vector('numeric',length(k))

      for(i in seq_along(k))

      model0 = keras_model_sequential() %>%
      layer_dense(units = 500,activation = 'relu',input_shape = c(784),
      trainable = F,name = 'dense1') %>%
      layer_dense(units = 10, activation = 'softmax',
      kernel_regularizer = regularizer_l2(k[i]),name='dense2') %>%
      compile(loss = 'categorical_crossentropy',optimizer = optimizer_rmsprop(),
      metrics = c('accuracy'))

      model0 %>% fit(
      x_train, y_train,
      epochs = 5, batch_size = 512,
      validation_split = 0.2,verbose=0)

      eval = model0 %>% evaluate(x_test, y_test)

      acc_vector[i] = eval$acc
      loss_vector[i] = eval$loss

      #I don't know why, but without the next 2 lines, my memory usage increase 2 times
      rm(model0,eval)
      gc()



      So, here is my problem. With this aproach (run fast, at least), my weights start by random in each loop and the value of L2 doesn't make any sense. I tried other approachs like include weights = "weights" in the first layer and worked fine, except by the process time... it increased a lot! After this, I tried to pop the last layer and add a new layer with the new L2 as follow:



      model0 = keras_model_sequential() %>% 
      layer_dense(units = 500,activation = 'relu',input_shape = c(784),
      trainable = F,name = 'dense1') %>%
      layer_dense(units = 10, activation = 'softmax',
      kernel_regularizer = regularizer_l2('any value'),name='dense2')

      for(i in seq_along(k))
      model0 %>% pop_layer() %>%
      layer_dense(units = 10, activation = 'softmax',
      kernel_regularizer = regularizer_l2(k[i]),name='dense2')



      But doesn't work. The behavior of the last approach makes the models have just the first layer. I just want change the value of L2 to retrain the last layer of my model. How can I do that in a simple way?







      r keras






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 18:10









      brunoroquette

      464




      464



























          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',
          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%2f53251700%2fhow-to-change-the-regularization-parameter-in-keras-layer-without-rebuild-a-new%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53251700%2fhow-to-change-the-regularization-parameter-in-keras-layer-without-rebuild-a-new%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







          這個網誌中的熱門文章

          Barbados

          How to read a connectionString WITH PROVIDER in .NET Core?

          Node.js Script on GitHub Pages or Amazon S3