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

tensorflow output layer configuration for classification task

I am trying to teach my first AI with google colab and tensorflow.

.predict method work is not clear to me.


I have following dataset example:

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

classification Input 1 Input 2
1 0.1 0.22
1 0.333 0.4
4 0.55 0.6

Expected classes are 1 or 4. Dataset contains 50% of each.

My code is:

  1. Slice Data:
  features = df.iloc[1:, 1:]
  labels =  df.iloc[1:, 0]
  1. Build Model:
model = tf.keras.Sequential([
    tf.keras.layers.Dense(256, activation='tanh', input_shape=(82,)),
    tf.keras.layers.Dense(2, input_shape=(256,), activation='tanh'),
    tf.keras.layers.Dense(1, activation='softmax')
])

model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
  1. Train Model:
model.fit(x=features, y=labels, shuffle=True, epochs=1)

Results

result of .predict method is always [[1.]].

But I think that is should be smth like:
[1: 0.4][2: 0.88]

where: 1 and 4 are classifications and 0.4 and 0.88 are probability

>Solution :

You have two issues in the configuration:

  1. tf.keras.layers.Dense(1, activation='softmax'), change this to tf.keras.layers.Dense(2, activation='softmax')
  2. Since your labels are in integer format, you need to change your loss function to model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

Also, the way your dataset is formatted and by means of softmax, you will never have an output like you described :

But I think that is should be smth like: [1: 0.4][2: 0.88]

The sum of probabilities will always sum up to 1 in your context.

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