Reputation: 3904
In Keras test sample evaluation is done like this
score = model.evaluate(testx, testy, verbose=1)
This does not return predicted values. There is a method predict
which return predicted values
model.predict(testx, verbose=1)
returns
[
[.57 .21 .21]
[.19 .15 .64]
[.23 .16 .60]
.....
]
testy
is one hot encode and its values are like this
[
[1 0 0]
[0 0 1]
[0 0 1]
]
How can the predicted values like testy
or how to convert the predicted values to one hot encoded?
note: my model looks like this
# setup the model, add layers
model = Sequential()
model.add(Conv2D(32, kernel_size=(3,3), activation='relu', input_shape=input_shape))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(classes, activation='softmax'))
# compile model
model.compile(loss=keras.losses.categorical_crossentropy, optimizer=keras.optimizers.Adadelta(), metrics=['accuracy'])
# fit the model
model.fit(trainx, trainy, batch_size=batch_size, epochs=iterations, verbose=1, validation_data=(testx, testy))
Upvotes: 16
Views: 39518
Reputation: 159
The function predict_classes()
will be deprecated. Now in order to obtain the one hot encoding you will have only to do on softmax (model.predict(q) > 0.5).astype("int32")
.
Upvotes: 0
Reputation: 913
The values being returned are probabilities of each class. Those values can be useful because they indicates the model's level of confidence.
If you are only interested in the class with the highest probability:
For example[.19 .15 .64]
= 2
(because index 2 in the list is largest)
Let the model to it
Tensorflow models have a built in method that returns the index of the highest class probability.
model.predict_classes(testx, verbose=1)
Do it manually
argmax is a generic function to return the index of the highest value in a sequence.
import tensorflow as tf
# Create a session
sess = tf.InteractiveSession()
# Output Values
output = [[.57, .21, .21], [.19, .15, .64], [.23, .16, .60]]
# Index of top values
indexes = tf.argmax(output, axis=1)
print(indexes.eval()) # prints [0 2 2]
Upvotes: 15
Reputation: 3462
Keras returns a np.ndarray with the normalized likelihood of class labels.
So, if you want to transform this into a onehotencoding, you will need to find the indices of the maximum likelihood per row, this can be done by using np.argmax
along axis=1. Then, to transform this into a onehotencoding, the np.eye
functionality can be used. This will place a 1 at the indices specified. The only care to be taken, is to dimensionalize to appropriate row length.
a #taken from your snippet
Out[327]:
array([[ 0.57, 0.21, 0.21],
[ 0.19, 0.15, 0.64],
[ 0.23, 0.16, 0.6 ]])
b #onehotencoding for this array
Out[330]:
array([[1, 0, 0],
[0, 0, 1],
[0, 0, 1]])
n_values = 3; c = np.eye(n_values, dtype=int)[np.argmax(a, axis=1)]
c #Generated onehotencoding from the array of floats. Also works on non-square matrices
Out[332]:
array([[1, 0, 0],
[0, 0, 1],
[0, 0, 1]])
Upvotes: 6