简体   繁体   中英

Getting errors when concatenating features to Keras Functional API with multiple inputs

Where would I insert features I've extracted from the training set to use in the model? Would I just concatenate with layers.concatenate([])? EX: I've calculated the semantic similarity of headline and document. I want to that feature as an input in the model.

Info:

embedded_sequences_head: Tensor w/shape (None, 15, 300) #Glove300D
embedded_sequences_body: Tensor w/shape (None, 150, 300) # Glove 300D
sequence_input_head: Tensor w/shape (None, 15)
sequence_input_body: Tensor w/shape (None, 150)
sequence_input_body: Tensor w/shape (None, 26784)
headline_pad: ndarray w/shape (26784, 15), dtype=int32
art_body_pad: ndarray w/shape (26784, 150), dtype=int32
y_train_cat: ndarray w/shape (26784, 4), dtype=float32
semantic_x_tr = np.array(x_train['semantic_sim_70'].to_list()) # ndarray (26784,)

Model

semantic_feat = Input(shape=(len(semantic_x_tr),), name ="semantic")
x1  = Conv1D( FILTERS, kernel_size = KERNEL, strides = STRIDE, padding='valid', activation = 'relu')(embedded_sequences_head)
x11 = GlobalMaxPooling1D()(x1)
x2  = Conv1D( FILTERS, kernel_size = KERNEL, strides = STRIDE, padding='valid', activation = 'relu')(embedded_sequences_body)
x22 = GlobalMaxPooling1D()(x2)
x = concatenate([x11,x22, semantic_feat], axis=1)
x = Dense(UNITS, activation="relu")(x)
x = Dropout(0.5)(x)
preds = Dense(4, activation="softmax", name = 'predic')(x)

Train Model

model = Model(inputs = [sequence_input_head, sequence_input_body, semantic_feat], outputs = [preds],)

history = model.fit({'headline':headline_pad, 'articleBody':art_body_pad, 'semantic': semantic_x_tr},
                    {'predic':y_train_cat},
                    epochs=100,
                    batch_size= BATCH__SIZE,
                    shuffle= True,
                    validation_data = ([headline_padded_validation, art_body_padded_validation, semantic_x_val], y_val_cat),
                    callbacks = [es]
                    )

This Model block compiles with seemingly no errors, but when I go to run the Train Model block of code it returns a warning and error:

WARNING: tensorflow:Model was constructed with shape (None, 26784) for input Tensor("semantic_6:0", shape=(None, 26784), dtype=float32), but it was called on an input with incompatible shape (None, 1).

ValueError: Input 0 of layer dense_16 is incompatible with the layer: expected axis -1 of input shape to have value 26804 but received input with shape [None, 21]

UPDATE 9/25/2020


I believe the issue was due to a syntax error on my part in the x = concatenate() function.

x = tf.keras.layers.Concatenate(axis=1)([x11, x22, semantic_feat])

There is a syntax error in the x = concatenate() function.

I fixed the errors I was getting by changing:

x = concatenate([x11,x22, semantic_feat], axis=1)

to

x = tf.keras.layers.Concatenate(axis=1)([x11, x22, semantic_feat])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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