Tensorflow 模型的输出。预测_生成器()]

Tensorflow 模型的输出。预测_生成器()],tensorflow,keras,neural-network,conv-neural-network,cnn,Tensorflow,Keras,Neural Network,Conv Neural Network,Cnn,我是Keras的新手,我有一个问题。当我在测试数据集上运行predict_generator时,我得到了奇怪的输出。如何获取介于0和1之间的数字 model.predict_generator(generator=test_generator, verbose = 1) 当我尝试的时候,我得到了这样的东西 [9.11257455100e-01, 1.11224177e-01, 6.96873654e-03, 6.45312342e-01, 我如何提供我的列车数据: 基本上我有5

我是Keras的新手,我有一个问题。当我在测试数据集上运行predict_generator时,我得到了奇怪的输出。如何获取介于0和1之间的数字

model.predict_generator(generator=test_generator, verbose = 1)
当我尝试的时候,我得到了这样的东西

       [9.11257455100e-01, 1.11224177e-01, 6.96873654e-03, 6.45312342e-01,
我如何提供我的列车数据:

基本上我有5个文件夹,比如说命名为A、B、C、D、E,每个文件夹都有与其类别名称相关的图片

datagen = tf.keras.preprocessing.image.ImageDataGenerator(
        rescale = 1./255, 
        validation_split=0.2, 
        featurewise_center=False, 
        samplewise_center=False,  
        featurewise_std_normalization=False,  
        samplewise_std_normalization=False,  
        zca_whitening=False,  
        zoom_range = 0.1,
        width_shift_range=0.1, 
        height_shift_range=0.2,  
   
       )
最后:

hist = model.fit_generator(
                     train_generator,
                     epochs=100,
                     shuffle = True,
                     validation_data= validation_generator,
                     steps_per_epoch = 64,
                     verbose = 1
                     )

它不像看上去那么复杂。运行模型预测时,它会对每个输入样本进行预测。数组中的每一行对应一个样本的预测。现在,数组中的列数等于模型训练的类数,在本例中,这些类看起来是5个类。列中的数字是样本位于该行表示的类中的概率。例如,在矩阵的第一行中,2.30657243e-10的值表示样本属于类别1的概率。第2列中的值4.47556471e-11表示样本1属于类别2等的概率。第4列中的值1.00000000 E+00表示样本属于类别4的概率基本上为100%。下一行中的数据对于第二个示例的结构类似。要查找最可能的类,请使用np.argmax查找概率最高的列,以获得该类最可能的预测。要使打印结果更具可读性,请使用下面的代码

preds=model.predict_generator(generator=test_generator, verbose = 1)
preds=preds.round(decimals=2)
for p in preds:
    print (p)
我假设您使用ImageDataGenerator创建测试样本。如果你做了,这里是如何打印出你的结果清楚

test_file_names=test_gen.filenames  # sequential list of name of test files of each sample
test_labels=test_gen.labels # is a sequential list  of test labels for each image sample
class_dict= test_gen.class_indices # a dictionary where key is the class name and value is the corresponding label for the class
print (class_dict) # have a look at the dictionary
new_dict={} 
for key in class_dict: # set key in new_dict to value in class_dict and value in new_dict to key in class_dict
    value=class_dict[key]
    new_dict[value]=key
print('PREDICTED CLASS  TRUE CLASS       FILENAME ' ) # adjust spacing based on your class names
for i, p in enumerate(preds):
    pred_index=np.argmax(p) # get the index that has the highest probability
    pred_class=new_dict[pred_index]  # find the predicted class based on the index
    true_class=new_dict[test_labels[i]] # use the test label to get the true class of the test file
    file=test_file_names[i]
    print(f'    {pred_class}       {true_class}       {file}')

那些是介于0和1之间的数字。是的,我知道。谢谢,非常感谢。为了这个答案。现在这更有意义。有没有办法得到介于0和1之间的结果,而不是这些奇怪的数字,以使其更易于阅读?参见修正后的答案如果你不介意的话,我可以再问你一个问题吗。。多亏了你,现在的输出非常清晰。我还想看看预测输出类。例如,在一行中,我得到这个[0.1.0.0.0.]。所以我想看看值1代表什么。正如你所说,我有5门课,那么它属于哪一门呢?提前非常感谢。这取决于您如何提供培训数据。没有这些,我就说不出我编辑这个问题是为了向你展示我是如何提供培训数据的。非常感谢。