当训练数据和测试数据的数量不同时,如何使用Keras?

时间:2017-11-09 01:48:56

标签: deep-learning keras

以下代码创建一个使用多个变量预测特定值的模型。我的数据以矩阵形式组成11947乘9,我创建的数据使用9000进行训练,2947进行测试。

import numpy as np
from keras.models import Model
from keras.layers import Dense, Input, concatenate, Conv1D
batchSize=1000

def get_model(rows, cols):

    inputs1 = Input(shape=(1, rows))
    inputs2 = Input(shape=(1, rows))
    inputs3 = Input(shape=(1, rows))
    inputs4 = Input(shape=(1, rows))
    inputs5 = Input(shape=(1, rows))
    inputs6 = Input(shape=(1, rows))
    inputs7 = Input(shape=(1, rows))

    conv1 = Conv1D(1024, 1, activation='relu', padding='same')(inputs1)
    conv1 = Conv1D(512, 1, activation='relu', padding='same' )(conv1)

    conv2 = Conv1D(1024, 1, activation='relu', padding='same')(inputs2)
    conv2 = Conv1D(512, 1, activation='relu', padding='same')(conv2)

    conv3 = Conv1D(1024, 1, activation='relu', padding='same')(inputs3)
    conv3 = Conv1D(512, 1, activation='relu', padding='same')(conv3)

    conv4 = Conv1D(1024, 1, activation='relu', padding='same')(inputs4)
    conv4 = Conv1D(512, 1, activation='relu', padding='same')(conv4)

    conv5 = Conv1D(1024, 1, activation='relu', padding='same')(inputs5)
    conv5 = Conv1D(512, 1, activation='relu', padding='same')(conv5)

    conv6 = Conv1D(1024, 1, activation='relu', padding='same')(inputs6)
    conv6 = Conv1D(512, 1, activation='relu', padding='same')(conv6)

    conv7 = Conv1D(1024, 1, activation='relu', padding='same')(inputs7)
    conv7 = Conv1D(512, 1, activation='relu', padding='same')(conv7)

    convConcat = concatenate([conv1, conv2, conv3, conv4, conv5, conv6, conv7])

    convOut = Dense(rows, activation='relu')(convConcat)


    model = Model(inputs=[inputs1, inputs2, inputs3, inputs4, inputs5, inputs6, inputs7], outputs=[convOut])


    model.compile(optimizer='Adam', loss='mean_squared_error', metrics=['accuracy'])

    return model

def train_and_predict():
    dataT = data_train.transpose()

    dataT_O = dataT[0].reshape((1, 1, -1))
    dataT_1 = dataT[2].reshape((1, 1, -1))
    dataT_2 = dataT[3].reshape((1, 1, -1))
    dataT_3 = dataT[4].reshape((1, 1, -1))
    dataT_4 = dataT[5].reshape((1, 1, -1))
    dataT_5 = dataT[6].reshape((1, 1, -1))
    dataT_6 = dataT[7].reshape((1, 1, -1))
    dataT_7 = dataT[8].reshape((1, 1, -1))

    model.fit([dataT_1, dataT_2, dataT_3, dataT_4, dataT_5, dataT_6, dataT_7], dataT_O, epochs=100, batch_size=batchSize)

    dataT = data_test.transpose()

    dataT_1 = dataT[2].reshape((1, 1, -1))
    dataT_2 = dataT[3].reshape((1, 1, -1))
    dataT_3 = dataT[4].reshape((1, 1, -1))
    dataT_4 = dataT[5].reshape((1, 1, -1))
    dataT_5 = dataT[6].reshape((1, 1, -1))
    dataT_6 = dataT[7].reshape((1, 1, -1))
    dataT_7 = dataT[8].reshape((1, 1, -1))

    model.predict([dataT_1, dataT_2, dataT_3, dataT_4, dataT_5, dataT_6, dataT_7])
    print('Done!')

if __name__ == '__main__':

    data_train = np.genfromtxt('./trainingLossdata_train.txt')
    data_test = np.genfromtxt('./trainingLossdata_test.txt')

    model = get_model(data_train.shape[0], data_train.shape[1])

    train_and_predict()

但是,由于训练数据和测试数据之间的差异而发生错误。确实显示以下错误消息。

  

“ValueError:检查时出错:预期input_1具有形状(无,1,9000)但是具有形状的数组(1,1,294)”

我该如何解决这个问题?事实上,我是使用Keras进行深度学习编程的初学者。请给我一些建议。

1 个答案:

答案 0 :(得分:0)

在我看来,您的输入创建是错误的。它与不同数量的训练和测试样本无关(实际上这种情况一直发生)。现在,你说

  

我的数据由9月组成11947

这意味着您有11947个样本,每个样本有9个功能。因此输入的理想形状应为None,9None,1,9。这个None实际上代表了输入的大小。