Best Practice for Tensorflow Keras validation












0












$begingroup$


I'm a newbie for ML and it might be that I've made a mistake, but it seems that I'm getting quite different results from different ways. My code is like this:



from sklearn.model_selection import KFold,
import tensorflow as tf
from tensorflow.keras import layers

X, y = some_func_to_get_data()
model = tf.keras.Sequential()
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dropout(0.1))
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dropout(0.1))
model.add(layers.Dense(1, activation='relu'))
model.compile(optimizer=tf.train.AdamOptimizer(0.001), loss='mse', metrics=['mse'])


then, the first way to get validation:



model.fit(X, y, validation_split=0.1, epochs=30, batch_size=32)


and the output for last epoch is somewhat like:



Epoch 30/30
4029/4029 [==============================] - 0s 16us/step - loss: 0.1289 - mean_squared_error: 0.1289 - val_loss: 0.2312 - val_mean_squared_error: 0.2312


the second way is to do K-Fold cross validation myself:



crossvalidation = KFold(n_splits=10, shuffle=False, random_state=1)
for train, test in crossvalidation.split(X, y):
model.fit(X[train], y[train], epochs=30, batch_size=32, verbose=0)
scores = model.evaluate(X[test], y[test], verbose=0)
print(scores)


and the output:



[0.11302296231899943, 0.11302296231899943]
[0.1082031112164259, 0.1082031112164259]
[0.15109882104609693, 0.15109882104609693]
[0.11689347002123084, 0.11689347002123084]
[0.0495243084483913, 0.0495243084483913]
[0.09257462301424571, 0.09257462301424571]
[0.1153098890291793, 0.1153098890291793]
[0.11075845470764493, 0.11075845470764493]
[0.09127715530011478, 0.09127715530011478]
[0.05938179766388414, 0.05938179766388414]


The problem is, why the value given in the second way much smaller than val_loss in the first way? Are they using different metrics or anything?









share







New contributor




jjs is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$

















    0












    $begingroup$


    I'm a newbie for ML and it might be that I've made a mistake, but it seems that I'm getting quite different results from different ways. My code is like this:



    from sklearn.model_selection import KFold,
    import tensorflow as tf
    from tensorflow.keras import layers

    X, y = some_func_to_get_data()
    model = tf.keras.Sequential()
    model.add(layers.Dense(64, activation='relu'))
    model.add(layers.Dropout(0.1))
    model.add(layers.Dense(64, activation='relu'))
    model.add(layers.Dropout(0.1))
    model.add(layers.Dense(1, activation='relu'))
    model.compile(optimizer=tf.train.AdamOptimizer(0.001), loss='mse', metrics=['mse'])


    then, the first way to get validation:



    model.fit(X, y, validation_split=0.1, epochs=30, batch_size=32)


    and the output for last epoch is somewhat like:



    Epoch 30/30
    4029/4029 [==============================] - 0s 16us/step - loss: 0.1289 - mean_squared_error: 0.1289 - val_loss: 0.2312 - val_mean_squared_error: 0.2312


    the second way is to do K-Fold cross validation myself:



    crossvalidation = KFold(n_splits=10, shuffle=False, random_state=1)
    for train, test in crossvalidation.split(X, y):
    model.fit(X[train], y[train], epochs=30, batch_size=32, verbose=0)
    scores = model.evaluate(X[test], y[test], verbose=0)
    print(scores)


    and the output:



    [0.11302296231899943, 0.11302296231899943]
    [0.1082031112164259, 0.1082031112164259]
    [0.15109882104609693, 0.15109882104609693]
    [0.11689347002123084, 0.11689347002123084]
    [0.0495243084483913, 0.0495243084483913]
    [0.09257462301424571, 0.09257462301424571]
    [0.1153098890291793, 0.1153098890291793]
    [0.11075845470764493, 0.11075845470764493]
    [0.09127715530011478, 0.09127715530011478]
    [0.05938179766388414, 0.05938179766388414]


    The problem is, why the value given in the second way much smaller than val_loss in the first way? Are they using different metrics or anything?









    share







    New contributor




    jjs is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.







    $endgroup$















      0












      0








      0





      $begingroup$


      I'm a newbie for ML and it might be that I've made a mistake, but it seems that I'm getting quite different results from different ways. My code is like this:



      from sklearn.model_selection import KFold,
      import tensorflow as tf
      from tensorflow.keras import layers

      X, y = some_func_to_get_data()
      model = tf.keras.Sequential()
      model.add(layers.Dense(64, activation='relu'))
      model.add(layers.Dropout(0.1))
      model.add(layers.Dense(64, activation='relu'))
      model.add(layers.Dropout(0.1))
      model.add(layers.Dense(1, activation='relu'))
      model.compile(optimizer=tf.train.AdamOptimizer(0.001), loss='mse', metrics=['mse'])


      then, the first way to get validation:



      model.fit(X, y, validation_split=0.1, epochs=30, batch_size=32)


      and the output for last epoch is somewhat like:



      Epoch 30/30
      4029/4029 [==============================] - 0s 16us/step - loss: 0.1289 - mean_squared_error: 0.1289 - val_loss: 0.2312 - val_mean_squared_error: 0.2312


      the second way is to do K-Fold cross validation myself:



      crossvalidation = KFold(n_splits=10, shuffle=False, random_state=1)
      for train, test in crossvalidation.split(X, y):
      model.fit(X[train], y[train], epochs=30, batch_size=32, verbose=0)
      scores = model.evaluate(X[test], y[test], verbose=0)
      print(scores)


      and the output:



      [0.11302296231899943, 0.11302296231899943]
      [0.1082031112164259, 0.1082031112164259]
      [0.15109882104609693, 0.15109882104609693]
      [0.11689347002123084, 0.11689347002123084]
      [0.0495243084483913, 0.0495243084483913]
      [0.09257462301424571, 0.09257462301424571]
      [0.1153098890291793, 0.1153098890291793]
      [0.11075845470764493, 0.11075845470764493]
      [0.09127715530011478, 0.09127715530011478]
      [0.05938179766388414, 0.05938179766388414]


      The problem is, why the value given in the second way much smaller than val_loss in the first way? Are they using different metrics or anything?









      share







      New contributor




      jjs is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.







      $endgroup$




      I'm a newbie for ML and it might be that I've made a mistake, but it seems that I'm getting quite different results from different ways. My code is like this:



      from sklearn.model_selection import KFold,
      import tensorflow as tf
      from tensorflow.keras import layers

      X, y = some_func_to_get_data()
      model = tf.keras.Sequential()
      model.add(layers.Dense(64, activation='relu'))
      model.add(layers.Dropout(0.1))
      model.add(layers.Dense(64, activation='relu'))
      model.add(layers.Dropout(0.1))
      model.add(layers.Dense(1, activation='relu'))
      model.compile(optimizer=tf.train.AdamOptimizer(0.001), loss='mse', metrics=['mse'])


      then, the first way to get validation:



      model.fit(X, y, validation_split=0.1, epochs=30, batch_size=32)


      and the output for last epoch is somewhat like:



      Epoch 30/30
      4029/4029 [==============================] - 0s 16us/step - loss: 0.1289 - mean_squared_error: 0.1289 - val_loss: 0.2312 - val_mean_squared_error: 0.2312


      the second way is to do K-Fold cross validation myself:



      crossvalidation = KFold(n_splits=10, shuffle=False, random_state=1)
      for train, test in crossvalidation.split(X, y):
      model.fit(X[train], y[train], epochs=30, batch_size=32, verbose=0)
      scores = model.evaluate(X[test], y[test], verbose=0)
      print(scores)


      and the output:



      [0.11302296231899943, 0.11302296231899943]
      [0.1082031112164259, 0.1082031112164259]
      [0.15109882104609693, 0.15109882104609693]
      [0.11689347002123084, 0.11689347002123084]
      [0.0495243084483913, 0.0495243084483913]
      [0.09257462301424571, 0.09257462301424571]
      [0.1153098890291793, 0.1153098890291793]
      [0.11075845470764493, 0.11075845470764493]
      [0.09127715530011478, 0.09127715530011478]
      [0.05938179766388414, 0.05938179766388414]


      The problem is, why the value given in the second way much smaller than val_loss in the first way? Are they using different metrics or anything?







      keras tensorflow cross-validation





      share







      New contributor




      jjs is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.










      share







      New contributor




      jjs is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      share



      share






      New contributor




      jjs is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 2 mins ago









      jjsjjs

      1




      1




      New contributor




      jjs is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      jjs is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      jjs is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          0






          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "557"
          };
          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: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          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
          });


          }
          });






          jjs is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f44541%2fbest-practice-for-tensorflow-keras-validation%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








          jjs is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          jjs is a new contributor. Be nice, and check out our Code of Conduct.













          jjs is a new contributor. Be nice, and check out our Code of Conduct.












          jjs is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Data Science Stack Exchange!


          • 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.


          Use MathJax to format equations. MathJax reference.


          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%2fdatascience.stackexchange.com%2fquestions%2f44541%2fbest-practice-for-tensorflow-keras-validation%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

          Ponta tanko

          Tantalo (mitologio)

          Erzsébet Schaár