0
down vote
favorite
So i have written a network which consists of the followings for multi-class classification : -y_labels transformed with to_categorical -last layer uses a sigmoid function with 3 neurons as my classes -model compile uses categorical_crossentropy as loss function So i used
model.predict_classes(x_test)
and then i used it as
classification_report(y_test,pred)
y_test has the form to_categorical And i am getting the following error :
ValueError: Mix type of y not allowed, got types set(['binary', 'multilabel-indicator'])
My question is how can i transform it back in order to use it as such?
The inverse of to_categorical is np.argmax or K.argmax.
If you're predicting 3 independent classes, make sure to use softmax output so the outputs sum to 1. With sigmoid output, the network could predict no class or all classes.
Cheers,
Ben
thanks :D
from numpy.random import randint
from numpy import argmax
from keras.utils.np_utils import to_categorical
k = 8
n = 20
x = randint(0, k, (n,))
print(x)
print(argmax(to_categorical(x, k)))
argmax(to_categorical(x, k), axis=1)
np.argmax( to_categorical(x,k) ,axis=1)
The inverse in R:
inverse_to_categorical <- function(mat)
{
apply(mat, 1, function(row) which(row==max(row))-1)
}
I know, it's closed, but for my problem the answer had to be slightly modified:
import numpy as np
import keras
two_examples = np.array([[0, 4, 3], [3, 4, 5]])
two_examples.shape # (2, 3)
two_examples_one_hot = keras.utils.to_categorical(two_examples)
two_examples_one_hot.shape # (2, 3, 6)
argmax(two_examples_one_hot, axis=2) # 2 means 3 (in fact, you should use "-1")
# array([[0, 4, 3],
# [3, 4, 5]])
Hi,
I am using ImageDataGenerator and flow_from_directtory. I set using 'categorical' labels.
When I print train_generator_dict[_att_group].class_indices, I obtain something like this:
'1': 0, '16': 1, '22': 2, '3': 3, '4': 4, '40': 5, '41': 6, '419': 7, '5': 8
and its okey because the name of my folders classes are the numbers '0', '16', '22',... and '0', '1', '2', '3',... are the assigned labels.
But when I obtain they y values, they are in categorical values (as I had selected), but I do not know which categorical value correspond with the target that prints class_indices.
I hope I have explained correctly.
Cheers and Thanks.
@gomezabeatriz your description is quite hard to understand.
It feels like you face a reverse dictionary problem, which is not related to keras, but is a more general python question.
Also, it might make sense for you, but keras disagrees: keras.utils.to_categorical
will create a class for every integer from 0
to max_int_in_the_data
. So, if you give it an array [3, 5, 7]
you will end up with 8-dimensional vectors -- be careful!
Hi,
I have created a Multi class classification model with MLP in KERAS. Now the model output is in categorical format. How can I change the format to real?
for more clarification the real out put should be 0, 1, 2, 3, 4, 5, 6 but the output format is as following
[1. 0. 0. 0. 0. 0. 0.]
Most helpful comment
argmax(to_categorical(x, k), axis=1)