convolution1d_1 的预期形状是什么

What is the expected convolution1d_1 to have shape

我目前正在使用卷积层来训练神经网络,但似乎在向其提供输入和输出维度时遇到了一些问题。

输入包括 (x,2050) 输出为 (x,13) 因此,对于具有 2050 列的每一行,应该创建一个包含 13 个元素的数组。

#Define 10 folds:
seed = 7
np.random.seed(seed)
kfold = KFold(n_splits=10, shuffle=False, random_state=None)
print "Splits"
cvscores_acc = []
cvscores_loss = []
hist = []
i = 0
train_set_data_vstacked_normalized_reshaped = np.reshape(train_set_data_vstacked_normalized,train_set_data_vstacked_normalized.shape+(1,))
train_set_output_vstacked_normalized_reshaped = np.reshape(train_set_output_vstacked_normalized,train_set_output_vstacked_normalized.shape+(1,))
for train, test in kfold.split(train_set_data_vstacked_normalized_reshaped):

    print "Model definition!"
    model = Sequential()

    model.add(Convolution1D(13, 3, border_mode='same', input_shape=(2050,1)))

    print "Compiling"
    model.compile(loss='mean_squared_error', optimizer="RMSprop")
    print "Compile done! "

    print '\n'

    print "Train start"

    reduce_lr=ReduceLROnPlateau(monitor='val_loss', factor=0.01, patience=3, verbose=1, mode='auto', epsilon=0.0001, cooldown=0, min_lr=0.00000001)
    stop  = EarlyStopping(monitor='val_loss', min_delta=0, patience=5, verbose=1, mode='auto')

    log=csv_logger = CSVLogger('training_'+str(i)+'.csv')


    hist_current = model.fit(train_set_data_vstacked_normalized_reshaped[train],
                                train_set_output_vstacked_normalized_reshaped[train],
                                shuffle=False,
                                validation_data=(train_set_data_vstacked_normalized_reshaped[test],train_set_output_vstacked_normalized_reshaped[test]),
                                validation_split=0.1,
                                nb_epoch=150,
                                verbose=1,
                                callbacks=[reduce_lr,log,stop])

    hist.append(hist_current)

    print()
    print model.summary()
    print "Model stored"
    model.save("Model"+str(i)+".h5")
    model.save_weights("Model"+str(i)+"_weights.h5")
    del model
    print "New Model:"
    i=i+1

我似乎在输入数据时遇到问题,我在某处读到 Convolution1D 期望 3d 作为输入和输出,这就是为什么我将其重塑为 3d 的原因。但出于某种原因是不是还是不合适。

我在当前代码中遇到此错误:

Exception: Error when checking model target: expected convolution1d_1 to have shape (None, 2050, 13) but got array with shape (221286, 13, 1)

为什么我无法开始训练?...

所以基本上 - 在您的网络中您有:

  • 由一维特征向量序列组成的输入,每个特征向量的长度为 2050。
  • 您正在应用一组具有 kernel_size = 3 的 13 one-dimensional 卷积特征,在 same 边界模式中生成每个长度为 2050 的 13 维特征向量序列。

这解释了为什么 Keras 向您显示它期待 (None, 2050, 13) 目标形状。

有几种方法可以让它发挥作用:

  1. 您可以通过添加以下内容来展平卷积层的输出:

    from keras.layers import Flatten, Dense 
    model.add(Flatten())
    model.add(Dense(13))
    

    然后通过 train_set_output_vstacked_normalized_reshaped[train].reshape((221286, 13))

  2. 将目标拉平
  3. 通过添加另一个卷积层结合池化和不同的边界模式以获得长度为13的输出序列。这里有太多的可能性,我将跳过这部分。

请注意 - 您在这里仅使用激活函数。虽然在最后的 dense 层中这似乎是一个不错的决定,但在前面的层中最好应用一些 non-linear 激活(如 relu)以使您的网络更好地工作。