How to switch between training and validation dataset with tf.MonitoredTrainingSession?

后端 未结 1 1051
感情败类
感情败类 2021-02-09 01:36

I want to use feedable iterator design in tensorflow Dataset API, so I can switch to validation data after some training steps. But if I switched to validation data

相关标签:
1条回答
  • 2021-02-09 02:07

    I would suggest to catch tf.errors.OutOfRangeError raised at the end of the validation dataset (you can also check the processing multiple epochs section in the official API for another solution using the repeat dataset ):

    while not sess.should_stop():
        x = sess.run(next_element, feed_dict={handle: training_handle})
        count_training += 1
        print('{} [training] {}'.format(count_training, x.shape))
    
        # we do periodic validation
        if count_training % 4 == 0:
            sess.run(validation_iterator.initializer)
            count_validation = 0
            while True:
                try:
                    y = sess.run(next_element, feed_dict={handle: validation_handle})
                    count_validation += 1
                    print('  {} [validation] {}'.format(count_validation, y.shape))
                except tf.errors.OutOfRangeError:
                    break
    

    This piece of code prints:

    1 [training] (4,)  
    2 [training] (4,)  
    3 [training] (4,)  
    4 [training] (4,)  
      1 [validation] (4,)  
      2 [validation] (4,)  
    5 [training] (4,)
    6 [training] (4,)
    7 [training] (4,)
    8 [training] (4,)
      1 [validation] (4,)
      2 [validation] (4,)
    
    0 讨论(0)
提交回复
热议问题