Reputation: 742
Keras is unfamiliar to me. I'm attempting to put some programming into action.
The data shape is as follows:
Train shape X: (249951, 5, 52) y (249951,)
Test shape X: (263343, 5, 52) y (263343,) # Do not confuse with the distribution, it is juts toy example
My date contains twelve labels. The keras CNN architecture is as follows:
def get_compiled_model():
# Make a simple 2-layer densely-connected neural network.
inputs = keras.Input(shape=(260,))
x = keras.layers.Dense(256, activation="relu")(inputs)
x = keras.layers.Dense(256, activation="relu")(x)
outputs = keras.layers.Dense(12)(x) # 12 classes
model = keras.Model(inputs, outputs)
model.compile(
optimizer=keras.optimizers.Adam(),
loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=[keras.metrics.SparseCategoricalAccuracy()],
)
return model
Now, I feed 12 neuron to the output layer, as my data contains 12 classes. However, the following error message is displayed:
Use `tf.data.Iterator.get_next_as_optional()` instead.
2255/7811 [=======>......................] - ETA: 18s - loss: 0.1109 - sparse_categorical_accuracy: 0.99452021-04-19 16:32:33.591493: W tensorflow/core/framework/op_kernel.cc:1767] OP_REQUIRES failed at sparse_xent_op.cc:90 : Invalid argument: Received a label value of 17 which is outside the valid range of [0, 12). Label values: 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 17 17 17 17 17
Traceback (most recent call last):
File "C:\Users\Nafees Ahmed\AppData\Local\Programs\Python\Python38\lib\contextlib.py", line 131, in __exit__
self.gen.throw(type, value, traceback)
File "C:\Users\Nafees Ahmed\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 2804, in variable_creator_scope
yield
File "C:\Users\Nafees Ahmed\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1098, in fit
tmp_logs = train_function(iterator)
File "C:\Users\Nafees Ahmed\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\eager\def_function.py", line 780, in __call__
result = self._call(*args, **kwds)
File "C:\Users\Nafees Ahmed\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\eager\def_function.py", line 807, in _call
return self._stateless_fn(*args, **kwds) # pylint: disable=not-callable
File "C:\Users\Nafees Ahmed\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\eager\function.py", line 2829, in __call__
return graph_function._filtered_call(args, kwargs) # pylint: disable=protected-access
File "C:\Users\Nafees Ahmed\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\eager\function.py", line 1843, in _filtered_call
return self._call_flat(
File "C:\Users\Nafees Ahmed\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\eager\function.py", line 1923, in _call_flat
return self._build_call_outputs(self._inference_function.call(
File "C:\Users\Nafees Ahmed\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\eager\function.py", line 545, in call
outputs = execute.execute(
File "C:\Users\Nafees Ahmed\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\eager\execute.py", line 59, in quick_execute
tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
tensorflow.python.framework.errors_impl.InvalidArgumentError: Received a label value of 17 which is outside the valid range of [0, 12). Label values: 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 17 17 17 17 17
[[node sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits (defined at Users\Nafees Ahmed\AppData\Local\Programs\Python\Python38\lib\threading.py:932) ]] [Op:__inference_train_function_846]
Function call stack:
train_function
Main Error: tensorflow.python.framework.errors_impl.InvalidArgumentError: Received a label value of 17 which is outside the valid range of [0, 12). Label values: 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 17 17 17 17 17
Upvotes: 1
Views: 2375
Reputation: 343
If your label(y) is numeric, it will not work. You need to convert it to binary data, since it is multiclass binary problem. Maybe you can use below to do so.
y_train = np_utils.to_categorical(y_train, num_classes)
y_test = np_utils.to_categorical(y_test, num_classes)
num_classes
is 12, in your case.
However, 12 classes sound too many for me. Is this really multiclass, rather than multilabel?
Upvotes: 1