【发布时间】:2017-10-28 18:38:05
【问题描述】:
有人能指出一个完整的例子吗?
- 使用
train_on_batch()将批处理(和腌制)数据放入循环中 - 为验证目的保留每批次的数据
- 在处理完所有个批次后留出测试数据以进行准确性评估(请参阅下面我示例的最后一行)。
我在互联网上找到了很多 1 - 5 行代码 sn-ps,说明如何调用 train_on_batch() 或 fit_generator(),但到目前为止,还没有任何内容能清楚说明如何分离和处理验证和测试数据在使用train_on_batch()时。
F。 Chollet 的出色示例 Cifar10_cnn (https://github.com/fchollet/keras/blob/master/examples/cifar10_cnn.py) 并未说明我上面列出的所有要点。
您可以说,“嘿,处理测试数据是您的问题。手动操作。”美好的!但我不明白这些例程做得好到什么程度,甚至不知道这是否有必要。它们大多是黑匣子,据我所知,它们在后台自动处理验证和测试数据。我希望更完整的示例能够消除混淆。
例如,在下面的示例中,我从 pickle 文件中迭代地读取批次,我将如何修改对 train_on_batch 的调用以处理 validation_data?以及如何留出测试数据(test_x 和test_y)以便在算法结束时评估准确性?
while 1:
try:
batch = np.array(pickle.load(fvecs))
polarities = np.array(pickle.load(fpols))
# Divide a batch of 1000 documents (movie reviews) into:
# 800 rows of training data, and
# 200 rows of test (validation?) data
train_x, val_x, train_y, val_y = train_test_split(batch, polarities, test_size=0.2)
doc_size = 30
x_batch = pad_sequences(train_x, maxlen=doc_size)
y_batch = train_y
# Fit the model
model.train_on_batch(x_batch, y_batch)
# model.fit(train_x, train_y, validation_data=(val_x, val_y), epochs=2, batch_size=800, verbose=2)
except EOFError:
print("EOF detected.")
break
# Final evaluation of the model
scores = model.evaluate(test_x, test_y, verbose=0)
print("Accuracy: %.2f%%" % (scores[1] * 100))
【问题讨论】:
标签: deep-learning keras batch-processing metrics