【问题标题】:Memory leak in TensorFlow upon starting new epoch开始新纪元时 TensorFlow 中的内存泄漏
【发布时间】:2017-07-23 23:54:28
【问题描述】:

我正在 TensorFlow 中编写一个训练脚本,以对两种不同类型的图像进行分类。这是创建数据集对象的类,该对象用于生成批次和递增时期。它工作正常,直到第一个 epoch 完成。然后它在next_batch 方法中的self._images = self._images[perm] 行失败。这对我来说没有意义,因为 Python 不应该复制 self._images——只需要重新洗牌。

class DataSet(object):
  def __init__(self, images, labels, norm=True):
    assert images.shape[0] == labels.shape[0], (
      "images.shape: %s labels.shape: %s" % (images.shape,
                                         labels.shape))
    self._num_examples = images.shape[0]
    self._images = images
    self._labels = labels
    self._epochs_completed = 0
    self._index_in_epoch = 0
    self._norm = norm
    # Shuffle the data right away
    perm = np.arange(self._num_examples)
    np.random.shuffle(perm)
    self._images = self._images[perm]
    self._labels = self._labels[perm]
  @property
  def images(self):
    return self._images
  @property
  def labels(self):
    return self._labels
  @property
  def num_examples(self):
    return self._num_examples
  @property
  def epochs_completed(self):
    return self._epochs_completed
  def next_batch(self, batch_size):
    """Return the next `batch_size` examples from this data set."""
    start = self._index_in_epoch
    self._index_in_epoch += batch_size
    if self._index_in_epoch > self._num_examples:
      # Finished epoch
      self._epochs_completed += 1
      print("Completed epoch %d.\n"%self._epochs_completed)
      # Shuffle the data
      perm = np.arange(self._num_examples)
      np.random.shuffle(perm)
      self._images = self._images[perm] # this is where OOM happens
      self._labels = self._labels[perm]
      # Start next epoch

在普通训练周期中,内存使用量不会增加。这是训练代码的一部分。 data_train_norm 是一个 DataSet 对象。

batch_size = 300
csv_plot = open("csvs/train_plot.csv","a")
for i in range(3000):
    batch = data_train_norm.next_batch(batch_size)
    if i%50 == 0:
            tce = cross_entropy.eval(feed_dict={x:batch[0],y_:batch[1],keep_prob:1.0},session=sess)
            print("\nstep %d, train ce %g"%(i,tce))
            print datetime.datetime.now()
            csv_plot.write("%d, %g\n"%(i,tce))

    train_step.run(feed_dict={x:batch[0],y_:batch[1],keep_prob:0.8},session=sess)

version = 1
saver.save(sess,'nets/cnn0nu_batch_gpu_roi_v%02d'%version)
csv_plot.close()

【问题讨论】:

    标签: python memory tensorflow


    【解决方案1】:

    这很可能是因为这段代码你在图中添加了一个新的next_batch 操作

    for i in range(3000):
        batch = data_train_norm.next_batch(batch_size)
    

    方法data_train_norm.next_batch创建一个新的TensorFlow操作,所以你应该只调用一次并使用创建的操作(保留在batch中)。查看doc 中的示例,例如:

    dataset = tf.contrib.data.Dataset.range(100)
    iterator = dataset.make_one_shot_iterator()
    next_element = iterator.get_next()
    
    for i in range(100):
      value = sess.run(next_element)
      assert i == value
    

    另外,调试TensorFlow内存泄漏时,可以使用sess.graph.finalize()

    【讨论】:

      【解决方案2】:

      您使用的是:dataset = dataset.shuffle(buffer_size)?

      尝试减少buffer_size。这对我有用

      【讨论】:

        猜你喜欢
        • 2017-11-03
        • 2011-08-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-27
        • 1970-01-01
        • 2018-03-14
        相关资源
        最近更新 更多