dabo1
dabo1

Reputation: 51

Receiving negative probabilities for my predictions

I got a model that guesses the gender and ethnicity of a person based of a 48x48 image from the data set(https://www.kaggle.com/nipunarora8/age-gender-and-ethnicity-face-data-csv).

The model is created with:

input_layer = keras.Input(shape=(48,48),name='img')
first_dense = keras.layers.Conv1D(128,kernel_size=48, activation='relu')(input_layer)
y1_output = keras.layers.Dense(units=2, name='genderoutput')(first_dense)
second_dense = keras.layers.Dense(units='64',activation='relu')(first_dense)
y2_output = keras.layers.Dense(units='5',name='ethnicityoutput')(second_dense)
model = keras.Model(inputs=input_layer,outputs=[y1_output, y2_output])
model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

and trained with

label ={"genderoutput": np.reshape(gedataset['gender'][:trainamount].to_numpy(),(-1,1)),"ethnicityoutput": np.reshape(gedataset['ethnicity'][:trainamount].to_numpy(),(-1,1))}
model.fit(traindata,y=label , epochs=10)

I generate the predictions with

predictions = model.predict(images)

The probabilities I receive e.g. for the gender of the person from one image can be [-184.00462 -183.6125 ], while the probabilities for the ethnicity can look like [21.918806 18.164877 11.856606 20.279469 20.550903] for the same image. It all varies pretty drastically each time I recreate the module with the same parameters. I am fairly new to creating models, but I am fairly certain the added probability should only be 1 and not be able to go negative. I wrote the code before with only gender as output where the probabilities added nicely up to 1, although I used for that a sequential model instead of a function model, so I guess I made the error somewhere with creating the layers of the function model, but can't seem to figure out how to modify it to get the probabilities to add up to 1.

Upvotes: 1

Views: 759

Answers (1)

dabo1
dabo1

Reputation: 51

Issue solved, y1_output and y2_output needed activation='softmax

Upvotes: 1

Related Questions