ValueError:形状(无,1)和(无,3)不兼容 [英] ValueError: Shapes (None, 1) and (None, 3) are incompatible

查看:194
本文介绍了ValueError:形状(无,1)和(无,3)不兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个音频文件的3维数据集,其中X.shape(329,20,85).我想运行一个简单的准系统模型,所以请不要挑剔并且只解决眼前的问题.这是代码:

I have a 3 dimensional dataset of audio files where X.shape is (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. 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))

但是随后出现标题中提到的错误: ValueError: Shapes (None, 1) and (None, 3) are incompatible

But then I had the error mentioned in the title: ValueError: Shapes (None, 1) and (None, 3) are incompatible

这是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. This post is completely unrelated and highly unreliable.This post although a bit relatable is unanswered for a while now.

更新1.0:

我强烈认为问题与最后的Dense层有关,在该层中我将nb_classes传递为3,因为我要在y中对3个类别进行分类.

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.

因此,我将Dense层的nb_classes更改为1,从而运行了模型并给出了此输出,我肯定是错误的.

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.

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>

更新2.0:

我热编码了y并解决了形状问题.但是现在,上面带有<tensorflow.python.keras.callbacks.History at 0x7f50f1dcebe0>的输出仍然存在.有什么帮助吗?还是我应该为此发布一个新问题?感谢您的所有帮助.

I one hot encoded the ys and resolved the shape issue. But now the above output with <tensorflow.python.keras.callbacks.History at 0x7f50f1dcebe0> persists. 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?

推荐答案

第一个问题是LSTM input_shape. input_shape = (20,85,1).

The first problem is with the LSTM input_shape. input_shape = (20,85,1).

从文档中: https://keras.io/layers/recurrent/

LSTM层需要 3D张量的形状(batch_size,时间步长,input_dim).

model.add(tf.keras.layers.Dense(nb_classes, activation='softmax'))-这表明您正在执行多类分类.

model.add(tf.keras.layers.Dense(nb_classes, activation='softmax')) - this suggets you're doing a multi-class classification.

因此,您需要将y_trainy_test进行一次热编码.这意味着它们必须具有尺寸(number_of_samples, 3),其中3表示类数.

So, you need your y_train and y_test have to be one-hot-encoded. That means they must have dimension (number_of_samples, 3), where 3 denotes number of classes.

您需要对它们应用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

tf.keras.callbacks.History()-此回调将自动应用于每个Keras模型.历史对象通过模型的fit方法返回.

tf.keras.callbacks.History() - this callback is automatically applied to every Keras model. The History object gets returned by the fit method of models.

ref: https://www.tensorflow.org/api_docs/python/tf/keras/callbacks/History

这篇关于ValueError:形状(无,1)和(无,3)不兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆