【问题标题】:ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 223461, 5), found shape=(None, 5)ValueError:“顺序”层的输入 0 与该层不兼容:预期形状 =(无,223461,5),找到形状 =(无,5)
【发布时间】:2022-06-18 01:20:15
【问题描述】:

我使用结合了 GRu 和 Conv1D 的模型。当我想拟合模型时,出现以下错误:

ValueError: Input 0 of layer "sequential_8" is in compatible with the layer: expected shape=(None, 223461, 5), found shape=(None, 5)

X_train 的形状是 (223461, 5),而 y_train 的形状是 (223461,)

这是我的代码:

verbose, epochs, batch_size = 0, 100, 64
n_timesteps, n_features, n_outputs = X_train.shape[0], X_train.shape[1], y_train.shape[0]
model = Sequential()
model.add(Conv1D(filters=64, kernel_size=3, activation='relu', input_shape=(n_timesteps,n_features)))
model.add(MaxPooling1D(pool_size=2))
model.add(GRU(64))
model.add(Dropout(0.4))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.4))
model.add(Dense(n_outputs, activation='softmax'))
opt = Adam(learning_rate=0.01)
model.compile(loss='categorical_crossentropy', optimizer=opt , metrics=['accuracy'])
model.summary()

summary的输出是:

Model: "sequential_8"
_____  Layer (type)                Output Shape              Param #
=====  conv1d_8 (Conv1D)           (None, 223459, 64)        1024
        max_pooling1d_8 (MaxPooling  (None, 111729, 64)       0           1D)

        gru_7 (GRU)                 (None, 64)                24960
        dropout_14 (Dropout)        (None, 64)                0
        flatten_6 (Flatten)         (None, 64)                0
        dense_14 (Dense)            (None, 128)               8320
        dropout_15 (Dropout)        (None, 128)               0
        dense_15 (Dense)            (None, 223461)            28826469

===== Total params: 28,860,773 Trainable params: 28,860,773 Non-trainable params: 0
_____

在这里我面临错误:

model.fit(X_train, y_train, epochs=epochs, batch_size=batch_size, verbose=verbose)
_, accuracy = model.evaluate(X_test, y_test, batch_size=batch_size, verbose=0)

【问题讨论】:

    标签: python tensorflow keras conv-neural-network conv1d


    【解决方案1】:

    根据您的模型,您的训练数据x_trainy_train 只是一条数据。

    所以你的训练数据必须扩大维度,像这样:

    X_train = X_train[None,:]
    y_train = y_train[None,:]
    

    或者使用 tensorflow 函数来做到这一点:

    X_train = tf.expand_dims(X_train, axis=0)
    y_train = tf.expand_dims(y_train, axis=0)
    

    模型的输出形状为 (1,223461)

    如果输出不是你所期望的,那说明你的模型设计是错误的。

    【讨论】:

    • 这是我想在我的模型中使用的architecture,我是如何制作它的吗?我想对数值数据进行二进制分类。我的课程是 0 和 1 。 @AugustusHsu
    • 我认为架构很好。但我想先检查你的数据集。剂量 X_train (223461, 5) 的形状意味着您有 223461 个具有 5 个序列和 1 个维度的数据?还是 1 个 223461 序列和 5 维的数据?
    • 是的,您可以查看我的 colab here。是的,我的意思是 223461 序列和 5 维。 @Augustus
    • 好的。现在你有了X_train,但你想预测什么?例如:如果要预测vmcategory的下一个时间戳。您需要设置n_timesteps 并将X_train 修改为(batch, n_timesteps, 5) 以预测vmcategory。请查看WindowGenerator 了解它的作用。
    • 另外,我将您的代码修改为this(我不确定这是否是您想要的。)。它只是一个草图,您可以更改架构、损失函数或参数(如 n_timesteps)。
    猜你喜欢
    • 2022-01-03
    • 2021-11-04
    • 2023-02-07
    • 1970-01-01
    • 1970-01-01
    • 2021-05-25
    • 2019-05-25
    • 2021-12-28
    • 2022-01-25
    相关资源
    最近更新 更多