lonyen11
lonyen11

Reputation: 93

How to make tensorflow CNN return probability of a sample for each possible class?

Is there a way to make my tensorflow CNN return the probability of a sample for each of the possible classes? (E.g. sample x has a 83% chance of belonging to class 0, a 7% chance of class 1, a 10% chance of class 2).

My model:

model_0 = keras.Sequential()
model_0.add(Conv1D(32, kernel_size=3, strides=1, activation='relu', input_shape=(X_train.shape[1], 1)))
model_0.add(Dropout(0.1))
model_0.add(Conv1D(64, kernel_size=3, strides=1, activation='relu'))
model_0.add(Dropout(0.2))
model_0.add(Conv1D(32, kernel_size=3, strides=1, activation='relu'))
model_0.add(Flatten())
model_0.add(Dense(3, activation='softmax')) 
model_0.compile(optimizer=Adam(learning_rate = 0.001), loss = 'sparse_categorical_crossentropy', metrics = ['accuracy'])
model_0.summary()
history_0 = model_0.fit(X_train, y_train, epochs=30, validation_data=(X_val, y_val), verbose=1)

y_pred_0 = np.argmax(model_0.predict(X_test_pad), axis=-1)

Currently, by default, y_pred_0 is just a vector containing the index of the predicted class for each test sample. What would I have to change in my model in order to get probabilities?

Upvotes: 0

Views: 514

Answers (1)

AnhPC03
AnhPC03

Reputation: 646

Just simply archive output in probabilities by

y_pred_0 = model_0.predict(X_test_pad)

You would get as your example is

[0.83, 0.07, 0.1]

Upvotes: -1

Related Questions