1. 程式人生 > >tensorflow中去除不足一個batch的資料

tensorflow中去除不足一個batch的資料

#-*- 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

#定義的dataset有引數,只能使用引數化迭代器
dataset = tf.data.Dataset.range(10)
# 定義引數化迭代器
dataset = dataset.shuffle(100)
dataset = dataset.apply(tf.contrib.data.batch_and_drop_remainder(3))  #每個batch3個數據,不足3個捨棄
iterator = dataset.make_initializable_iterator() next_element = iterator.get_next() with tf.Session() as sess: # 需要用引數初始化迭代器 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