我正在使用 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/