title: How to limit RAM usage while batch training in tensorflow?
body: <p>I am training a <em>deep neural network</em> with a <em>large image dataset</em> in mini-batches of size 40. My dataset is in <code>.mat</code> format (which I can easily change to any other format e.g. <code>.npy</code> format if necessitates) and before training, loaded as a 4-D <code>numpy</code> array. My problem is that while training, cpu-RAM (not GPU RAM) is very quickly exhausting and starts using almost half of my Swap memory.</p>
  
  <p>My training code has the following pattern:</p>
  
  <pre><code>batch_size = 40
  ...
  with h5py.File('traindata.mat', 'r') as _data:
      train_imgs = np.array(_data['train_imgs'])
  
  # I can replace above with below loading, if necessary
  # train_imgs = np.load('traindata.npy')
  
  ...
  
  shape_4d = train_imgs.shape 
  for epoch_i in range(max_epochs):
      for iter in range(shape_4d[0] // batch_size):
          y_ = train_imgs[iter*batch_size:(iter+1)*batch_size]
          ...
          ...
  </code></pre>
  
  <p>This seems like the initial loading of the full training data is itself becoming the bottle-neck (taking over 12 GB cpu RAM before I abort).</p>
  
  <p>What is the best efficient way to tackle this bottle-neck?</p>
  
  <p>Thanks in advance.</p>