简体   繁体   English

将功能连接到具有多个输入的 Keras Functional API 时出错

[英]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([])?我会与layers.concatenate([]) 连接吗? EX: I've calculated the semantic similarity of headline and document. EX:我计算了标题和文档的语义相似度。 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).警告:tensorflow:Model 是用形状 (None, 26784) 构建的,用于输入 Tensor("semantic_6:0", shape=(None, 26784), dtype=float32),但它在形状不兼容的输入上被调用 (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]值错误:dense_16 层的输入 0 与层不兼容:输入形状的预期轴 -1 具有值 26804,但接收到形状为 [None, 21] 的输入

UPDATE 9/25/2020 2020 年 9 月 25 日更新


I believe the issue was due to a syntax error on my part in the x = concatenate() function.我相信这个问题是由于我在 x = concatenate() 函数中的语法错误造成的。

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

There is a syntax error in the x = concatenate() function. x = concatenate() 函数中存在语法错误。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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