本文介绍了从Keras的生成器获取x_test,y_test?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
对于某些问题,验证数据不能是生成器,例如: TensorBoard
直方图:
For certain problems, the validation data can't be a generator, e.g.: TensorBoard
histograms:
我当前的代码如下:
image_data_generator = ImageDataGenerator()
training_seq = image_data_generator.flow_from_directory(training_dir)
validation_seq = image_data_generator.flow_from_directory(validation_dir)
testing_seq = image_data_generator.flow_from_directory(testing_dir)
model = Sequential(..)
# ..
model.compile(..)
model.fit_generator(training_seq, validation_data=validation_seq, ..)
我如何将其提供为validation_data=(x_test, y_test)
?
How do I provide it as validation_data=(x_test, y_test)
?
推荐答案
Python 2.7和Python 3. *解决方案:
Python 2.7 and Python 3.* solution:
from platform import python_version_tuple
if python_version_tuple()[0] == '3':
xrange = range
izip = zip
imap = map
else:
from itertools import izip, imap
import numpy as np
# ..
# other code as in question
# ..
x, y = izip(*(validation_seq[i] for i in xrange(len(validation_seq))))
x_val, y_val = np.vstack(x), np.vstack(y)
或支持class_mode='binary'
,然后:
from keras.utils import to_categorical
x_val = np.vstack(x)
y_val = np.vstack(imap(to_categorical, y))[:,0] if class_mode == 'binary' else y
完整的可运行代码: https://gist.github.com/AlecTaylor/7f6cc03ed6c3dd84548a039e2e0>
Full runnable code: https://gist.github.com/AlecTaylor/7f6cc03ed6c3dd84548a039e2e0fd006
这篇关于从Keras的生成器获取x_test,y_test?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!