Removing less than one batch of data in tensorflow

Time:2020-5-16

I don’t need to talk much nonsense, just go to the code!

#-*- coding:utf-8 -*-
import tensorflow as tf
import numpy as np
 
value1 = tf.placeholder(dtype=tf.float32)
value2 = tf.placeholder(dtype=tf.float32)
value3 = value1 + value2
 
#Defined dataset has parameters, only parameterized iterators can be used
dataset = tf.data.Dataset.range(10)
#Defining parametric iterators
dataset = dataset.shuffle(100)
Dataset = dataset. Apply (TF. Contrib. Data. Batch and drop retain (3)) (3 data per batch, less than 3 data discarded)
iterator = dataset.make_initializable_iterator()
next_element = iterator.get_next()
 
with tf.Session() as sess:
  #Iterators need to be initialized with parameters
  for i in range(2):
    sess.run(iterator.initializer)
    while True:
      try:
        value = sess.run(next_element)
        result = sess.run(value3,feed_dict={value1:value,value2:value})
        print(result)
      except tf.errors.OutOfRangeError:
        print("End of epoch %d" % i)
        break

The above implementation of removing less than one batch of data in tensorflow is all the content shared by Xiaobian. I hope it can give you a reference, and I hope you can support developer more.