数据集形状不匹配Conv1D输入层 [英] Dataset shape mismatch Conv1D input layer

查看:83
本文介绍了数据集形状不匹配Conv1D输入层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试将Conv1D层设置为keras中的输入层.

Trying to set up a Conv1D layer to be the input layer in keras.

数据集是1000个时间步长,每个时间步长都有1个特征.

The dataset is 1000 timesteps, and each timestep has 1 feature.

在阅读一堆答案后,我将数据集重塑为以下格式(n_samples,时间步长,特征),这与我的情况相对应:

After reading a bunch of answers I reshaped my dataset to be in the following format of (n_samples, timesteps, features), which corresponds to the following in my case:

train_data   = (78968, 1000, 1)
test_data    = (19742, 1000, 1)
train_target = (78968,)
test_target  = (19742,)

稍后我会使用以下几行代码来创建和编译代码

I later create and compile the code using the following lines

model = Sequential()
model.add(Conv1D(64, (4), input_shape = (1000,1) ))

model.add(MaxPooling1D(pool_size=2))
model.add(Dense(1))
optimizer = opt = Adam(decay = 1.000-0.999)
model.compile(optimizer=optimizer,
              loss='mean_squared_error',
              metrics=['mean_absolute_error','mean_squared_error'])

然后我尝试拟合,请注意,train_target和test_target是熊猫系列,因此我正在调用DataFrame.values转换为numpy数组,我怀疑那里可能存在问题吗?

Then I try to fit, note, train_target and test_target are pandas series so i'm calling DataFrame.values to convert to numpy array, i suspect there might be an issue there?

training = model.fit(train_data, 
                 train_target.values,
                 validation_data=(test_data, test_target.values),
                 epochs=epochs,
                 verbose=1)

模型可以编译,但是在尝试拟合时出现错误

The model compiles but I get an error when I try to fit

Error when checking target: expected dense_4 to have 3 dimensions, 
but got array with shape (78968, 1)

我已经尝试了各种重塑数据的组合,但都无法正常工作.

I've tried every combination of reshaping the data and can't get this to work.

我以前仅在不同的项目中使用了密集层的keras,在该项目中指定了input_dimension而不是input_shape,所以我不确定在这里做错了什么.我已经阅读了几乎所有有关数据形状问题的堆栈溢出问题,而且恐怕问题还在其他地方,我们将不胜感激,谢谢.

I've used keras with dense layers only before for a different project where the input_dimension was specificied instead of the input_shape, so I'm not sure what I'm doing wrong here. I've read almost every stack overflow question about data shape issues and I'm afraid the problem is elsewhere, any help is appreciated, thank you.

推荐答案

model.add(MaxPooling1D(pool_size=2))行下,添加一行model.add(Flatten()),您的问题将得到解决.展平功能将帮助您将数据转换为正确的形状,请访问此网站了解更多信息

Under the line model.add(MaxPooling1D(pool_size=2)), add one line model.add(Flatten()), your problem will be solved. Flatten function will help you convert your data into correct shape, please see this site for more information https://www.tensorflow.org/api_docs/python/tf/keras/layers/Flatten

这篇关于数据集形状不匹配Conv1D输入层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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