Model.predict outputs do not add up to one

Tell us what’s happening:
I have re scaled my inputs so all inputs are divided by 255. I have set the last Dense layer to 2 so I was hoping to get 2 values between 0 and 1 that add up to 1. Instead I seem to be getting random integers.

I know the model is not accurate that is part of the challenge and improving the accuracy is not what I am asking here. I just want to know how to get the expected output.

Your code so far
This is the model:

model = Sequential()
model.add(layer=tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(IMG_HEIGHT, IMG_WIDTH, 3)))
model.add(layer=tf.keras.layers.MaxPooling2D((2, 2)))
model.add(layer=tf.keras.layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layer=tf.keras.layers.MaxPooling2D((2, 2)))
model.add(layer=tf.keras.layers.Conv2D(64, (3, 3), activation='relu'))

model.add(layer=tf.keras.layers.Flatten())
model.add(layer=tf.keras.layers.Dense(64, activation='relu'))
model.add(layer=tf.keras.layers.Dense(2))
model.compile(optimizer='adam', loss=tf.keras.losses.BinaryCrossentropy(), metrics=['accuracy'])

model.summary()

This is my fitting the model:

history = model.fit(train_data_gen, steps_per_epoch=10, epochs=epochs, validation_data=val_data_gen, validation_steps=800)

This is what generates the values that I expect to be between 0 and 1

probabilities = model.predict(test_data_gen, batch_size=batch_size)
print(probabilities)

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36.

Challenge: Cat and Dog Image Classifier

Link to the challenge:

Ok so I figured this out and posting answer in case anyone else runs into the same issue.
The following code is missing an activation function on the last layer. This activation function is what squashes the numbers between 0 and 1. Also BinaryCrossEntropy loss function is designed to use with one output that goes between 0 and 1 not 2 outputs, one for each class.

model.add(layer=tf.keras.layers.Flatten())
model.add(layer=tf.keras.layers.Dense(64, activation='relu'))
model.add(layer=tf.keras.layers.Dense(2))
model.compile(optimizer='adam', loss=tf.keras.losses.BinaryCrossentropy(), metrics=['accuracy'])

I have now changed this code to

model.add(layer=tf.keras.layers.Flatten())
model.add(layer=tf.keras.layers.Dense(64, activation='relu'))
model.add(layer=tf.keras.layers.Dense(1, activation='sigmoid'))
model.compile(optimizer='adam', loss=tf.keras.losses.BinaryCrossentropy(), metrics=['accuracy'])
2 Likes