0
votes

I am having some trouble adjusting the following model. I am trying to train a model with keras and tensorflow to classify time sequences of a radar emitter. The error it is giving is:

ValueError: Error when checking target: expected concatenate_1 to have shape (2,) but got array with shape (1,)

It seems that I am not able to adjust the proper dimensions for the labels (I have tried with categorical labels (I have to classes but it outputs to 3, why?) and using [y_train,y_train] in the fit call, but no luck at all. Maybe the error comes from a different part and I am not able to see it.

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())

Model summary is:

Layer(type) ------------------- Output Shape -------- Param #----Connected to


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


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


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


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


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


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


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


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


aux_output (Dense) --------------- (None, 1) ------ 257 --------- dropout_1[0][0]


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


concatenate_1 (Concatenate) ------- (None, 2) ------ 0 --------- aux_output[0][0] main_output[0][0]


Total params: 921,730 Trainable params: 921,730 Non-trainable params: 0

Thanks for the help!

1
I would start debugging by printing the shapes of auxiliary_output and main_output. Also, I wonder what your concatenate function does. It would also help if you provided the full stack trace.iga

1 Answers

0
votes

Turns out the error was coming from the labels, as I initially thought. However, I did not use the proper shape so I guessed the problem was somewhere else. As there are two inputs, there should be two inputs also for the labels, so I initially used:

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

But this was NOT right as the shape of the labels where in two different arrays instead of in one. So the proper way to do it is:

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

Reshaping the np arrays into just one with 2 columns, repeating the labels.

Actually the same problem occurs in the evaluate function, it should be as:

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

Cheers