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:
| 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:
- Slice Data:
features = df.iloc[1:, 1:]
labels = df.iloc[1:, 0]
- 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'])
- 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:
tf.keras.layers.Dense(1, activation='softmax'), change this totf.keras.layers.Dense(2, activation='softmax')- 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.