合并后预期的Keras形状不匹配 [英] Mismatch in expected Keras shapes after pooling

查看:117
本文介绍了合并后预期的Keras形状不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Keras中构建一些简单的模型,以提高我的深度学习知识,并遇到一些我不太了解如何调试的问题.

I'm building a few simple models in Keras to improve my knowledge of deep learning, and encountering some issues I don't quite understand how to debug.

我想使用1D CNN对某些时间序列数据执行回归.我的输入特征张量的形状为N x T x D,其中N是数据点的数量,T是序列的数量,而D是维的数量.我的目标张量的形状为N x T x 1(1,因为我试图输出标量值).

I want to use a 1D CNN to perform regression on some time-series data. My input feature tensor is of shape N x T x D, where N is the number of data points, T is the number of sequences, and D is the number of dimensions. My target tensor is of shape N x T x 1 (1 because I am trying to output a scalar value).

我已经建立了这样的模型架构:

I've set up my model architecture like this:

feature_tensor.shape
# (75584, 40, 38)
target_tensor.shape
# (75584, 40, 1)

inputs = Input(shape=(SEQUENCE_LENGTH,DIMENSIONS))
conv1 = Conv1D(filters=64, kernel_size=3, activation='relu')
x = conv1(inputs)
x = MaxPooling1D(pool_size=2)(x)
x = Flatten()(x)
x = Dense(100, activation='relu')(x)
predictions = Dense(1, activation="linear")(x)
model = Model(inputs, predictions)
opt = Adam(lr=1e-5, decay=1e-4 / 200)
model.compile(loss="mean_absolute_error", optimizer=opt)

但是,当我尝试训练模型时,会得到以下输出:

When I attempt to train my model, however, I get the following output:

r = model.fit(cleaned_tensor, target_tensor, epochs=100, batch_size=2058)

ValueError:检查目标时出错:预期density_164为2 尺寸,但数组的形状为(75584,40,1).

ValueError: Error when checking target: expected dense_164 to have 2 dimensions, but got array with shape (75584, 40, 1).

熟悉前两个数字:75584是样本数,40是序列长度.

The first two numbers are familiar: 75584 is the # of samples, 40 is the sequence length.

调试模型摘要对象时,看到Flatten层的预期输出应为1216:

When I debug my model summary object, I see that the expected output from the Flatten layer should be 1216:

但是,我和我的同事长时间盯着代码,无法理解为什么(75584, 40, 1)的形状在到达密集层时是通过体系结构到达的.

However, my colleague and I stared at the code for a long time and could not understand why the shape of (75584, 40, 1) was being arrived at via the architecture when it reached the dense layer.

有人可以指出我做错了什么方向吗?

Could someone point me in the direction of what I am doing wrong?

推荐答案

尝试将目标变量重塑为N x T,看来您的最终密集层应为40而不是1(我认为).

Try reshaping your target variable to N x T, and it looks like your final dense layer should be 40 rather than 1 (i think).

这篇关于合并后预期的Keras形状不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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