score:70

Accepted answer

Range [0, 1) means every number between 0 and 1, excluding 1. So 1 is not a value in the range [0, 1).

I am not 100% sure, but the issue could be due to your choice of loss function. For a binary classification, binary_crossentropy should be a better choice.

score:0

Another potential answer to this question is regarding the workspace. If it's not a logic/sparseness/entropy error as other answers suggest, keep reading:

If you created a workspace to hold the data as the model trained, the old workspace data can cause this error if you re-train the data with new samples, especially with a different number of folders and are using the folders as the labels for classification.

Example:

I trained my original set on:

Original Sample Set

and when I tried to retrain on the new Sample Set:

New Sample Set

I received the error: Received a label value of 3 which is outside the valid range of [0, 3)

This is likely because the old sample set's cached values of 4 folders versus the new sample set's 3 folders caused some kind of issue. All I know for sure is once I cleared the old information out of my workspace, and ran it again, it ran to completion. This was an isolated change after multiple failures, so I am certain it is what solved the issue.

Disclaimer: I am using C# and ML.NET, but it is still utilizing TensorFlow, which is where both of our errors were produced, so it should absolutely apply to the question.

score:0

For me issue was that the number of class passed to model was less than the actual number of class in the data. Hence model predicted -1 for most case and thus giving error as out of range.

score:1

I had this problem when I had labels of type "float", cast them it "int" and the problem was solved...

score:2

Cray and Shaili's answer was correct! I had a range of outcomes from 1 to 6, and the line:

tf.keras.layers.Dense(6, activation = 'softmax') 

Produced that error message, saying that things were outside of the range [0,6). I had thought that it was a labels problem (were all values present in both the training and validation label sets?), and was flogging them.

)

score:2

the error is in range [0,4) ,you can just add one to the number of classes(lables) . for example change this :

layers.Dense(4)

to :

layers.Dense(5)

**same for [0,1)

score:21

Peculiarities of sparse categorical crossentropy

The loss function sparse_categorical_crossentropy interprets the final layer in the context of classifiers as a set of probabilities for each possible class, and the output value as the number of the class. (The Tensorflow/Keras documentation goes into a bit more detail.) So x neurons in output layer are compared against output values in the range from 0 to x-1; and having just one neuron in the output layer is an 'unary' classifier that doesn't make sense.

If it's a classification task where you want to have output data in the form from 0 to x-1, then you can keep sparse categorical crossentropy, but you need to set the number of neurons in the output layer to the number of classes you have. Alternatively, you might encode the output in a one-hot vector and use categorical crossentropy loss function instead of sparse categorical crossentropy.

If it's not a classification task and you want to predict arbitrary real-valued numbers as in a regression, then categorical crossentropy is not a suitable loss function at all.

score:27

In the last Dense layer you used model.add(Dense(1, activation='softmax')). Here 1 restricts its value from [0, 1) change its shape to the maximum output label. For eg your output is from label [0,7) then use model.add(Dense(7, activation='softmax'))