title: How to bulk write TFRecords?
body: <p>I have a CSV with approximately 40 million rows. Each row is a training instance. As per <a href="https://www.tensorflow.org/programmers_guide/datasets#reading_input_data" rel="noreferrer">the documentation on consuming TFRecords</a> I am trying to encode and save the data in a TFRecord file. </p>
  
  <p>All the examples I have found (<a href="https://github.com/tensorflow/tensorflow/blob/3c3c0481ec087aca4fa875d6d936f19b31191fc1/tensorflow/examples/how_tos/reading_data/convert_to_records.py" rel="noreferrer">even the ones in the TensorFlow repo</a>) show the process of creating a TFRecord is dependant on the class TFRecordWriter. This class has a method <code>write</code> that takes as input a serialised string representation of the data and writes it to disk. However, this appears to be done one training instance at a time.</p>
  
  <p><strong>How do I write a batch of the serialised data?</strong></p>
  
  <p>Let's say I have a funtion:</p>
  
  <pre><code>  def write_row(sentiment, text, encoded):
      feature = {"one_hot": _float_feature(encoded),
                 "label": _int64_feature([sentiment]),
                 "text": _bytes_feature([text.encode()])}
  
      example = tf.train.Example(features=tf.train.Features(feature=feature))
      writer.write(example.SerializeToString())
  </code></pre>
  
  <p>Writing to disk 40 million times (once for each example) is going to be incredibly slow. It would be far more efficient to batch this data and write 50k or 100k examples at a time (as far as the machine's resources will allow). However there does not appear to be any method to do this inside <code>TFRecordWriter</code>. </p>
  
  <p>Something along the lines of:</p>
  
  <pre><code>class MyRecordWriter:
  
    def __init__(self, writer):
      self.records = []
      self.counter = 0
      self.writer = writer
  
    def write_row_batched(self, sentiment, text, encoded):
      feature = {"one_hot": _float_feature(encoded),
                 "label": _int64_feature([sentiment]),
                 "text": _bytes_feature([text.encode()])}
  
      example = tf.train.Example(features=tf.train.Features(feature=feature))
      self.records.append(example.SerializeToString())
      self.counter += 1
      if self.counter &gt;= 10000:
        self.writer.write(os.linesep.join(self.records))
        self.counter = 0
        self.records = []
  </code></pre>
  
  <p>But when reading the file created by this method I get the following error:</p>
  
  <pre><code>tensorflow/core/framework/op_kernel.cc:1192] Invalid argument: Could not parse example input, value: '
  ��
  
  label
  
  ��
  one_hot����
  ��
  </code></pre>
  
  <p>Note: I could change the encoding process so that each <code>example</code> proto contains several thousand examples instead of just one but 
  I don't want to pre-batch the data when writing to the TFrecord file in this way as it will introduce extra overhead in my training pipeline when I want to use the file for training with different batch sizes.</p>