如何使用两个文本特征作为输入来构建keras分类模型
How to build keras classification model using two text features as input
我正在尝试构建一个文本分类模型,该模型使用两个输入文本特征来最终预测 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())
我的问题是,这是最佳做法还是我应该研究其他问题?
所以对于这个问题,我能想到的最简单的形式如下。
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
并且您不需要为两个子模型显式拥有模型,因为您没有针对子模型进行优化,而是优化整个事情马上。
我正在尝试构建一个文本分类模型,该模型使用两个输入文本特征来最终预测 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())
我的问题是,这是最佳做法还是我应该研究其他问题?
所以对于这个问题,我能想到的最简单的形式如下。
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
并且您不需要为两个子模型显式拥有模型,因为您没有针对子模型进行优化,而是优化整个事情马上。