Keras(TensorFlow后端)拟合模型时出现维度错误求助
Hey there! Let's break down the dimension error you're hitting with your shared LSTM model in Keras (TensorFlow backend) and work through fixes step by step.
First, Let's Diagnose the Likely Issues
From your code snippet, the dimension mismatch is almost certainly tied to one (or more) of these common pitfalls when using shared stateful LSTMs:
1. Confusing LSTM Parameters & Input Shape
When you define shared_lstm = LSTM(INPUT_SIZE, stateful=args.stateful), remember:
- The first parameter of
LSTM()is the output dimension of the layer, not the input feature dimension. - For stateful LSTMs, your input tensors must have an explicit
batch_sizeand shape(batch_size, time_steps, feature_dim). If yourbuild_input_nodefunction isn't returning tensors with this exact shape (especially missing the feature dimension in the last axis), Keras will throw a dimension error.
2. Stateful LSTM State Carryover
When stateful=True, the LSTM retains the state from the previous batch of inputs. In your code, you're feeding text1 first, then text2 into the same shared LSTM—this means the state from processing text1's batch will carry over to text2's batch. If your data isn't structured to account for this (e.g., text1 and text2 aren't sequential batches in a single sequence), this can cause unexpected dimension mismatches or logical errors.
3. Ambiguous Concatenation Logic
You mention wanting to create a tensor of shape (2*batch_size, INPUT_SIZE), but haven't shown the actual concatenation code. If you're concatenating along the wrong axis (e.g., axis=1 instead of axis=0), you'll end up with a shape like (batch_size, 2*INPUT_SIZE) instead of what you want, leading to downstream layer dimension errors.
Fixed Example Code
Here's a revised version of your model with these issues addressed:
from keras.layers import Input, LSTM, Concatenate from keras.models import Model def build_model(args): # Clarify variable names to avoid confusion INPUT_FEATURE_DIM = 128 # e.g., your word embedding dimension LSTM_OUTPUT_UNITS = args.input_size # Renamed from INPUT_SIZE for clarity # Define input nodes with explicit shape for stateful LSTM text1 = Input( shape=(args.time_steps, INPUT_FEATURE_DIM), batch_size=args.batch_size, name='text1' ) text2 = Input( shape=(args.time_steps, INPUT_FEATURE_DIM), batch_size=args.batch_size, name='text2' ) # Use stateful only if you explicitly need sequence state carryover shared_lstm = LSTM(LSTM_OUTPUT_UNITS, stateful=args.stateful) encoded1 = shared_lstm(text1) # If using stateful=True, reset state before processing text2 (critical for training!) if args.stateful: shared_lstm.reset_states() encoded2 = shared_lstm(text2) # Explicitly concatenate along the batch axis to get (2*batch_size, LSTM_OUTPUT_UNITS) merged = Concatenate(axis=0)([encoded1, encoded2]) # Add your output layer here (e.g., classification/regression) # from keras.layers import Dense # output = Dense(1, activation='sigmoid')(merged) # Build and compile the model model = Model(inputs=[text1, text2], outputs=merged) model.compile(optimizer='adam', loss='binary_crossentropy') # Adjust loss for your task return model
Key Fixes to Note
- State Management: If you must use
stateful=True, remember to reset the LSTM state between processingtext1andtext2during training (either in the model build as shown, or in your training loop). If you don't need to retain sequence state, setstateful=False—this will eliminate most state-related dimension headaches. - Input Shape Clarity: Ensure your input tensors include the feature dimension (last axis) and explicit
batch_sizefor stateful mode. - Concatenation Axis: Use
axis=0when concatenating to stack batches, and verify the shape matches what your downstream layers expect.
内容的提问来源于stack exchange,提问作者RePierre

