简体   繁体   English

一维 CNN (Keras) 的输入形状

[英]Input Shape for 1D CNN (Keras)

I'm building a CNN using Keras, with the following Conv1D as my first layer:我正在使用 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),
))

I'm training with the function:我正在训练这个功能:

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

In which train_df is a pandas dataframe of two columns where, for each row, label is an int (0 or 1) and payload is a ndarray of floats padded with zeros/truncated to a length of 1000. The total # of training examples within train_df is 15641.其中 train_df 是两列的 Pandas 数据帧,其中,对于每一行,标签是一个 int(0 或 1),payload 是一个用零填充/截断为 1000 长度的浮点数组。其中的训练示例总数train_df 是 15641。

The model compiles, but during training, I get this error:模型编译,但在训练期间,我收到此错误:

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....

I looked at this post and tried changing my input to a ndarray of 1000-float-long lists, but ended up with another error:我查看了这篇文章并尝试将我的输入更改为 1000 个浮点长列表的 ndarray,但最终出现了另一个错误:

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

Any ideas?有任何想法吗?

So I set the input_shape to (1000, 1)所以我将 input_shape 设置为 (1000, 1)

I also converted the input that's fed to fit() into a single ndarray of n ndarrays (each ndarray is a vector of 1000 floats, n is the total count of samples/vectors) and reshaped each of those ndarrays to (1, 1000, 1) during preprocessing after reading this explanation on inputs & input shape我还将提供给 fit() 的输入转换为 n 个 ndarray 的单个 ndarray(每个 ndarray 是一个包含 1000 个浮点数的向量,n 是样本/向量的总数)并将这些 ndarray 中的每一个重新整形为 (1, 1000, 1)读取后预处理期间输入端上的和输入的形状的说明

The final shape of my input data was (15641, 1000, 1)我的输入数据的最终形状是 (15641, 1000, 1)

All of this should apply to validation data too (if specified).所有这些也应该适用于验证数据(如果指定)。

This fixed my issue这解决了我的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM