ValueError:输入 0 与层 conv1d_1 不兼容:预期 ndim=3,发现 ndim=4 [英] ValueError: Input 0 is incompatible with layer conv1d_1: expected ndim=3, found ndim=4

查看:33
本文介绍了ValueError:输入 0 与层 conv1d_1 不兼容:预期 ndim=3,发现 ndim=4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Keras 提供的 conv1d 层为序列数据构建预测模型.我就是这样做的

I am building a prediction model for the sequence data using conv1d layer provided by Keras. This is how I did

model= Sequential()
model.add(Conv1D(60,32, strides=1, activation='relu',padding='causal',input_shape=(None,64,1)))
model.add(Conv1D(80,10, strides=1, activation='relu',padding='causal'))
model.add(Dropout(0.25))
model.add(Conv1D(100,5, strides=1, activation='relu',padding='causal'))
model.add(MaxPooling1D(1))
model.add(Dropout(0.25))
model.add(Dense(300,activation='relu'))
model.add(Dense(1,activation='relu'))
print(model.summary())

但是调试信息有

Traceback (most recent call last):
File "processing_2a_1.py", line 96, in <module>
model.add(Conv1D(60,32, strides=1, activation='relu',padding='causal',input_shape=(None,64,1)))
File "build/bdist.linux-x86_64/egg/keras/models.py", line 442, in add
File "build/bdist.linux-x86_64/egg/keras/engine/topology.py", line 558, in __call__
File "build/bdist.linux-x86_64/egg/keras/engine/topology.py", line 457, in assert_input_compatibility
ValueError: Input 0 is incompatible with layer conv1d_1: expected ndim=3, found ndim=4

训练数据和验证数据形状如下

The training data and validation data shape are as follows

('X_train shape ', (1496000, 64, 1))
('Y_train shape ', (1496000, 1))
('X_val shape ', (374000, 64, 1))
('Y_val shape ', (374000, 1))

我认为第一层中的 input_shape 设置不正确.如何设置?

I think the input_shape in the first layer was not setup right. How to set it up?

更新:使用input_shape=(64,1)后,我收到以下错误信息,即使模型摘要运行完毕

Update: After using input_shape=(64,1), I got the following error message, even though the model summary runs through

________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
conv1d_1 (Conv1D)            (None, 64, 60)            1980
_________________________________________________________________
conv1d_2 (Conv1D)            (None, 64, 80)            48080
_________________________________________________________________
dropout_1 (Dropout)          (None, 64, 80)            0
_________________________________________________________________
conv1d_3 (Conv1D)            (None, 64, 100)           40100
_________________________________________________________________
max_pooling1d_1 (MaxPooling1 (None, 64, 100)           0
_________________________________________________________________
dropout_2 (Dropout)          (None, 64, 100)           0
_________________________________________________________________
dense_1 (Dense)              (None, 64, 300)           30300
_________________________________________________________________
dense_2 (Dense)              (None, 64, 1)             301
=================================================================
Total params: 120,761
Trainable params: 120,761
Non-trainable params: 0
_________________________________________________________________
None
Traceback (most recent call last):
  File "processing_2a_1.py", line 125, in <module>
    history=model.fit(X_train, Y_train, batch_size=batch_size, validation_data=(X_val,Y_val), epochs=nr_of_epochs,verbose=2)
  File "build/bdist.linux-x86_64/egg/keras/models.py", line 871, in fit
  File "build/bdist.linux-x86_64/egg/keras/engine/training.py", line 1524, in fit
  File "build/bdist.linux-x86_64/egg/keras/engine/training.py", line 1382, in _standardize_user_data
  File "build/bdist.linux-x86_64/egg/keras/engine/training.py", line 132, in _standardize_input_data
ValueError: Error when checking target: expected dense_2 to have 3 dimensions, but got array with shape (1496000, 1)

推荐答案

您应该将 input_shape 更改为

input_shape=(64,1)

... 或使用 batch_input_shape:

batch_input_shape=(None, 64, 1)

本次讨论详细解释了两者在keras中的区别.

This discussion explains the difference between the two in keras in detail.

这篇关于ValueError:输入 0 与层 conv1d_1 不兼容:预期 ndim=3,发现 ndim=4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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