Python predict()返回大于1的类概率?

Python predict()返回大于1的类概率?,python,tensorflow,machine-learning,keras,cnn,Python,Tensorflow,Machine Learning,Keras,Cnn,我试图在CNN上调用tf.keras中的model.predict来预测单个图像的类。出于某种原因,类概率会回到高于1的水平,这是毫无意义的。我不确定为什么会发生这种情况。以下是我如何训练我的CNN: class_names = ['Angry','Disgust','Fear','Happy','Sad','Surprise','Neutral'] model = models.Sequential() model.add(layers.Conv2D(64, (3, 3), activatio

我试图在CNN上调用tf.keras中的model.predict来预测单个图像的类。出于某种原因,类概率会回到高于1的水平,这是毫无意义的。我不确定为什么会发生这种情况。以下是我如何训练我的CNN:

class_names = ['Angry','Disgust','Fear','Happy','Sad','Surprise','Neutral']
model = models.Sequential()
model.add(layers.Conv2D(64, (3, 3), activation='relu', input_shape=(48, 48, 1), kernel_regularizer=tf.keras.regularizers.l1(0.01)))
model.add(layers.Conv2D(128, (3, 3), padding='same', activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(tf.keras.layers.Dropout(0.5))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(tf.keras.layers.Dropout(0.5))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))


model.summary()

model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(7))


#model.summary()
model.compile(optimizer='adam',loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True),metrics=['accuracy'])
lr_reducer = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.9, patience=3) #monitors the validation loss for signs of a plateau and then alter the learning rate by the specified factor if a plateau is detected

early_stopper = tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', min_delta=0, patience=6, mode='auto')  #This will monitor and stop the model training if it is not further converging

checkpointer = tf.keras.callbacks.ModelCheckpoint('C:\\Users\\rtlum\\Documents\\DataSci_Projects\\PythonTensorFlowProjects\\Datasets\\FER2013_Model_Weights\\Model\\weights.hd5', monitor='val_loss', verbose=1, save_best_only=True) #This allows checkpoints to be saved each epoch just in case the model stops training

epochs = 100
batch_size = 64
learning_rate = 0.001

model.fit(
          train_data,
          train_labels,
          epochs = epochs,
          batch_size = batch_size,
          validation_split = 0.2,
          shuffle = True,
          callbacks=[lr_reducer, checkpointer, early_stopper]
          )
下面是我如何调用model.predict并传入单个图像以进行预测:

    model = tf.keras.models.load_model('Model\\weights.hd5')
    img = Image.open(test_image).convert('L')
    img = img.resize([48, 48])
    image_data = np.asarray(img, dtype=np.uint8)
    #image_data = np.resize(img,3072)
    image_data = image_data / 255
    image_data_test = image_data.reshape((1, 48, 48, 1))
    class_names = ['Angry','Disgust','Fear','Happy','Sad','Surprise','Neutral']
    x = model.predict(image_data_test)
    app.logger.info(x)
    image_pred = np.argmax(x)
    y = round(x[0][np.argmax(x)], 2)
    confidence = y * 100
    print(class_names[image_pred], confidence)
最后,下面是我从model.predict得到的类概率:

>>> x = model.predict(image_data_test)
>>> x
array([[ 1.0593076 , -3.5140653 ,  0.7505076 ,  2.1341033 ,  0.02394461,
        -0.08749148,  0.6640976 ]], dtype=float32)

添加一个激活层,将您的输出值转换为[0,1]的值。

添加一个激活层,将您的输出值转换为[0,1]的值。

您的最后一个层模型。addlayers.Dense7正在使用线性激活功能。要获得7个类的概率,应使用softmax激活

将最后一层更改为

model.add(layers.Dense(7 , activation='softmax'))
您的最后一个图层model.addlayers.Dense7正在使用线性激活功能。要获得7个类的概率,应使用softmax激活

将最后一层更改为

model.add(layers.Dense(7 , activation='softmax'))

这并不能回答这个问题;具体激活什么?这并不能回答这个问题;具体激活什么?回答没有帮助?回答没有帮助?这解决了问题。谢谢你的帮助~这解决了问题。谢谢你的帮助~