gpt4 book ai didi

python - 从 Keras 中的生成器获取 x_test、y_test?

转载 作者:行者123 更新时间:2023-11-28 22:17:55 27 4
gpt4 key购买 nike

对于某些问题,验证数据不能是生成器,例如:TensorBoard histograms :

If printing histograms, validation_data must be provided, and cannot be a generator.

我当前的代码如下:

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)

最佳答案

Python 2.7 和 Python 3.* 解决方案:

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/7f6cc03ed6c3dd84548a039e2e0fd006

关于python - 从 Keras 中的生成器获取 x_test、y_test?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50928329/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com