mg nt
mg nt

Reputation: 191

Label value is outside the valid range

X = tf.placeholder(shape=(1, 5, 7), name='inputs', dtype=tf.float32)
X_flat = tf.layers.flatten(X)
y = tf.placeholder(shape=(1), name='outputs', dtype=tf.int32)
hidden1 = tf.layers.dense(X_flat, 150, kernel_initializer=he_init)
hidden2 = tf.layers.dense(hidden1, 50, kernel_initializer=he_init)
logits = tf.layers.dense(hidden2, 1, kernel_initializer=he_init)
with tf.name_scope("loss"):
    xentropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=y)
    loss = tf.reduce_mean(xentropy, name="loss")

So I get the following error:

tensorflow.python.framework.errors_impl.InvalidArgumentError: Received a label value of 1 which is outside the valid range of [0, 1).  Label values: 1

My labels have integers that range from 0 to 4. I'm curious as to why this is not working. When I worked with MNIST in a sample code, I don't think the y training set was in the range of 0 to 1, but apparently that is what is going on here.

How do I make the cross entropy function work? Is there any sort of normalization which will make the code work?

Also how come MNIST can use integers but this one cannot for the labels?

Upvotes: 1

Views: 2514

Answers (1)

Alex Ding
Alex Ding

Reputation: 158

Edit

Just to be sure, this is what I changed into.

X = tf.placeholder(shape=(1, 5, 7), name='inputs', dtype=tf.float32)
X_flat = tf.layers.flatten(X)
y = tf.placeholder(shape=(1), name='outputs', dtype=tf.int32)
hidden1 = tf.layers.dense(X_flat, 150, kernel_initializer=he_init)
hidden2 = tf.layers.dense(hidden1, 50, kernel_initializer=he_init)
logits = tf.layers.dense(hidden2, 5, kernel_initializer=he_init)
with tf.name_scope("loss"):
    xentropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=y)
    loss = tf.reduce_mean(xentropy, name="loss")

This runs without problem on my end.

Original

Okay. So if your y variable has range (0,4), then your logit needs to have the shape (batch_size, 5) (in your case, (1,5)), since each value is your model's confidence on a particular label.

This:

logits = tf.layers.dense(hidden2, 1, kernel_initializer=he_init)

Needs to be this:

logits = tf.layers.dense(hidden2, 5, kernel_initializer=he_init)

To do even better, you should probably define these variables.

num_classes = 5
# ...
logits = tf.layers.dense(hidden2, num_classes, kernel_initializer=he_init)
# ...

Upvotes: 1

Related Questions