TensorFlow train.string_input_producer多次读取CSV文件
我使用标准输入管道读取TensorFlow中的CSV文件。我有一个输入文件,我想使用小批量读取。我担心的是,即使我认为已正确设置了历代的数量,文件仍被多次读取。我认为这可能是由于train.string\u input\u producer()函数的行为造成的TensorFlow train.string_input_producer多次读取CSV文件,csv,tensorflow,Csv,Tensorflow,我使用标准输入管道读取TensorFlow中的CSV文件。我有一个输入文件,我想使用小批量读取。我担心的是,即使我认为已正确设置了历代的数量,文件仍被多次读取。我认为这可能是由于train.string\u input\u producer()函数的行为造成的 with self.graph.as_default(): epochs = np.floor(fileSize / batchSize) + 1 self.fileNameQ = tf.train.str
with self.graph.as_default():
epochs = np.floor(fileSize / batchSize) + 1
self.fileNameQ = tf.train.string_input_producer(fileNameList, num_epochs = epochs)
self.batchInput, self.label = self.inputPipeline(batchSize, dim)
在这里,文件大小由以下因素决定:
fileSize = sum(1 for line in open(file.name))
我将输入管道函数定义如下:
def readFromCsv(self, dim):
reader = tf.TextLineReader()
_, csvLine = reader.read(self.fileNameQ)
recordDefaults = [["\0"] for cl in range(dim + 3)]
recordStr = tf.decode_csv(csvLine, record_defaults=recordDefaults)
self.label = tf.stack(recordStr[0:3])
self.features = tf.stack(recordStr[3:dim + 3])
return (self.features, self.label)
def inputPipeline(self, batchSize, dim):
minAfterDequeue = 10000
capacity = minAfterDequeue + 3 * batchSize
example, label = self.readFromCsv(dim)
exampleBatchStr, labelBatch = tf.train.batch([example, label], batch_size=batchSize, capacity=capacity)
exampleBatch = tf.string_to_number(exampleBatchStr)
return (tf.transpose(exampleBatch), tf.transpose(labelBatch))
然后我进行训练,每次处理1000条记录时打印出一个点
def train(self, batchSize, dim):
with self.sess:
self.sess.run(tf.local_variables_initializer())
# Start populating the filename queue.
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
#Training iterations
self.iterationInput = 0
while self.iterationInput < self.iterations:
#Train with each vector one by one
self.iterationInput += 1
print("iteration " + str(self.iterationInput) + " for window size " + str(dim))
try:
loopCount = 0
while not coord.should_stop():
#Fill in input data.
self.sess.run([self.batchInput, self.label])
self.sess.run(self.trainingOp)
#For every 1,000 samples, print a dot.
if loopCount % 1000 == 0:
sys.stdout.flush()
sys.stdout.write('.')
loopCount += 1
except tf.errors.OutOfRangeError:
print("Done training -- epoch limit reached")
coord.request_stop()
# When done, join the threads
coord.join(threads)
def序列(自身、批次大小、尺寸):
使用self.sess:
self.sess.run(tf.local\u variables\u initializer())
#开始填充文件名队列。
coord=tf.train.Coordinator()
线程=tf.train.start\u queue\u runner(coord=coord)
#训练迭代
self.iterationput=0
当self.iterationput
运行程序时打印出的点数明显大于文件中的记录数(/1000)。我知道train.string\u input\u producer()初始化了四个线程。我担心的是,每个线程只读取一次文件,当硬件中没有足够的并行化时,会导致运行时增加。因为运行时已经很长了,我不想增加太多。有什么方法可以防止文件被读取四次吗?问题在于如何设置纪元数。您只需将其设置为无论批大小都要读取文件的次数。这非常有用,谢谢!问题在于如何设置纪元的数量。您只需将其设置为无论批大小都要读取文件的次数。这非常有用,谢谢!