GoogleCloudPlatform / cloudml-samples

Cloud ML Engine repo. Please visit the new Vertex AI samples repo at https://github.com/GoogleCloudPlatform/vertex-ai-samples
https://cloud.google.com/ai-platform/docs/
Apache License 2.0
1.52k stars 857 forks source link

How to convert this code for multi-label classification? #278

Closed jennings1716 closed 5 years ago

jennings1716 commented 5 years ago

Is that possible to convert this model into a multi-label classification?I want to different classes to report their probability(Eg: A class - 80.99, B class - 93.99, C class - 87.99)

jennings1716 commented 5 years ago

Could someone help ??

gogasca commented 5 years ago

Hi @jennings1716 Which model are you referring? For reporting the different probabilities you will need a softmax function. For multi-label classification take a look at Iris dataset.

https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/learn/iris.py https://github.com/tensorflow/models/blob/master/samples/outreach/blogs/blog_custom_estimators.py

jennings1716 commented 5 years ago

Hi @gogasca I want to change the classification model for flowers dataset to multi-label classification.I have changed the final softmax layer to sigmoid layer.Yet it seems to change labels to onehotencoding.I have no idea where to change.

puneith commented 5 years ago

@jennings1716 see this line https://github.com/GoogleCloudPlatform/cloudml-samples/blob/master/flowers/trainer/model.py#L269 which computes argmax. You need to remove argmax so that max probability class label is not reported, and probabilities are reported instead.

jennings1716 commented 5 years ago

[7.799568768618315e-10, 5.413010040200561e-10, 1.6661587665112165e-08, 4.89881024634542e-09, 1.411282681829107e-07, 9.827594737998879e-08, 1.2115825143155234e-07, 1.8789449995892937e-08, 6.705686672603406e-08, 1.0478414225190136e-07, 1.050282662617974e-05, 1.1320223336497293e-07, 6.65690365053706e-08, 5.1431349845643126e-08, 4.839898338104831e-06, 0.16670562326908112, 1.0286857587971099e-08, 4.848571961701964e-07, 3.940714421446501e-09, 1.4307776154964813e-06, 0.8332762122154236, 5.512023903975205e-08, 5.235987998730707e-08, 1.5232886951821456e-08, 5.756805698808876e-09] I got the same values @puneith Could you tell me what else should I change in code to get probabilities for each class

ksalama commented 5 years ago

@jennings1716 - For multi-label classification, you need to replace the softmax with a sigmoid activation. With sigmoid, each output will have a value between 0 and 1, unlike softmax that makes the sum of all the outputs equals to 1.

jennings1716 commented 5 years ago

Got..the required result...Just changed from tf.nn.softmax to tf.nn.sigmoid and removed the argmax as @puneith suggested.Thanks.