且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Keras 和 Conv1D 问题的输入形状

更新时间:2023-12-02 08:02:46

什么是(训练)样本?

(训练)数据可能包含数十、数百或数千个样本.例如,像 Cifar-10 或 ImageNet 这样的图像数据集中的每个图像都是一个样本.再举一个例子,对于一个时序数据集,该数据集由 10 年多天的天气统计数据组成,每个训练样本可能是每天的时间序列.如果我们在一天中记录了 100 次测量,并且每次测量都包含温度和湿度(即每次测量有两个特征),那么我们数据集的形状大致为 (10x365, 100, 2).

批量大小只是模型一次可以处理的样本数.我们可以使用 Keras 中 fit 方法的 batch_size 参数设置批量大小.常见的值是 16、32、64、128、256 等(尽管您必须选择一个数字,以便您的机器有足够的 RAM 来分配所需的资源).

The batch size is simply the number of samples that can be processed by the model at a single time. We can set the batch size using the batch_size argument of fit method in Keras. The common values are 16, 32, 64, 128, 256, etc (though you must choose a number such that your machine could have enough RAM to allocate the required resources).

此外,步数"(也称为序列长度")和通道"(也称为特征大小")分别是测量次数和每次测量的大小.例如,在上面的天气示例中,我们有 steps=100 和 channels=2.

Further, the "steps" (also called "sequence length") and "channels" (also called "feature size") are the number of measurements and the size of each measurement, respectively. For example in our weather example above, we have steps=100 and channels=2.

要解决代码问题,您需要定义训练数据(即 X),使其具有 (num_samples、steps 或 time_len、channels 或 feat_size)的形状代码>:

To resolve the issue with your code you need to define your training data (i.e. X) such that it has a shape of (num_samples, steps or time_len, channels or feat_size):

n_samples = 1000   # we have 1000 samples in our training data
n_channels = 1     # each measurement has one feature
X = np.random.randn(n_samples, time_len, n_channels)

# if you want to predict one value for each measurement
Y = np.random.randn(n_samples, time_len)

# or if you want to predict one value for each sample
Y = np.random.randn(n_samples)

还有一件事是您应该将一个样本的形状作为模型的输入形状传递.因此,输入层的输入形状必须像 shape=X.shape[1:] 一样传递.

One more thing is that you should pass the shape of one sample as the input shape of the model. Therefore, the input shape of Input layer must be passed like shape=X.shape[1:].