valentineday
valentineday

Reputation: 79

Question about Binary classification Keras

I'm studying classification system using keras. Keras classify labels by checking which is the highest number. For instance, if output is [0.1 0.8 0.1], it classify label 1 as correct answer because it is 0.8 which is highest number.

But in binary classification I got this result.

[0.642]
[0.996]
[0.976]
[0.302]
[0.963]
[0.115]
   .
   .
   .

I thought Keras classify them as: if result[i][0] > 0.5 return 1

But it is only my guess so I want to know exactly how Keras classify binary results.

Here is my code I used.

model = Sequential()

# Step 1 - Convolution
model.add(Conv2D(32, (3, 3), input_shape = (64, 64, 3), activation = 'relu'))

# Step 2 - Pooling
model.add(MaxPooling2D(pool_size = (2, 2)))

# Adding a second convolutional layer
model.add(Conv2D(32, (3, 3), activation = 'relu'))
model.add(MaxPooling2D(pool_size = (2, 2)))

# Step 3 - Flattening
model.add(Flatten())

# Step 4 - Full connection
model.add(Dense(units = 128, activation = 'relu'))
model.add(Dense(units = 1, activation = 'sigmoid'))

import numpy as np

print("-- Predict --")
output = model.predict_generator(test_set, steps=5)
np.set_printoptions(formatter={'float': lambda x: "{0:0.3f}".format(x)})
print(test_set.class_indices)
print(output)

Upvotes: 0

Views: 76

Answers (2)

Raj kamal Srivastav
Raj kamal Srivastav

Reputation: 204

In your code, we can see model.add(Dense(units = 1, activation = 'sigmoid')). So we are using sigmoid as activation function. if you see this link, you will see that range of function is [0,1] so considering threshold as 0.5, classes are assigned.

if you would have been doing multi-class classification, softmax will be the appropriate activation function.

Upvotes: 2

De Poudelas
De Poudelas

Reputation: 13

I recommend you to learn various activation function. In the given exmaple, you are using sigmoid activation function which outputs a continous range of values between 0 and 1. If you use softmax function , it will generate outputs a vector that represents the probability distributions of a list of potential outcomes. it will turns numbers aka logits into probabilities that sum to one [0.1 0.8 0.1] like you mentioned in your question.

Upvotes: 0

Related Questions