【发布时间】: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