如何让Keras IMDB情感分类模型直接接收字符串作为输入?
Absolutely—you can tweak the pipeline so your model accepts raw string reviews instead of preprocessed integer sequences. The key is building a preprocessing layer (or function) that converts your input strings into the integer format the model expects, using the IMDB word index mapping.
Let’s break this down into actionable steps with code examples:
1. Load and Adjust the IMDB Word Index
First, grab the official word index from Keras. Note that the index reserves a few special tokens (like padding, start, and unknown words) that are offset by 3—we’ll adjust the mapping to include these:
from tensorflow.keras.datasets import imdb from tensorflow.keras.preprocessing.sequence import pad_sequences # Load the raw word index word_index = imdb.get_word_index() # Add reserved tokens and shift existing indices word_index = {k: (v + 3) for k, v in word_index.items()} word_index["<PAD>"] = 0 # For padding sequences to fixed length word_index["<START>"] = 1 # Mark the start of a review word_index["<UNK>"] = 2 # For words not found in the index word_index["<UNUSED>"] = 3
2. Build a String-to-Sequence Preprocessing Function
This function will clean your input string, split it into words, map each word to its corresponding index, and align the sequence to the model’s expected input length:
def preprocess_raw_review(review, max_sequence_length=80): # Clean the review to match the original dataset's preprocessing review = review.lower().strip() # Remove common punctuation (adjust regex for more precision if needed) review = review.replace(".", "").replace(",", "").replace("!", "").replace("?", "") words = review.split() # Convert words to indices; use <UNK> for unknown terms sequence = [word_index.get(word, word_index["<UNK>"]) for word in words] # Add the required <START> token at the beginning sequence = [word_index["<START>"]] + sequence # Pad or truncate to match the model's input length padded_sequence = pad_sequences( [sequence], maxlen=max_sequence_length, padding="post", truncating="post" ) return padded_sequence
3. Use the Preprocessing with Your Trained Model
Assuming you have your trained LSTM model (like the one from the IMDB LSTM example), you can now pass raw strings directly by preprocessing them first:
# Example raw review input raw_review = "This movie is awesome! I loved every minute of it, the acting was top-tier." # Preprocess the string into a model-compatible sequence processed_input = preprocess_raw_review(raw_review) # Get prediction from the model prediction = model.predict(processed_input) # Interpret the result (standard IMDB model outputs 0 for negative, 1 for positive) sentiment = "positive" if prediction[0][0] > 0.5 else "negative" print(f"Review sentiment: {sentiment}")
Key Notes to Keep in Mind
- Consistent Preprocessing: Make sure your string cleaning matches how the original IMDB dataset was processed (lowercasing, punctuation removal, whitespace splitting). Mismatched preprocessing can hurt model performance.
- Unknown Words: Words not in the word index will map to
<UNK>. The model was trained with this token, so it should still generalize well to unseen terms. - Input Length: The
max_sequence_lengthmust match themaxlenvalue used during model training (the original example uses 80). Adjust this if your model expects a different input size.
内容的提问来源于stack exchange,提问作者freeChris

