简体   繁体   English

如何用合适的输入形状训练 CNN?

[英]how to fit train CNN with the appropriate input shape?

I am trying to train a CNN and LSTM.network with S&P 500 dataset.我正在尝试使用 S&P 500 数据集训练 CNN 和 LSTM.network。 This is the shape of my train dataset:这是我的火车数据集的形状:

xtrain shape: (6445, 16) ytrain shape: (6445,) xtrain 形状:(6445, 16) ytrain 形状:(6445,)

this is the input shape I have gave to CNN:这是我给 CNN 的输入形状:

model.add(TimeDistributed(Conv1D(filters=64, kernel_size=1, activation='relu'), input_shape=(None,16)))

withe input shape parameter shown in the code I get this error:使用代码中显示的输入形状参数,我收到此错误:

ValueError: Input 0 of layer conv1d_8 is incompatible with the layer: : expected min_ndim=3, found ndim=2. ValueError:层 conv1d_8 的输入 0 与层不兼容::预期 min_ndim=3,发现 ndim=2。 Full shape received: [None, 16]收到完整形状:[无,16]

expected min_ndim=3, found ndim=2.

Keras expects three-dimensional arrays when working with Conv1D: the expected shape is [batch_size, sequence_length, feature_dimension] . Keras 在使用 Conv1D 时期望三维 arrays:期望的形状是[batch_size, sequence_length, feature_dimension] In your case, you only have one feature dimension, I suspect the price, but imagine you also wanted to pass the trading volumes data, you would have xtrain.shape == (6445,16,2) .在你的情况下,你只有一个特征维度,我怀疑价格,但假设你还想传递交易量数据,你会有xtrain.shape == (6445,16,2) The last dimension would contain information about the price and the volume.最后一个维度将包含有关价格和数量的信息。

To solve your issue, you need to reshape your xtrain to要解决您的问题,您需要将xtrain重塑为

(batch_size, sequence_length, feature_dimension=(6445,16,1)

To do so, you can use tensorflow:为此,您可以使用 tensorflow:

xtrain = tf.expand_dims(xtrain, axis=-1) # -1 means expand the LAST axis

or with numpy:或使用 numpy:

xtrain = np.expand_dims(xtrain, axis=-1) # -1 means expand the LAST axis

This function just does what the name implies: it adds a new axis in the position specified by axis .这个 function 顾名思义:它在 axis 指定的 position 中添加了一个新axis This results in xtrain having the shape we wanted, now you can just proceed with your model, for example:这导致xtrain具有我们想要的形状,现在您可以继续使用 model,例如:

model = keras.models.Sequential()
model.add(keras.layers.TimeDistributed(keras.layers.Conv1D(filters=64, kernel_size=1, activation='relu'), input_shape=(None,16,1)))

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

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