Search code examples
pythontensorflowdeep-learningnlpconv-neural-network

Output the confiendence / probability for a class of a CNN neuronal network


I have a problem. I want to get the confiendence/ probability for my prediction. How could I get the confidence? I looked at How can I implement confidence level in a CNN with tensorflow? . But I do not understand how I could get the prediction.

class CNN_Text:
    def __init__(self, x, y):
        self.x =x
        self.y = y

    def forward(self):
            filter_sizes = [1,2,3,5]
            num_filters = 32
        
            inp = Input(shape=(maxlen, ))
            x = Embedding(embedding_matrix.shape[0], 300, weights=[embedding_matrix], trainable=False)(inp)
            x = SpatialDropout1D(0.4)(x)
            x = Reshape((maxlen, embed_size, 1))(x)

            conv_0 = Conv2D(num_filters, kernel_size=(filter_sizes[0], embed_size), kernel_initializer='normal',
                                                                                            activation='elu')(x)
            conv_1 = Conv2D(num_filters, kernel_size=(filter_sizes[1], embed_size), kernel_initializer='normal',
                                                                                            activation='elu')(x)
            conv_2 = Conv2D(num_filters, kernel_size=(filter_sizes[2], embed_size), kernel_initializer='normal',
                                                                                            activation='elu')(x)
            conv_3 = Conv2D(num_filters, kernel_size=(filter_sizes[3], embed_size), kernel_initializer='normal',
                                                                                            activation='elu')(x)

            maxpool_0 = MaxPool2D(pool_size=(maxlen - filter_sizes[0] + 1, 1))(conv_0)
            maxpool_1 = MaxPool2D(pool_size=(maxlen - filter_sizes[1] + 1, 1))(conv_1)
            maxpool_2 = MaxPool2D(pool_size=(maxlen - filter_sizes[2] + 1, 1))(conv_2)
            maxpool_3 = MaxPool2D(pool_size=(maxlen - filter_sizes[3] + 1, 1))(conv_3)

            z = Concatenate(axis=1)([maxpool_0
                                     , maxpool_1
                                     , maxpool_2
                                     , maxpool_3
                                     ]) 
            # z = Dropout(0.3)(z)  
            z = Flatten()(z)
            z = Dropout(0.3)(z)

            outp = Dense(53, activation="softmax")(z)

            model = Model(inputs=inp, outputs=outp)
            model.summary()
            return model

p1 = CNN_Text(...)
model = p1.forward()

model.compile(...)
history = model.fit(...)
pred = model.predict(...)

How I predict a class

x = x.lower()
x = remove_URL(x)
x = remove_punct(x)
x = remove_stopwords(x)
    
x = tokenizer.texts_to_sequences([x])
x = pad_sequences(x, maxlen=maxlen)
pred = model.predict(x)
pred = pred.argmax(axis=1)
pred = le.classes_[pred]
return pred[0]

Solution

  • The Softmax activation function normalises the output of the network, giving you the predicted probability of each of the 53 classes for a given sample.

    pred = pred.argmax(axis=1)
    

    This line gives you the index of the node with the highest predicted probability.

    pred = pred.max(axis=1)
    

    This will give you the according probability instead (If you don't overwrite pred with argmax before).