简体   繁体   English

Keras 用于大量样本的 Conv1D 输入

[英]Keras Conv1D input for a very large number of samples

I have a dataset containing a huge amount of samples 1686663 and 107 features (1686663, 107).我有一个包含大量样本 1686663 和 107 个特征(1686663、107)的数据集。 I'm building a neural network using keras, and wanted to apply a 1D convolution Conv1D.我正在使用 keras 构建神经网络,并想应用一维卷积 Conv1D。

The input for the Conv1D is (batch size, number_features, timestep). Conv1D 的输入是(批量大小、数字特征、时间步长)。 the batch size is basically the number of samples, however in my case i cannot use the number of samples which is too large for my RAM.批量大小基本上是样本的数量,但是在我的情况下,我不能使用对于我的 RAM 来说太大的样本数量。 So i selected a batch size = 512.所以我选择了一个批量大小 = 512。

in_shape = (batch_size,x_train.shape[1],1)

Hence, my input shape is now (512, 107, 1).因此,我的输入形状现在是 (512, 107, 1)。

I reshaped the training vectors to match the convolution:我重塑了训练向量以匹配卷积:

x_train = x_train.reshape(x_train.shape[0],x_train_shape[1],1)

When running training i get the following error:运行训练时出现以下错误:

ValueError: Input 0 of layer "sequential_10" is incompatible with the layer: expected shape=(None, 512, 107, 1), found shape=(None, 107, 1) 

Could anyone tell me what I am missing here?谁能告诉我我在这里缺少什么?

When you specify the input shape, either by adding atf.keras.Input layer as first layer, or by setting the argument input_shape directly in the first layer of your model, you don't have to add the batch size.当您指定输入形状时,通过添加tf.keras.Input层作为第一层,或者通过直接在 model 的第一层中设置参数input_shape ,您不必添加批量大小。 So in your case it would be:因此,在您的情况下,它将是:

in_shape = (x_train.shape[1], 1)

The batch size is automatically set as first dimension of your input shape, by taking the value you set in the batch_size argument of the fit() method.通过采用您在fit()方法的batch_size参数中设置的值,批量大小会自动设置为输入形状的第一维。

But if you do like this (batch_size, x_train.shape[1], 1) , it will add the batch size twice.但是,如果您这样做(batch_size, x_train.shape[1], 1) ,它将两次添加批量大小。

The error is basically saying that it expected to find (batch size, 512, 107, 1) but found (batch size, 107, 1) .该错误基本上是说它期望找到(batch size, 512, 107, 1)但找到了(batch size, 107, 1) It was expecting that additional 512, because you added the batch size twice.预计会有额外的 512,因为您添加了两次批量大小。

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

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