Restrict the field of prediction possible

Hi all,

I trained a Mnist model with DL4J.
When i use this model in inference mode :
INDArray prediction = myModel.output(myINDArrayImage);
that gives me a prediction in an INDArray, it works properly. The size of this INDArray is equal to number of output on my OutputLayer model (softmax activation).

Is there a possibility to ask the model to predict only on a certain number ? Or have I to change my model ?

thx for your responses ! :slight_smile:

As @eduardo has said in https://stackoverflow.com/questions/60928846/dl4j-is-there-a-way-to-restrict-the-prediction-of-a-model already, you should multiply your output with a mask.

A mask is an array that for every possible output class has either a 1 or a 0. So if you have 10 possible output classes and you want to keep only the results for class 0, 1, 3, 5 and 7 it looks like this: [1, 1, 0, 1, 0, 1, 0, 1, 0, 0].

Again as already said in the stackoverflow answer, you should then use argMax to get the index of the class with the highest probability of the remaining classes.

Thx for your response, but i don’t understand anyway :confused:
myu model use a softmax output validation so in my response i have for exemple :
[0,0,0,0,1,0,0,0,0,0]
I don’t understand how a mask can say to the model “I know the result is 3 or 7, give me a prediction on these value” because my model can return 4 ([0,0,0,0,1,0,0,0,0,0])
and i want a prediction only on 3 or 7 ([0,0,0,1,0,0,0,1,0,0])

if my model give me that response :
[0,0,0,0,1,0,0,0,0,0]
a mask with
[0,0,0,1,0,0,0,1,0,0]
will return me [0,0,0,0,0,0,0,0,0,0] no ?

Usually a model doesn’t return responses like that. It is called softmax because it isn’t an actual max, so you will not have all zero results.

But, if you absolutely want to do it some other way, you can retrain your full model, and add a bias input, i.e. for each input image you train, you add an additional input vector that uses a multi hot encoding to denote which classes the current training image could be. You will have to make sure that you aren’t just training with a one-hot encoding there, otherwise your model will learn to ignore the actual picture, and just use the answer you are giving it.

that i say, my model use softmax so I have only one 1 and 0 to other value.

when i make a prediction, my model look my input and give me the best solution for one of ten output (softmax), i want to tell him :
don’t look on ten solution but only 2 for exemple, I think it’s not possible…

i think I have to make 10 model : one for each number and call the concerned model for prediction that can respond me “true or false”
an other solution is to use an other validationfunction like sigmoide to get a probability like 80% / 20% but I haven’t managed to do it yet
Thanks anyway for taking the time to consider my problem

You don’t understand how softmax works. Unless your model is 100% sure that an input is a specific class (which basically never happens), you will never get exactly 0 or exactly 1.

Anyway, if you are fine with creating multiple models, you can also use something different from softmax as your final activation function. Sigmoid will work well enough, but you have to use a different loss function, one that doesn’t expect the output to be a probability distribution.
In that case you can imagine it like 11 models working together, first one model extracts all the necessary features and then 10 more simpler models use those features as input.

I think I know how softmax works…
my model predict the good answer 97% times

in this case, the good result is the first index, but he says me 1 in other value…

mask out the 1 and sum the result, it will still be more than 0. So selecting from the other outputs should still work for you.

this response is exceptional, in most of the time i get only one 1 and 0 to others values.

i just want to know if there is a way to say in a model who are trained with 10 output :
“Give me the best result between 4 output for exemple”

sorry for my english, I can’t explain what I want to do, the mask is not the solution I’m waiting for because I only have one possible solution with my prediction

ok i understand, i use a loss function who are too restrictive for my model. I have to change it. Thx !

If you are getting exactly 0 on those outputs (i.e. not just values that are rounded to 0 for the .toString() call, which prints at a limited precision), then you are getting huge inputs into your softmax which forces the values into underflow, and you should probably check your model.

Otherwise the function that defines the softmax can not return anything that is exactly 0:
image

I’ve tried it again with a very overfitting model on data that it has seen thousands of times, and even then I can not force softmax to return something that resembles your output. It is always something like this: [ 8.7626e-8, 5.6123e-7, 0.9999, ... 1.9215e-5, 3.3234e-5, 4.4047e-8]

I suggest you check your model as well.

witch loss function do you use ?

That was with NEGATIVELOGLIKELIHOOD(which is identical to MCXENT).

hum i use the same :confused:

Post your network configuration, and I might be able to tell you where things can go wrong.

ok, I use a relu rathen than a sigmoide before last FC layer. Now i’m able to put a mask on prediction because I don’t only have 1 or 0 results :slight_smile: thx!