Keras和张量流的级联和拟合错误

时间:2018-06-25 08:48:08

标签: python tensorflow keras

我在调整以下型号时遇到一些麻烦。我正在尝试使用keras和tensorflow训练模型以对雷达发射器的时间序列进行分类。它给出的错误是:

  

ValueError:检查目标时出错:预期concatenate_1具有形状(2,)但具有形状(1,)的数组

似乎我无法调整标签的正确尺寸(我尝试过使用分类标签(我必须分类,但输出为3,为什么?),并在fit调用中使用[y_train,y_train] ,但完全没有运气。也许错误来自不同的部分,我看不到它。

def get_model_lstm_fcn(shape):
    main_input = Input(shape=shape, dtype='float32', name='main_input')
    lstm_out = LSTM(256, dtype=float)(main_input)
    # lstm_out = (LSTM(128, dtype=float))(lstm_out)
    lstm_out = (Dropout(0.25))(lstm_out)
    auxiliary_output = Dense(1, activation='sigmoid', name='aux_output')(lstm_out)

    auxiliary_input = Input(shape=shape, name='aux_input')
    conv = Convolution1D(128, 10, input_shape=shape, activation='relu')(auxiliary_input)
    conv = (Convolution1D(256, 10, activation='relu'))(conv)
    conv = (Convolution1D(128, 10, activation='relu'))(conv)
    conv = (GlobalAveragePooling1D())(conv)
    main_output = Dense(1, activation='sigmoid', name='main_output')(conv)

    concatenation = concatenate([auxiliary_output, main_output])

    model = Model(inputs=[main_input, auxiliary_input], outputs=concatenation)
    model.summary()
    model.compile(optimizer='adam', loss='binary_crossentropy',
              metrics=['accuracy'])

    return model

if __name__ == "__main__":
    x_train = load_data("D:\\Software_Herramienta\\Datasets\\sweep_switch_train.csv")
    y_train = load_data("D:\\Software_Herramienta\\Datasets\\sweep_switch_labels_train.csv")
    x_train = x_train.reshape(x_train.shape[0], x_train.shape[1], 1)
    y_train.astype(int)
    y_train = y_train.reshape(1000, 1)
    # y_train = to_categorical(y_train)

    batch = 50

    model = get_model_lstm_fcn(x_train.shape[1:])
    model.fit([x_train,x_train], y_train, epochs=5, batch_size=batch)

    x_test = load_data("D:\\Software_Herramienta\\Datasets\\sweep_switch_test.csv")
    y_test =     load_data("D:\\Software_Herramienta\\Datasets\\sweep_switch_labels_test.csv")
    x_test = x_test.reshape(x_test.shape[0], x_test.shape[1], 1)
    y_test.astype(int)
    y_test = y_test.reshape(1000, 1)
    y_test = to_categorical(y_test)

    loss_and_metrics = model.evaluate(x_test, y_test, batch_size=batch)
    classes = model.predict(x_test, batch_size=batch)
    print("Loss, accuracy: ")
    print(loss_and_metrics)
    print("Classes:")
    print(classes.flatten())

模型摘要为:

图层(类型)-------------------输出形状--------参数#----已连接到


aux_input(InputLayer)--------(无,1000,1)----- 0 ----------


conv1d_1(Conv1D)--------------(无,991,128)--- 1408 ----- aux_input [0] [0]


main_input(InputLayer)---------(None,1000,1)---- 0


conv1d_2(Conv1D)--------------(无,982,256)---- 327936 ----- conv1d_1 [0] [0]


lstm_1(LSTM)-------------------(None,256)------- 264192 ---- main_input [0] [0]


conv1d_3(Conv1D)---------------(无,973,128)--- 327808 ---- conv1d_2 [0] [0]


dropout_1(Dropout)-------------(None,256)-------- 0 --------- lstm_1 [0] [0]


global_average_pooling1d_1 ------(无,128)-------- 0 --------- conv1d_3 [0] [0]


aux_output(密集)---------------(无,1)------ 257 --------- dropout_1 [0] [0]


main_output(Dense)---------------(None,1)---- 129 --------- global_average_pooling1d_1 [0] [0]


concatenate_1(串联)-------(无,2)------ 0 --------- aux_output [0] [0] main_output [0] [0] < / p>


总参数:921,730 可训练的参数:921,730 不可训练的参数:0

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

原来是我最初认为的,错误来自标签。但是,我没有使用正确的形状,所以我猜到问题出在其他地方。 由于有两个输入,因此标签也应该有两个输入,因此我最初使用:

model.fit([x_train, x_train], [y_train, y_train], epochs=5, batch_size=batch)

但这并不正确,因为标签的形状位于两个不同的数组中而不是一个数组中。因此,正确的方法是:

model.fit([x_train, x_train], np.reshape([y_train, y_train], (1000, 2)), epochs=5, batch_size=batch)

将np数组重塑为只有2列的数组,并重复标签。

实际上,在评估函数中会出现相同的问题,它应该是:

loss_and_metrics = model.evaluate([x_test, x_test], np.reshape([y_test, y_test]), batch_size=batch)

欢呼