Had a doubt about multi output incase of multi label

Can I feed in one X input to the network but two y inputs y1 and y2? How would the classifier know which label to predict?

1 Like

Hi @Samhita_V ,

In the training process, your model is trained with X, and the true label y. For example, let the model be a cat classifier; the input X would be images of cat, and the y would be the true label for each of the images. So the model will learn what a cat looks like after training. When an unseen image is given to the model, it would output a value to indicate if that image is a cat or not cat. If your model is good with high accuracy, the prediction of getting the correct answer would be high. Otherwise, the model would need to be adjusted and train further to improve the accuracy.
If the above is not the answer to your question, please give more information, an example would be good to illustrate your point.

1 Like

I meant that each input variable would have two label outputs y1 and y2. (eg y1 can take values 0 or 1) and y2 can take values (0 or 1 or 2 or 3).
If I want to have one dense layer d1 that predicts y1 and another dense layer d2 that predicts y2 how would I specify which output label I want each specific layer(d1,d2) to predict? Would input parameters to custom loss remain the same in this case? (if I wish to have a custom loss for each of the two outputs) Also, how would model.fit() look?

1 Like

Hi @Samhita_V ,

Here is a very good article explaining multi-label classification and how it works. There is also code sample of what the model looks like, the cost function and evaluation procedure.

1 Like

Hi @Samhita_V :slight_smile:

This again looks Tensorflow advanced technique specialisation query, so I will try to put some of my inputs on your query.

Whenever you have a multi label classification, make the input part of your model’s output.

To do this, simply build your model with the functional API, and then add the input tensor to the list of outputs:

For example, input = Input(input_shape)

build the rest of your model with the standard functional API here
this example model was taken from the Keras docs
x = Dense(100, activation=‘relu’)(input)
x = Dense(100, activation=‘relu’)(x)
x = Dense(100, activation=‘relu’)(x)
output = Dense(10, activation=‘softmax’)(x)

model = Model(inputs=[input], outputs=[output, input])

then, make y_true a combination of your input data and the original ground truth.

This can be handled by concatenate your outputs together which lets you pass your custom loss into model.compile(). Then deal with the outputs in the custom loss function.

I think you are currently doing course 2 of the specialisation, when you go further you find assignment explaining this part of multi-label input as well as gradient tape function.

Let us know if you have still any doubts.

Another update, I am updating your header to the specialisation your query is specified to, so mentors of the specialisation can also put forth their thoughts on your query.

Regards
DP

1 Like

Is this a clarification of the thread title, which says … multi-input ?
Did you really mean to write multi-output, which is what I believe is addressed by both @Kic and the article they linked.

If so, maybe you would like to edit the thread title (if you can) for clarity. It will help others searching for help understand whether or not this thread might be of interest. Cheers

Or maybe “multi label classification” instead of “multi input”.

1 Like

Hello ai_curious,

I think she was aiming her heading more towards parameters being used as input shape and how that can be used to give multiple outputs, probably that’s she chose multi input. In the custom loss where the true labels and pred values are used to create a multiple output where in how custom loss could be used in case multiple input (y_true, y_pred)

Regards
DP

I agree ‘multi label classification’ is one concept the OP might have been asking about. But Keras Functional API also supports multiple outputs of different types, including mixed classification and regression outputs. See for example this reference document

Specifically the section

Manipulate complex graph topologies

Models with multiple inputs and outputs

The example there has one output that is a regression for predicting trouble ticket priority and one that is a classification for predicting department to which the ticket should be assigned.

I can’t discern from the thread above which is the desired functionality.

1 Like

From the problem description, it seems multi output related query, you could check out python - Multiple outputs in Keras - Stack Overflow

2 Likes

Thank you so much for all the responses! I will try it out and get back if any issues. I have changed the heading now, is it more appropriate?

2 Likes

Hello @Samhita_V,

if your question or query was related to what you felt was multi-label then surely change, but if your query was related to something else, feel free to ask and discuss. The suggestion by mentor and leader was only because they didn’t wanted you to be confused between input and output.

Hope you are understanding!!!

Regards
DP

1 Like

You could add value to the community if you share your learning experience with us. What did your model end up looking like? What activation function(s) did you use? How did training go, and was the trained model able to make useful predictions? What worked well or not so well? This will allow future readers of the thread to not only follow the discussion to this point, but benefit from your solution. Thanks in advance

1 Like

I wasn’t able to completely try it out as I have been struggling with another error for a few days now. I have multiple outputs but am not able to access any output with index greater than 1 ( that is by doing y_pred[2] or y_pred[3] etc) . I get this error :

slice index 2 of dimension 0 out of bounds

if i try to access y_pred[2] in my custom loss function

1 Like

how would you correctly split y_true in custom loss function? Is the custom loss function calculated input by input or by batch or how does it work? Moreover if i try to add to y_true, it tells me that I should have the same number of elements in y as in x

1 Like

Hello @Samhita_V

If the error you are encountering is related an assignment of the selected course here, then please share a screenshot of the error you are getting. Please make sure your screenshot don’t include any codes from the grader cell and only error message it is showing.

Please make sure mention which specialisation, course, week and assignment name when posting a query related to error encountered in assignment for better approach to address your issue.

Regards
DP

1 Like

My understanding is that have two outputs from your model, and y_pred is the output of your model. Can you examine the shape of y_true and y_pred inside your loss function? I don’t have a running TF /Keras environment to test myself at the moment, but my intuition is you have a vector of predictions in y_pred[0] and a vector of different predictions in y_pred[1], each of those vectors is the length of the ground truth inputs in y_true.

Maybe I’m missing something

1 Like