我正在尝试建立一个文本分类模型,该模型使用两个输入文本特征来最终预测10
类,但我需要对每个输入分支对最终输出产生重大影响,即每个分支应参与50%最终决定
当前要实现的设置如下
from keras import Input
from keras.models import Sequential, Model
from keras.layers import Dense, Dropout, Activation
from keras.layers.merge import concatenate
# input
input_1 = Input(shape=(x1_train[0].shape)) # , dtype = 'int32')
input_2 = Input(shape=(x2_train[0].shape)) # , dtype = 'int32')
desc = Sequential()
desc.add(Dense(5, activation='relu', input_shape=x1_train[0].shape))
desc.add(Dropout(0.2))
desc.add(Dense(10, activation='sigmoid'))
tax = Sequential()
tax.add(Dense(5, activation='relu', input_shape=x2_train[0].shape))
tax.add(Dropout(0.2))
tax.add(Dense(10, activation='sigmoid'))
# conact
concat = concatenate([desc, tax])
final_model = Sequential()
final_model.add(concat)
final_model.add(Dropout(0.5))
final_model.add(Dense(10, activation='softmax'))
# compile
model = Model(inputs=[input_1, input_2], outputs=final_model)
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['acc'])
print(model.summary())
我的问题是,这是最佳实践还是我应该研究其他东西?
答案 0 :(得分:1)
因此,我想出的最简单的形式如下。
from tensorflow.keras import Input
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense, Dropout, Activation, Concatenate
n_desc_features = 10
n_tax_features = 20
# Input layers
input_1 = Input(shape=(n_desc_features,)) # , dtype = 'int32')
input_2 = Input(shape=(n_tax_features,)) # , dtype = 'int32')
# Getting model 1 output
desc_out = Dense(5, activation='relu')(input_1)
desc_out = Dropout(0.2)(desc_out)
desc_out = Dense(10, activation='sigmoid')(desc_out)
# Getting model 2 output
tax_out = Dense(5, activation='relu')(input_2)
tax_out = Dropout(0.2)(tax_out)
tax_out = Dense(10, activation='sigmoid')(tax_out)
# Concatenating and creating the final output
concat = Concatenate(axis=-1)([desc_out, tax_out])
final_out = Dropout(0.5)(concat)
final_out = Dense(10, activation='softmax')(final_out)
# Create the model and compile
model = Model(inputs=[input_1, input_2], outputs=final_out)
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['acc'])
print(model.summary())
希望这很简单。您创建了两个子模型。一个产生desc_out
,另一个产生tax_out
。
然后使用这两个输出创建最终模型输出final_out
。然后将其与输入层一起使用来创建Model
对象。
我认为这里Sequential
是不必要的,并且您不需要显式地为两个子模型建立模型,因为您无需针对子模型进行优化,而可以在一次。