我正在尝试使用两个输入来训练CNN模型,但是我注意到训练和验证的准确性仍然很高且保持不变。我的代码可能有问题。欢迎任何解决此问题的帮助。
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense, Input, Dropout, concatenate
from tensorflow.keras.layers import Embedding, LSTM, GlobalMaxPooling1D
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.layers import Bidirectional
input_text = Input(shape=(100,), dtype='int32', name='input_text')
meta_input = Input(shape=(2,), name='meta_input')
embedding = Embedding(input_dim=len(tokenizer.word_index) + 1,
output_dim=300,
input_length=100)(input_text)
lstm = Bidirectional(LSTM(units=128,
dropout=0.9,
recurrent_dropout=0.9,
return_sequences=True),
merge_mode='concat')(embedding)
pool = GlobalMaxPooling1D()(lstm)
dropout = Dropout(0.5)(pool)
text_output = Dense(1, activation='sigmoid', name='aux_output')(dropout)
output = concatenate([text_output, meta_input])
output = Dense(n_codes, activation='relu')(output)
main_output = Dense(1, activation='softmax', name='main_output')(output)
model = Model(inputs=[input_text,meta_input], outputs=[output])
optimer = Adam(lr=.001)
model.compile(optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy'])
model.summary()
model.fit([X1, X2], [y],
validation_data=([X_valid1,X_valid2], [y_valid]),
batch_size=64, epochs=20, verbose=1)
结果: 训练行:11416 验证行:2035 [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 158] 型号:“ model_6”
input_text(InputLayer)[[None,100)] 0
embedding_7(嵌入)(无,100、300)889500 input_text [0] [0]
bidirectional_7(双向)(无,100,256)439296 embedding_7 [0] [0]
global_max_pooling1d_7(GlobalM(None,256)0 bidirectional_7 [0] [0]
dropout_7(Dropout)(无,256)0 global_max_pooling1d_7 [0] [0]
aux_output(密集)(无,1)257 dropout_7 [0] [0]
meta_input(InputLayer)[(None,2)] 0
concatenate_7(串联)(无,3)0 aux_output [0] [0]
meta_input [0] [0]
总参数:1,331,233 可训练的参数:1,331,233 不可训练的参数:0
训练11416个样本,验证2035个样本 时代1/20 11416/11416 [==============================]-143s 13ms /样本-损失:0.0254-准确性:0.9982-val_loss :0.0236-val_accuracy:0.9982 时代2/20 11416/11416 [==============================]-143s 13ms /样本-损失:0.0233-准确性:0.9982-val_loss :0.0235-val_accuracy:0.9982 时代3/20 11416/11416 [==============================]-150s 13ms / sample-损耗:0.0233-精度:0.9982-val_loss :0.0233-val_accuracy:0.9982 时代4/20 11416/11416 [==============================]-166s 15ms /样本-损耗:0.0232-精度:0.9982-val_loss :0.0234-val_accuracy:0.9982 时代5/20 11416/11416 [==============================]-198s 17ms / sample-损耗:0.0232-精度:0.9982-val_loss :0.0234-val_accuracy:0.9982 时代6/20 11416/11416 [==============================]-236s 21ms /采样-损耗:0.0232-精度:0.9982-val_loss :0.0232-val_accuracy:0.9982 时代7/20