python - 一维 CNN (Keras) 的输入形状

标签 python keras conv-neural-network

我正在使用 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 数据帧,其中对于每一行,标签是一个 int(0 或 1),有效负载是一个 float 组,用零填充/截断为 1000 的长度。 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)

有什么想法吗?

最佳答案

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

我还将馈送到 fit() 的输入转换为 n 个 ndarrays 的单个 ndarray(每个 ndarray 是 1000 个 float 的向量,n 是样本/向量的总数)并将这些 ndarrays 中的每一个 reshape 为(1 , 1000, 1) 读取后的预处理期间 this输入和输入形状的解释

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

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

这解决了我的问题

关于python - 一维 CNN (Keras) 的输入形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53477565/

相关文章:

python - 在 Keras 中显示 'y' 的预测值和相应的实际 'x' 值

python - 使用 $\pm 1$ 值的内核进行卷积是否应该比使用常规类型更快?

python - Keras | 对象本地化预测整个图像

python - 改变预训练模型的输入形状及其权重

python - 如何在 Python 中处理流式传输到 Firebase 信令服务器的视频

python - 匹配由空格分隔的精确字符串 python

keras - 如何在从 keras 转换的 coreml 中格式化 conv1d/lstm nn 的输入数据

python - Keras Convolution2D 输入 : Error when checking model input: expected convolution2d_input_1 to have shape

python - 如何从Python子列表中提取值

Python 进度条通过日志记录模块