Reputation: 261
I was trying to design a multi-class Classification NN.
My train_x dataset contains 23 examples each containing 37 features (dimension : 23*37)
train_y contains output for each example (dimension : 23*7) [ 7 Labels/Classes ]. I used one-hot encoding for each example's output.
len(words) is the number of features
This is my model design :
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(units=len(words), input_shape=[len(words)]),
tf.keras.layers.Dense(8, activation="relu"),
tf.keras.layers.Dense(8, activation="relu"),
tf.keras.layers.Dense(len(labels), activation="softmax")
])
For optimizer I used Adam Optimizer and for loss function I used Sparse Categorical Entropy.
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=['accuracy'])
model.fit(train_x, train_y, epochs=100)
I am getting the following traceback call:
Epoch 1/100
Traceback (most recent call last):
File "main.py", line 83, in <module>
model.fit(train_x, train_y, epochs=100, callbacks=[callbacks])
File "C:\Users\aaman\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\training.py", line 66, in _method_wrapper
return method(self, *args, **kwargs)
File "C:\Users\aaman\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\training.py", line 848, in fit
tmp_logs = train_function(iterator)
File "C:\Users\aaman\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\eager\def_function.py", line 580, in __call__
result = self._call(*args, **kwds)
File "C:\Users\aaman\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\eager\def_function.py", line 644, in _call
return self._stateless_fn(*args, **kwds)
File "C:\Users\aaman\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\eager\function.py", line 2420, in __call__
return graph_function._filtered_call(args, kwargs) # pylint: disable=protected-access
File "C:\Users\aaman\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\eager\function.py", line 1661, in _filtered_call
return self._call_flat(
File "C:\Users\aaman\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\eager\function.py", line 1745, in _call_flat
return self._build_call_outputs(self._inference_function.call(
File "C:\Users\aaman\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\eager\function.py", line 593, in call
outputs = execute.execute(
File "C:\Users\aaman\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: logits and labels must have the same first dimension, got logits shape [23,7] and labels shape [161]
[[node sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits (defined at main.py:83) ]] [Op:__inference_train_function_709]
Function call stack:
train_function
I have been searching in various sites for two days. But all of them flattens the input data for first layer. All of them use either grey scale images or RGB images as input. All of them requires the first layer to be flattened. But my input data is already flattened.
As much I am understanding this, I am getting the traceback call for the first layer. I may have misunderstood the concept of units and input_shape, thus defining them incorrectly.
Upvotes: 0
Views: 94
Reputation: 2744
Change sparse_categorical_crossentropy
to categorical_crossentropy
.
Upvotes: 1