问题描述
我应该在哪里插入从训练集中提取的特征以在模型中使用?我是否可以与layers.concatenate([])串联? 例:我已经计算出标题和文档的语义相似度。我想将该功能作为模型中的输入。
信息:
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,)
模型
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,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)
火车模型
model = Model(inputs = [sequence_input_head,sequence_input_body,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]
)
此 Model 块似乎没有错误,但是当我运行 Train Model 代码块时,它返回警告和错误:
警告:张量流:模型的形状(无,26784)用于 输入Tensor(“ semantic_6:0”,shape =(None,26784),dtype = float32),但是 在形状不兼容(无,1)的输入中调用了它。
ValueError:层density_16的输入0与该层不兼容: 输入形状的预期轴-1的值为26804,但已接收到输入 形状为[无,21]
UPDATE 9/25/2020
我认为问题是由于x = concatenate()函数中的语法错误所致。
x = tf.keras.layers.Concatenate(axis=1)([x11,semantic_feat])
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)