首页 > 解决方案 > 尝试训练 NLP 模型时出现值错误

问题描述

'''
epochs = 2 #### before: 30 #### setting lower to save time

input_text = Input(shape=(400, ))
          
input_title = Input(shape=(20, ))
input_nontext = Input(shape=(25, )) ######## input_nontext = Input(shape=(25, ))

# Text
text_embeddings = Embedding(NUM_WORDS_TEXT, output_dim=100,
                     embeddings_initializer=Constant(wiki_weights_text),
                     input_length=400, trainable=False)(input_text)
LSTM_text = LSTM(100, return_sequences=False)(text_embeddings)
dense_text = Dense(1, activation="linear")(LSTM_text)

# Title
title_embeddings = Embedding(NUM_WORDS_TITLE, output_dim=100,
                     embeddings_initializer=Constant(wiki_weights_title),
                     input_length=20, trainable=False)(input_title)
LSTM_title = LSTM(100, return_sequences=False)(title_embeddings)
dense_title = Dense(1, activation="linear")(LSTM_title)

# Concatenate
concat = concatenate([dense_text, dense_title, input_nontext])
dense_full = Dense(1024, activation="relu")(concat)
dense_full = Dense(512, activation="relu")(dense_full)
dense_full = Dense(256, activation="relu")(dense_full)
output_layer = Dense(1, activation="linear")(dense_full)

model = Model(inputs=[input_text, input_title, input_nontext], outputs = output_layer)
model.compile(loss = losses.LogCosh(), optimizer = "adam", metrics=['mae', 'mse'])

print(model.summary())
callbacks = [EarlyStopping(monitor='val_loss', patience=3, verbose=1, min_delta=0.01, restore_best_weights=True)]

story = model.fit([X_tr_int_pad_text, X_tr_int_pad_title, X_train_text.drop(columns=["summary_clean", "name_clean"])], 
                  y_train_text, epochs=epochs, verbose=1, batch_size=256, validation_split=0.2, callbacks=callbacks)
'''

我收到以下错误:ValueError: Error when checks input: expected input_53 to have shape (25,) but got array with shape (4,)

由于我已经在第 5 行定义了形状,所以我不明白为什么会出现这个错误。任何帮助将不胜感激,谢谢!

标签: kerasnlp

解决方案


推荐阅读