Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get class prediction for new entry value in neural networks?

I am new to Nerual Networks model building !! I am doing an multi-class text classification using neural networks.

Steps I have done :

1.Data Cleaning
2.Keras – Text Vectorization for Input Data
3.Dummie values for Label data

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I have build the model like this:

model = tf.keras.Sequential([
  layers.Embedding(max_features + 1, embedding_dim),
  layers.Dropout(0.2),
  layers.GlobalAveragePooling1D(),
  layers.Dropout(0.2),
  layers.Dense(10)])

model.summary()

model.compile(loss=losses.BinaryCrossentropy(from_logits=True),
              optimizer='adam',
              metrics=tf.metrics.BinaryAccuracy(threshold=0.0))

epochs = 10
history = model.fit(X_train,Y_train , validation_data=(X_test , Y_test), epochs=epochs)

I am getting 90 accuracy , but the problem is when I want to test with a single text sample , I am getting an array of values like these [-1.975121 , -2.8071122, -2.183652 , …, -2.3590472, -2.214075 ,
-1.9891262] ,but in these which one I should pick ?

And How to do it ??

>Solution :

I am going to guess based on your model shape (layers.Dense(10)) that you’ve 10 classes to classify. In that case, when you do a model.predict you’d get an array of shape (10,) where each element corresponds to a class.

The highest scoring index is the class the model classified. You can get it by:

import numpy as np
predicted_class = np.argmax(<your_models_prediction_array>)

Also, considering it’s a multiclass classification task you should change your model.compile to match >2 (non-binary) classification task:

model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading