'If my model is trained using sigmoid at the final layer and binary_crossentropy, can I shtill out put probability of classes rather than 0/1?

I have trained a CNN model with dense layer at the end using a sigmoid function:

model.add(layers.Dense(1, activation='sigmoid'))

I have also compiled using binary cross entropy:

model.compile(loss='binary_crossentropy',
              optimizer = 'Adam',
              metrics=[tf.keras.metrics.Precision(),tf.keras.metrics.Recall(),'accuracy'])

The f1 score of the binary images classification comes low and my model predicts one class over the other. So I decided to add a threshold based on the output probability of my sigmoid function at the final layer:

c = load_img('/home/kenan/Desktop/COV19D/validation/covid/ct_scan_19/120.jpg', 
             color_mode='grayscale',
             target_size = (512,512))
c=img_to_array(c)
c= np.expand_dims(c, axis=0)
pred = model.predict_proba(c)
pred
y_classes = ((model.predict(c)> 0.99)+0).ravel() 
y_classes

I want to use 'pred' in my code as a probability of the class but it is always either 0 or 1 as shown below:

Out[113]: array([[1.]], dtype=float32)

why doesn't it give the probability of predicting the class between [0,1] instead of 1? is there a way to get the class probability in my case rather than 0 or 1?



Solution 1:[1]

No you cant. Sigmoid activation in the final layer will output ONE value in the range of 0 to 1. If you want to obtain class probabilities of the different labels, you'll have to change the final layer activation to softmax.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 onyeka okonji