python-3.x - 使用predict_generator时如何返回项目的真实标签?

标签 python-3.x machine-learning neural-network deep-learning keras

我正在使用 predict_generator() 函数观察神经网络的输出,但我无法看到预测项目的真实标签。如何实现一个 block 来查看输入项的真实标签?

test_datagen = ImageDataGenerator(
rescale=1./255,
rotation_range=45,
width_shift_range=0.25,
height_shift_range=0.25,
horizontal_flip=True,
)
test_generator = test_datagen.flow_from_directory(
    evaluate_path,
    target_size=(width, height),
    batch_size=batch_size,
    class_mode='categorical')

model.compile(optimizer=SGD(lr=0.0001, momentum=0.9),      loss='categorical_crossentropy', metrics=['accuracy'])
x = model.predict_generator(test_generator, val_samples=1)
print(x)

最佳答案

尝试以下功能:

from six import next

def generator_with_true_classes(model, generator):
    while True:
        x, y = next(generator)
        yield x, model.predict(x), y

它将产生原始数据,y_predy_true。按以下方式使用:

nb_of_samples = 0
nb_of_samples_to_compute = 100 # set your own value
for x, y_pred, y_true in generator_with_true_classes(model, test_generator):
    # do something with data, eg. print it.
    nb_of_samples += 1
    if nb_of_samples == nb_of_samples_to_compute:
         break

关于python-3.x - 使用predict_generator时如何返回项目的真实标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44970445/

相关文章:

machine-learning - Caffe:如何使用已训练的模型一次检查多个数据集的准确性?

neural-network - 我可以在反向传播过程中(选择性地)反转Theano梯度吗?

python-3.x - 是否可以保存经过训练的层以在 Keras 上使用层?

python - 如何在 Python 包中导入符号?

python - Could not convert string to float 错误来自泰坦尼克号竞赛

python-3.x - 如何使用python使用hdfs3 lib上传HDFS上的本地文件

numpy - Keras:ValueError:decode_predictions 需要一批预测

python - Keras:嵌入层的加权平均值

string - Hackerrank `super-functional-strings` 因超时而终止

python - 从另一个文件获取变量 - python