[英]ValueError: Input 0 of layer "sequential_8" is incompatible with the layer - deep learning model
I am attempting to setup my first deep learning sequential model with a small test dataset.我正在尝试使用小型测试数据集设置我的第一个深度学习顺序 model。
Unfortunately, I get the following error message when I call model.fit():不幸的是,当我调用 model.fit() 时收到以下错误消息:
ValueError: Input 0 of layer "sequential_8" is incompatible with the layer: expected shape=(None, 160, 4000), found shape=(32, 4000)
My model is as follows我的model如下
num_of_classes = 2
input_shape = (1,4000)
y_train_cat = keras.utils.to_categorical(y_train, num_of_classes)
y_test_cat = keras.utils.to_categorical(y_test, num_of_classes)
model = Sequential()
model.add(Conv1D(filters=10, kernel_size=5, input_shape=(160, 4000)))
model.add(MaxPool1D(pool_size=5))
model.add(Flatten())
model.add(Dense(1000, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
The data is of the following dimensions数据有以下维度
x_train.shape is (160, 4000)
y_train_cat is (160, 2)
There are two classes.有两个班。
Thank you for reading this far and your help in advance感谢您阅读本文并提前提供帮助
When you give a layer the shape, is supposed to be the same of a single sample... change当您为图层赋予形状时,应该与单个样本相同...更改
model.add(Conv1D(filters=10, kernel_size=5, input_shape=(160, 4000)))
to至
model.add(Conv1D(filters=10, kernel_size=5, input_shape=(4000,1)))
and it should work fine它应该可以正常工作
Edit:编辑:
You probably also need to reshape your input to add a dimension:您可能还需要重塑输入以添加维度:
x_train = np.expand_dims(x_train, 2)
Explanation:解释:
consider a single element, a 1D convolution "slides" a 1D filter over your 1D element, however you can assume to have multiple channels, thus the leading "1" in the shape of the input考虑单个元素,一维卷积在您的一维元素上“滑动”一维过滤器,但是您可以假设有多个通道,因此输入形状中的前导“1”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.