【问题标题】:Input Shape for 1D CNN (Keras)1D CNN (Keras) 的输入形状
【发布时间】:2018-11-26 08:56:32
【问题描述】:

我正在使用 Keras 构建一个 CNN,并将以下 Conv1D 作为我的第一层:

cnn.add(Conv1D(
    filters=512,
    kernel_size=3,
    strides=2,
    activation=hyperparameters["activation_fn"],
    kernel_regularizer=getattr(regularizers, hyperparameters["regularization"])(hyperparameters["regularization_rate"]),
    input_shape=(1000, 1),
))

我正在使用函数进行训练:

cnn.fit(
    x=train_df["payload"].tolist(),
    y=train_df["label"].tolist(),
    batch_size=hyperparameters["batch_size"],
    epochs=hyperparameters["epochs"],
)

其中 train_df 是一个包含两列的 pandas 数据帧,其中,对于每一行,label 是一个 int(0 或 1),而有效负载是一个用零填充/截断长度为 1000 的浮点 ndarray。 train_df 中的训练示例总数为 15641。

模型编译,但在训练期间,我收到此错误:

ValueError: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 1 array(s), but instead got the following list of 15641 arrays: [array([[0.09019608],
   [0.01176471],
   [0.01176471],
   [0.        ],
   [0.30196078],
   [0.        ],
   [0.        ],
   [0.        ],
   [0.        ],
   [0....

我查看了 this post 并尝试将我的输入更改为 1000 个浮点长列表的 ndarray,但最终出现另一个错误:

ValueError: Error when checking input: expected conv1d_1_input to have 3 dimensions, but got array with shape (15641, 1000)

有什么想法吗?

【问题讨论】:

标签: python keras conv-neural-network


【解决方案1】:

所以我将 input_shape 设置为 (1000, 1)

我还将提供给 fit() 的输入转换为一个包含 n 个 ndarray 的 ndarray(每个 ndarray 是一个 1000 个浮点数的向量,n 是样本/向量的总数)并将这些 ndarray 中的每一个重新整形为 (1 , 1000, 1) 在阅读 this 输入和输入形状说明后进行预处理

我输入数据的最终形状是 (15641, 1000, 1)

所有这些都应该适用于验证数据(如果指定)。

这解决了我的问题

【讨论】:

    猜你喜欢
    • 2018-08-23
    • 2021-05-19
    • 1970-01-01
    • 2020-10-17
    • 2019-10-19
    • 1970-01-01
    • 1970-01-01
    • 2018-02-16
    • 2020-10-20
    相关资源
    最近更新 更多