[英]how to shuffle in itertools.cycle()?
I'm using the itetools
in the data generator in tensorflow based network training. 我使用的是itetools
在基于tensorflow网络训练数据生成。 The main framework is as follows: 主要框架如下:
def data_generator(filenames, batch_size):
files= itertools.cycle(filenames)
while True:
X = []
Y = []
for _ in range(batch_size):
filename = files.next()
# read data into X and Y
....
yield np.array(X), np.array(Y)
When using this data generater, 使用此数据生成器时,
train_input = data_generator(train_filenames,batch_size=1)
for ep in range(num_epochs):
for _ in range(num_train_samples):
image_batch, label_batch = train_input.next()
loss_val = sess.run([loss_op], feed_dict={})
My question is: in general, we need to shuffle the training data after each epoch, how to shuffle in this circumstance? 我的问题是:总的来说,我们需要在每个时期之后对训练数据进行洗牌,在这种情况下如何洗牌? Thanks. 谢谢。
You can not modify a cycle
iterator once created, so you have to make a new one in each "epoch" loop: 一旦创建,您将无法修改cycle
迭代器,因此必须在每个“ epoch”循环中创建一个新的cycle
迭代器:
def data_generator(filenames, batch_size):
filenames = filenames[:] # make a copy
random.shuffle(filenames)
files = itertools.cycle(filenames)
...
for ep in range(num_epochs):
train_input = data_generator(train_filenames, batch_size=1)
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.