How to monitor validation loss in the training of estimators in TensorFlow?











up vote
1
down vote

favorite












I want to ask a question about how to monitor validation loss in the training process of estimators in TensorFlow. I have checked a similar question (validation during training of Estimator) asked before, but it did not help much.



If I use estimators to build a model, I will give an input function to the Estimator.train() function. But there is no way to add another validation_x, and validation_y data in the training process. Therefore, when the training started, I can only see the training loss. The training loss is expected to decrease when the training process running longer. However, this information is not helpful to prevent overfitting. The more valuable information is validation loss. Usually, the validation loss is the U-shape with the number of epochs. To prevent overfitting, we want to find the number of epochs that the validation loss is minimum.



So this is my problem. How can I get validation loss for each epoch in the training process of using estimators?










share|improve this question


















  • 1




    So, it looks like you need to manually control this. Define separate input functions for training and validation dataset. Train for X steps/epochs using train() and the training input, call get validation loss using evaluate() with the validation input. Decide whether or not you want to train more. Run train() again or quit.
    – Mad Wombat
    Nov 9 at 21:13












  • Hi Mad Wombat, yes. My goal is to get the right number of epochs to prevent overfitting.
    – Han M
    Nov 9 at 22:16










  • If you want to control the training on every step, you might want to skip the estimators and implement your own training cycle. If you are OK with a bit less granularity, you can implement a simple loop where you call train() for some preset number of steps or epochs (and you can adjust it as you go) and then call evaluate() to judge your progress. This is basic python we are talking about, nothing too complicated.
    – Mad Wombat
    Nov 10 at 5:00















up vote
1
down vote

favorite












I want to ask a question about how to monitor validation loss in the training process of estimators in TensorFlow. I have checked a similar question (validation during training of Estimator) asked before, but it did not help much.



If I use estimators to build a model, I will give an input function to the Estimator.train() function. But there is no way to add another validation_x, and validation_y data in the training process. Therefore, when the training started, I can only see the training loss. The training loss is expected to decrease when the training process running longer. However, this information is not helpful to prevent overfitting. The more valuable information is validation loss. Usually, the validation loss is the U-shape with the number of epochs. To prevent overfitting, we want to find the number of epochs that the validation loss is minimum.



So this is my problem. How can I get validation loss for each epoch in the training process of using estimators?










share|improve this question


















  • 1




    So, it looks like you need to manually control this. Define separate input functions for training and validation dataset. Train for X steps/epochs using train() and the training input, call get validation loss using evaluate() with the validation input. Decide whether or not you want to train more. Run train() again or quit.
    – Mad Wombat
    Nov 9 at 21:13












  • Hi Mad Wombat, yes. My goal is to get the right number of epochs to prevent overfitting.
    – Han M
    Nov 9 at 22:16










  • If you want to control the training on every step, you might want to skip the estimators and implement your own training cycle. If you are OK with a bit less granularity, you can implement a simple loop where you call train() for some preset number of steps or epochs (and you can adjust it as you go) and then call evaluate() to judge your progress. This is basic python we are talking about, nothing too complicated.
    – Mad Wombat
    Nov 10 at 5:00













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I want to ask a question about how to monitor validation loss in the training process of estimators in TensorFlow. I have checked a similar question (validation during training of Estimator) asked before, but it did not help much.



If I use estimators to build a model, I will give an input function to the Estimator.train() function. But there is no way to add another validation_x, and validation_y data in the training process. Therefore, when the training started, I can only see the training loss. The training loss is expected to decrease when the training process running longer. However, this information is not helpful to prevent overfitting. The more valuable information is validation loss. Usually, the validation loss is the U-shape with the number of epochs. To prevent overfitting, we want to find the number of epochs that the validation loss is minimum.



So this is my problem. How can I get validation loss for each epoch in the training process of using estimators?










share|improve this question













I want to ask a question about how to monitor validation loss in the training process of estimators in TensorFlow. I have checked a similar question (validation during training of Estimator) asked before, but it did not help much.



If I use estimators to build a model, I will give an input function to the Estimator.train() function. But there is no way to add another validation_x, and validation_y data in the training process. Therefore, when the training started, I can only see the training loss. The training loss is expected to decrease when the training process running longer. However, this information is not helpful to prevent overfitting. The more valuable information is validation loss. Usually, the validation loss is the U-shape with the number of epochs. To prevent overfitting, we want to find the number of epochs that the validation loss is minimum.



So this is my problem. How can I get validation loss for each epoch in the training process of using estimators?







python tensorflow machine-learning deep-learning






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 20:39









Han M

667




667








  • 1




    So, it looks like you need to manually control this. Define separate input functions for training and validation dataset. Train for X steps/epochs using train() and the training input, call get validation loss using evaluate() with the validation input. Decide whether or not you want to train more. Run train() again or quit.
    – Mad Wombat
    Nov 9 at 21:13












  • Hi Mad Wombat, yes. My goal is to get the right number of epochs to prevent overfitting.
    – Han M
    Nov 9 at 22:16










  • If you want to control the training on every step, you might want to skip the estimators and implement your own training cycle. If you are OK with a bit less granularity, you can implement a simple loop where you call train() for some preset number of steps or epochs (and you can adjust it as you go) and then call evaluate() to judge your progress. This is basic python we are talking about, nothing too complicated.
    – Mad Wombat
    Nov 10 at 5:00














  • 1




    So, it looks like you need to manually control this. Define separate input functions for training and validation dataset. Train for X steps/epochs using train() and the training input, call get validation loss using evaluate() with the validation input. Decide whether or not you want to train more. Run train() again or quit.
    – Mad Wombat
    Nov 9 at 21:13












  • Hi Mad Wombat, yes. My goal is to get the right number of epochs to prevent overfitting.
    – Han M
    Nov 9 at 22:16










  • If you want to control the training on every step, you might want to skip the estimators and implement your own training cycle. If you are OK with a bit less granularity, you can implement a simple loop where you call train() for some preset number of steps or epochs (and you can adjust it as you go) and then call evaluate() to judge your progress. This is basic python we are talking about, nothing too complicated.
    – Mad Wombat
    Nov 10 at 5:00








1




1




So, it looks like you need to manually control this. Define separate input functions for training and validation dataset. Train for X steps/epochs using train() and the training input, call get validation loss using evaluate() with the validation input. Decide whether or not you want to train more. Run train() again or quit.
– Mad Wombat
Nov 9 at 21:13






So, it looks like you need to manually control this. Define separate input functions for training and validation dataset. Train for X steps/epochs using train() and the training input, call get validation loss using evaluate() with the validation input. Decide whether or not you want to train more. Run train() again or quit.
– Mad Wombat
Nov 9 at 21:13














Hi Mad Wombat, yes. My goal is to get the right number of epochs to prevent overfitting.
– Han M
Nov 9 at 22:16




Hi Mad Wombat, yes. My goal is to get the right number of epochs to prevent overfitting.
– Han M
Nov 9 at 22:16












If you want to control the training on every step, you might want to skip the estimators and implement your own training cycle. If you are OK with a bit less granularity, you can implement a simple loop where you call train() for some preset number of steps or epochs (and you can adjust it as you go) and then call evaluate() to judge your progress. This is basic python we are talking about, nothing too complicated.
– Mad Wombat
Nov 10 at 5:00




If you want to control the training on every step, you might want to skip the estimators and implement your own training cycle. If you are OK with a bit less granularity, you can implement a simple loop where you call train() for some preset number of steps or epochs (and you can adjust it as you go) and then call evaluate() to judge your progress. This is basic python we are talking about, nothing too complicated.
– Mad Wombat
Nov 10 at 5:00












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










You need to create a validation input_fn and either use estimator.train() and estimator.evaluate() alternatively or simpy use tf.estimator.train_and_evaluate()



x = ...
y = ...

...

# For example, if x and y are numpy arrays < 2 GB
train_dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train))
val_dataset = tf.data.Dataset.from_tensor_slices((x_val_, y_val))

...

estimator = ...

for epoch in n_epochs:
estimator.train(input_fn = train_dataset)
estimator.evaluate(input_fn = val_dataset)


estimator.evaluate() will compute the loss and any other metrics that are defined in your model_fn and will save the events in a new "eval" directory inside your job_dir.






share|improve this answer























    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%2f53232969%2fhow-to-monitor-validation-loss-in-the-training-of-estimators-in-tensorflow%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    You need to create a validation input_fn and either use estimator.train() and estimator.evaluate() alternatively or simpy use tf.estimator.train_and_evaluate()



    x = ...
    y = ...

    ...

    # For example, if x and y are numpy arrays < 2 GB
    train_dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train))
    val_dataset = tf.data.Dataset.from_tensor_slices((x_val_, y_val))

    ...

    estimator = ...

    for epoch in n_epochs:
    estimator.train(input_fn = train_dataset)
    estimator.evaluate(input_fn = val_dataset)


    estimator.evaluate() will compute the loss and any other metrics that are defined in your model_fn and will save the events in a new "eval" directory inside your job_dir.






    share|improve this answer



























      up vote
      1
      down vote



      accepted










      You need to create a validation input_fn and either use estimator.train() and estimator.evaluate() alternatively or simpy use tf.estimator.train_and_evaluate()



      x = ...
      y = ...

      ...

      # For example, if x and y are numpy arrays < 2 GB
      train_dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train))
      val_dataset = tf.data.Dataset.from_tensor_slices((x_val_, y_val))

      ...

      estimator = ...

      for epoch in n_epochs:
      estimator.train(input_fn = train_dataset)
      estimator.evaluate(input_fn = val_dataset)


      estimator.evaluate() will compute the loss and any other metrics that are defined in your model_fn and will save the events in a new "eval" directory inside your job_dir.






      share|improve this answer

























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        You need to create a validation input_fn and either use estimator.train() and estimator.evaluate() alternatively or simpy use tf.estimator.train_and_evaluate()



        x = ...
        y = ...

        ...

        # For example, if x and y are numpy arrays < 2 GB
        train_dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train))
        val_dataset = tf.data.Dataset.from_tensor_slices((x_val_, y_val))

        ...

        estimator = ...

        for epoch in n_epochs:
        estimator.train(input_fn = train_dataset)
        estimator.evaluate(input_fn = val_dataset)


        estimator.evaluate() will compute the loss and any other metrics that are defined in your model_fn and will save the events in a new "eval" directory inside your job_dir.






        share|improve this answer














        You need to create a validation input_fn and either use estimator.train() and estimator.evaluate() alternatively or simpy use tf.estimator.train_and_evaluate()



        x = ...
        y = ...

        ...

        # For example, if x and y are numpy arrays < 2 GB
        train_dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train))
        val_dataset = tf.data.Dataset.from_tensor_slices((x_val_, y_val))

        ...

        estimator = ...

        for epoch in n_epochs:
        estimator.train(input_fn = train_dataset)
        estimator.evaluate(input_fn = val_dataset)


        estimator.evaluate() will compute the loss and any other metrics that are defined in your model_fn and will save the events in a new "eval" directory inside your job_dir.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 10 at 17:11

























        answered Nov 10 at 17:03









        Olivier Dehaene

        65519




        65519






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232969%2fhow-to-monitor-validation-loss-in-the-training-of-estimators-in-tensorflow%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

            Florida Star v. B. J. F.

            Danny Elfman

            Lugert, Oklahoma