首页 > 解决方案 > 训练损失和准确性以及验证准确性保持不变

问题描述

我正在尝试使用两个输入来训练 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 0 0 0 0 0 158] 型号:“model_6”


层(类型)输出形状参数#连接到

input_text (InputLayer) [(无, 100)] 0


embedding_7 (嵌入) (None, 100, 300) 889500 input_text[0][0]


bidirectional_7 (双向) (None, 100, 256) 439296 embedding_7[0][0]


global_max_pooling1d_7 (GlobalM (None, 256) 0 bidirectional_7[0][0]


dropout_7(丢弃)(无,256)0 global_max_pooling1d_7[0][0]


aux_output(密集)(无,1)257 dropout_7[0][0]


元输入(输入层)[(无,2)] 0


concatenate_7(连接)(无,3)0 aux_output[0][0]
meta_input[0][0]


密集_5(密集)(无,545)2180 concatenate_7[0][0]

总参数:1,331,233 可训练参数:1,331,233 不可训练参数:0


训练 11416 个样本,验证 2035 个样本 Epoch 1/20 11416/11416 [==============================] - 143s 13 毫秒/样本 - 损失: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 Epoch 3/20 11416/11416 [============= =================] - 150s 13ms/样本 - 损失:0.0233 - 准确度:0.9982 - val_loss:0.0233 - val_accuracy:0.9982 Epoch 4/20 11416/11416 [=== ===========================] - 166s 15ms/样本 - 损失:0.0232 - 准确度:0.9982 - val_loss:0.0234 - val_accuracy:0.9982 Epoch 5/20 11416/11416 [===============================] - 198s 17ms/样本 - 损失: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

标签: python-3.xtensorflowkerasneural-network

解决方案


推荐阅读