合并后预期的Keras形状不匹配

2024-03-29 11:10:37 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我想用一维CNN对一些时间序列数据进行回归。我的输入特征张量的形状是N x T x D,其中N是数据点的数量,T是序列的数量,D是维度的数量。我的目标张量的形状是N x T x 1(1,因为我试图输出一个标量值)。你知道吗

我将我的模型架构设置为:

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)

然而,当我尝试训练我的模型时,我得到以下输出:

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

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

前两个数字很熟悉:75584是样本数,40是序列长度。你知道吗

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

enter image description here

然而,我和我的同事盯着代码看了很长一段时间,不明白为什么(75584, 40, 1)的形状是通过体系结构到达稠密层的。你知道吗

有人能指出我做错了什么吗?你知道吗


Tags: 数据模型targetsize数量model序列activation