[英]ValueError: Shapes (None, 1) and (None, 3) are incompatible
I have a 3 dimensional dataset of audio files where X.shape
is (329,20,85)
.我有一个音频文件的 3 维数据集,其中
X.shape
是(329,20,85)
。 I want to have a simpl bare-bones model running, so please don't nitpick and address only the issue at hand.我想要一个简单的准系统 model 运行,所以请不要挑剔并只解决手头的问题。 Here is the code:
这是代码:
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.LSTM(32, return_sequences=True, stateful=False, input_shape = (20,85,1)))
model.add(tf.keras.layers.LSTM(20))
model.add(tf.keras.layers.Dense(nb_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=["accuracy"])
model.summary()
print("Train...")
model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=50, validation_data=(X_test, y_test))
But then I had the error mentioned in the title: ValueError: Shapes (None, 1) and (None, 3) are incompatible
但后来我遇到了标题中提到的错误:
ValueError: Shapes (None, 1) and (None, 3) are incompatible
Here is the model.summary()
这是
model.summary()
Model: "sequential_13"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
lstm_21 (LSTM) (None, 20, 32) 15104
_________________________________________________________________
lstm_22 (LSTM) (None, 20) 4240
_________________________________________________________________
dense_8 (Dense) (None, 3) 63
=================================================================
Total params: 19,407
Trainable params: 19,407
Non-trainable params: 0
_________________________________________________________________
Train...
For this, I followed this post and updated Tensorflow to the latest version, but the issue persists.为此,我关注了这篇文章并将 Tensorflow 更新到最新版本,但问题仍然存在。 This post is completely unrelated and highly unreliable.
这篇文章完全不相关,而且非常不可靠。 This post although a bit relatable is unanswered for a while now.
这篇文章虽然有点相关,但现在暂时没有答案。
Update 1.0:更新 1.0:
I strongly think the problem has something to do with the final Dense
layer where I pass nb_classes as 3, since I am classifying for 3 categories in y
.我强烈认为这个问题与我将 nb_classes 传递为 3 的最终
Dense
层有关,因为我在y
中对 3 个类别进行分类。
So I changed the Dense
layer's nb_classes
to 1, which ran the model and gives me this output, which I am positive is wrong.所以我将
Dense
层的nb_classes
为 1,它运行 model 并给了我这个 output,我肯定这是错误的。
Train...
9/9 [==============================] - 2s 177ms/step - loss: 0.0000e+00 - accuracy: 0.1520 - val_loss: 0.0000e+00 - val_accuracy: 0.3418
<tensorflow.python.keras.callbacks.History at 0x7f50f1dcebe0>
Update 2.0:更新 2.0:
I one hot encoded the y
s and resolved the shape issue.我对
y
进行了热编码并解决了形状问题。 But now the above output with <tensorflow.python.keras.callbacks.History at 0x7f50f1dcebe0>
persists.但是现在上面的 output 和
<tensorflow.python.keras.callbacks.History at 0x7f50f1dcebe0>
仍然存在。 Any help with this?有什么帮助吗? Or should I post a new question for this?
或者我应该为此发布一个新问题? Thanks for all the help.
感谢所有的帮助。
How should I proceed, or what should I be changing?我应该如何进行,或者我应该改变什么?
The first problem is with the LSTM input_shape.第一个问题是 LSTM input_shape。
input_shape = (20,85,1)
. input_shape = (20,85,1)
。
From the doc: https://keras.io/layers/recurrent/来自文档: https://keras.io/layers/recurrent/
LSTM layer expects 3D tensor with shape (batch_size, timesteps, input_dim). LSTM 层需要3D 具有形状(batch_size、timesteps、input_dim)的张量。
model.add(tf.keras.layers.Dense(nb_classes, activation='softmax'))
- this suggets you're doing a multi-class classification. model.add(tf.keras.layers.Dense(nb_classes, activation='softmax'))
- 这表明您正在进行多类分类。
So, you need your y_train
and y_test
have to be one-hot-encoded.所以,你需要你的
y_train
和y_test
必须是单热编码的。 That means they must have dimension (number_of_samples, 3)
, where 3
denotes number of classes.这意味着它们必须具有维度
(number_of_samples, 3)
,其中3
表示类数。
You need to apply tensorflow.keras.utils.to_categorical
to them.您需要将
tensorflow.keras.utils.to_categorical
应用于它们。
y_train = to_categorical(y_train, 3)
y_test = to_categorical(y_test, 3)
ref: https://www.tensorflow.org/api_docs/python/tf/keras/utils/to_categorical参考: https://www.tensorflow.org/api_docs/python/tf/keras/utils/to_categorical
tf.keras.callbacks.History()
- this callback is automatically applied to every Keras model. tf.keras.callbacks.History()
- 此回调自动应用于每个 Keras model。 The History object gets returned by the fit method of models.历史 object 由模型的 fit 方法返回。
ref: https://www.tensorflow.org/api_docs/python/tf/keras/callbacks/History参考: https://www.tensorflow.org/api_docs/python/tf/keras/callbacks/History
Check if the last Dense Layer(output) has same number of classes as the number of target classes in the training dataset.检查最后一个密集层(输出)的类数是否与训练数据集中的目标类数相同。 I made similar mistake while training the dataset and correcting it helped me.
我在训练数据集并纠正它时犯了类似的错误,这对我有帮助。
Another thing to check, is whether your labels are one-hot-coded, or integers only.要检查的另一件事是,您的标签是单热编码还是仅整数。 See this post: https://www.kaggle.com/general/197993
看到这个帖子: https://www.kaggle.com/general/197993
The error arose because 'categorical_crossentropy' works on one-hot encoded target, while 'sparse_categorical_crossentropy' works on integer target.
出现错误是因为“categorical_crossentropy”适用于单热编码目标,而“sparse_categorical_crossentropy”适用于 integer 目标。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.