【问题标题】:Why am I getting very small number as CNN prediction?为什么我的 CNN 预测数字非常小?
【发布时间】:2022-01-02 14:26:38
【问题描述】:

我使用 Tensorflow 创建了一个 CNN 来识别肺炎,有时它会返回一个非常小的数字作为预测。为什么会这样?

I have attached the link for the dataset

这里是我如何处理和加载数据。

from tensorflow.keras.preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator( rescale = 1.0/255. )
val_datagen  = ImageDataGenerator( rescale = 1.0/255. )
test_datagen = ImageDataGenerator( rescale = 1.0/255. )

train_generator = train_datagen.flow_from_directory('/kaggle/input/chest-xray-pneumonia/chest_xray/chest_xray/train/',
                                                    batch_size=20,
                                                    class_mode='binary',
                                                    target_size=(350, 350)) 

validation_generator =  val_datagen.flow_from_directory('/kaggle/input/chest-xray-pneumonia/chest_xray/chest_xray/val/',
                                                         batch_size=20,
                                                         class_mode  = 'binary',
                                                         target_size = (350, 350))
test_generator = test_datagen.flow_from_directory('/kaggle/input/chest-xray-pneumonia/chest_xray/chest_xray/test/',
                                                         batch_size=20,
                                                         class_mode  = 'binary',
                                                         target_size = (350, 350

这里是模型,编译和拟合函数

import tensorflow as tf

model = tf.keras.models.Sequential([
    # Note the input shape is the desired size of the image 150x150 with 3 bytes color
    tf.keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(350, 350, 3)),
    tf.keras.layers.MaxPooling2D(2,2),
    tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2), 
    tf.keras.layers.Conv2D(64, (3,3), activation='relu'), 
    tf.keras.layers.MaxPooling2D(2,2),
    # Flatten the results to feed into a DNN
    tf.keras.layers.Flatten(), 
    # 512 neuron hidden layer
    tf.keras.layers.Dense(1024, activation='relu'), 
    # Only 1 output neuron. It will contain a value from 0-1 where 0 for 1 class ('cats') and 1 for the other ('dogs')
    tf.keras.layers.Dense(1, activation='sigmoid')  
])

编译模型

from tensorflow.keras.optimizers import RMSprop

model.compile(optimizer=RMSprop(learning_rate=0.001),
              loss='binary_crossentropy',
              metrics = ['accuracy'])

模型拟合

history = model.fit(train_generator,
                              validation_data=validation_generator,
                              steps_per_epoch=200,
                              epochs=2000,
                              validation_steps=200,
                              callbacks=[callbacks],
                              verbose=2)

评估指标如下,损失:0.2351 - 准确率:0.9847

预测显示阴性肺炎的数字非常小,而阳性则显示超过 0.50。

我有两个问题:

  1. 为什么我得到一个非常小的数字2.xxxx * 10e-20?

  2. 为什么我不能将以下值设为 null?

    val_acc = history.history[ 'val_accuracy' ] 
    val_loss = history.history['val_loss' ]
    

【问题讨论】:

  • 2*10e-20 不是负数。你需要寻找科学记数法。
  • @Frightera 是的,不是,我已经编辑了我的问题。感谢您的评论。

标签: python tensorflow machine-learning conv-neural-network image-classification


【解决方案1】:

我看到你的代码没有问题,你得到的结果也没有问题。

这是一个二元分类问题(2 类:阳性或阴性肺炎),您的模型的输出是一个神经元,其值介于 0 和 1 之间。

所以如果输出高于 0.5,这意味着肺炎阳性。否则,当你有一个非常小的值时,比如2 * 10e-20,这意味着它是阴性肺炎。

对于您的第二个问题,您不应该仅仅因为模型经过良好训练并且在训练数据上具有 98% 的准确率而将准确率和损失值设为空。

【讨论】:

  • history= model.fit(),历史对象既没有 val_accury/val_acc 也没有 val_loss。这就是为什么我提到它没有价值。
  • @NaminduSanchila,您能否将val_acc = history.history[ 'val_accuracy' ] val_loss = history.history['val_loss' ] 的输出添加到问题中。
  • 显示错误,--------------------------------------------------------------------------- KeyError Traceback (most recent call last) /tmp/ipykernel_35/1406712358.py in <module> 6 #----------------------------------------------------------- 7 acc = history.history['accuracy'] ----> 8 val_acc = history.history['val_acc'] 9 loss = history.history['loss'] 10 # val_loss = history.history['val_loss'] KeyError: 'val_acc'
  • 第一个 epoch 是这样显示的200/200 [==============================] - 68s 303ms/step - loss: 0.3897 - accuracy: 0.8739 - val_loss: 1.0525 - val_accuracy: 0.6875 然后在 epochs 之后它没有显示 val_acc 和 val_loss
  • @NaminduSanchila 请关注您的问题(必要时编辑)到一个明确的问题;实际上,很难看到您的问题究竟是关于什么的,因为您似乎将非常小的数字与负数混淆了。 “为什么我无法将以下值设为 null?”不像“我无法获得 val_acc 和 val_loss 值”,因为您似乎暗示你的 cmets 在这里。
猜你喜欢
  • 2021-04-15
  • 1970-01-01
  • 2019-09-11
  • 1970-01-01
  • 2014-08-12
  • 2021-01-29
  • 1970-01-01
  • 2018-01-04
  • 2017-07-31
相关资源
最近更新 更多