A sigmoid activation converts an arbitrary numeric value into the [0, 1] interval, producing a score that can be interpreted as a probability. In binary classification, place it on a final Dense layer with one unit. section 4.1.3 section 4.1.7
Binary image classification
A dogs-versus-cats ConvNet ends with `Dense(1, activation="sigmoid")`. Its single output represents the probability of one class or the other. Compile this arrangement with `binary_crossentropy`, commonly using the `adam` optimizer. section 8.2.3
Use sigmoid with these practical rules:
- Choose one sigmoid output unit for two-class classification rather than separate outputs for both classes. section 4.1.7
- Pair a single sigmoid output with binary crossentropy during model compilation. section 4.1.7 section 4.1.3
- Do not use sigmoid for unrestricted scalar regression: it limits predictions to values between 0 and 1. section 4.3.3
Listing 8.7 Instantiating a small ConvNet for dogs vs. cats classification
import keras from keras import layers inputs = keras.Input(shape=(180, 180, 3)) #1 x = layers.Rescaling(1.0 / 255)(inputs) #2 x = layers.Conv2D(filters=32, kernel_size=3, activation="relu")(x) x = layers.MaxPooling2D(pool_size=2)(x) x = layers.Conv2D(filters=64, kernel_size=3, activation="relu")(x) x = layers.MaxPooling2D(pool_size=2)(x) x = layers.Conv2D(filters=128, kernel_size=3, activation="relu")(x) x = layers.MaxPooling2D(pool_size=2)(x) x = layers.Conv2D(filters=256, kernel_size=3, activation="relu")(x) x = layers.MaxPooling2D(pool_size=2)(x) x = layers.Conv2D(filters=512, kernel_size=3, activation="relu")(x) x = layers.GlobalAveragePooling2D()(x) #3 outputs = layers.Dense(1, activation="sigmoid")(x) model = keras.Model(inputs=inputs, outputs=outputs)
Inspect the complete Keras ConvNet definition to see sigmoid attached to the final Dense layer after convolution and pooling stages. section 8.2.3















