一维CNN的输入形状(Keras) [英] Input Shape for 1D CNN (Keras)

查看:116
本文介绍了一维CNN的输入形状(Keras)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm building a CNN using Keras, with the following Conv1D as my first layer:

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"],
)

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

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.

模型可以编译,但是在训练过程中,出现此错误:

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

我查看了这篇文章,并尝试将输入内容更改为长度为1000浮点数的列表的ndarray,但最终出现另一个错误:

I looked at this post and tried changing my input to a ndarray of 1000-float-long lists, but ended up with another error:

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

有什么想法吗?

推荐答案

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

So I set the input_shape to (1000, 1)

我还将输入到fit()的输入转换为n个ndarray的单个ndarray(每个ndarray是1000个float的向量,n是样本/向量的总数),并将每个ndarray整形为(1,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

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

The final shape of my input data was (15641, 1000, 1)

所有这些都应同样适用于验证数据(如果已指定).

All of this should apply to validation data too (if specified).

这解决了我的问题

这篇关于一维CNN的输入形状(Keras)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆