How to make prediction in Tensorflow?

0

Issue

This seems to be a simple question, but yet I can’t find anywhere online.

I have run through the tutorial of text classification (Positive and Negative)
https://www.tensorflow.org/tutorials/keras/text_classification_with_hub

After running all the codes, I wish to make prediction with my own text, says, “Test”.

model.predict(["Test"])
#output
array([[-0.87035626]], dtype=float32)

It return 1 value, positive or negative (-0.87 from the above example).

But that doesn’t make sense. I am expecting it returns a classification probability like:

[0:0.2, 1:0.8]

which means, label 0 has 0.2 probability, and label 1 has 0.8 probability.

Can someone guide me on this ?

Solution

It is indeed odd, since you would expect values between 0 and 1 (regardless of sigmoid or softmax)

The error is in the TensorFlow tutorial.

On the last line of code you have : model.add(Dense(1)) with no activation provided. If none is provided, then it defaults to linear activation, which is actually suitable for regression problems.

What you can do in order to get the results in the form you want is to add the final layer and the model compilation like:

model.add(tf.keras.layers.Dense(2, activation = 'softmax'))
...
model.compile(optimizer='adam',
          loss=tf.keras.losses.CategoricalCrossentropy(from_logits=False),
          metrics=['accuracy'])

Answered By – Timbus Calin

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More