机器学习 – Tensorflow:在TFRecords中分离培训和评估数据

我有一个.tfrecords文件填充标记数据.我想将X%用于培训,使用(1-X)%进行评估/测试.显然不应该有任何重叠.这样做的最佳方式是什么?

下面是我阅读tfrecords的小块代码.有什么办法可以让shuffle_batch将数据分成训练和评估数据吗?我错了吗?

reader = tf.TFRecordReader()
files = tf.train.string_input_producer([TFRECORDS_FILE], num_epochs=num_epochs)

read_name, serialized_examples = reader.read(files)
features = tf.parse_single_example(
  serialized = serialized_examples,
  features={
      'image': tf.FixedLenFeature([], tf.string),
      'value': tf.FixedLenFeature([], tf.string),
  })
image = tf.decode_raw(features['image'], tf.uint8)
value = tf.decode_raw(features['value'], tf.uint8)

image, value = tf.train.shuffle_batch([image, value],
 enqueue_many = False,
 batch_size = 4,
 capacity  = 30,
 num_threads = 3,
 min_after_dequeue = 10)

最佳答案 虽然这个问题是在一年前提出的,但我最近也有类似的问题.

我在输入哈希上使用了tf.data.Dataset和过滤器.这是一个示例:

dataset = tf.data.TFRecordDataset(files)

if is_evaluation:
  dataset = dataset.filter(
    lambda r: tf.string_to_hash_bucket_fast(r, 10) == 0)
else:
  dataset = dataset.filter(
    lambda r: tf.string_to_hash_bucket_fast(r, 10) != 0)

dataset = dataset.map(tf.parse_single_example)

return dataset

到目前为止,我注意到的一个缺点是每个评估可能需要数据遍历10倍才能收集到足够的数据.为避免这种情况,您可能希望在数据预处理时间分离数据.

点赞