Thinc中使用PyTorchLSTM时出现维度不匹配ValueError的问题求助
Let's break down exactly why you're hitting this error and how to fix it quickly.
The Root Cause
Your model's PyTorchLSTM layer (wrapped in with_padded) outputs a 3-dimensional tensor with shape (batch_size, sequence_length, hidden_dim). This makes sense because an LSTM returns a hidden state for every time step in your input sequence.
But the next component, with_array2d(Sigmoid(...)), expects a 2-dimensional input (shape (batch_size, num_features)). with_array2d only converts array formats—it doesn't automatically collapse the sequence dimension from the LSTM output. That's where the mismatch happens.
Two Simple Fixes
You need to convert the 3D LSTM output to 2D before passing it to the Sigmoid layer. Thinc has built-in components for this common use case:
1. Take the Last Valid Time Step (Most Common for Sequence Classification)
Use reduce_last() to extract the hidden state from the last non-padding time step of each sequence. This is standard for tasks like sentiment classification where you care about the final context of the sequence:
from thinc.api import chain, PyTorchLSTM, Sigmoid, Embed, with_padded, with_array2d, reduce_last vocab_size = len(vocab_to_int)+1 # +1 for the 0 padding + our word tokens output_size = 1 embedding_dim = 400 hidden_dim = 256 n_layers = 2 model = chain( Embed(nV=vocab_size, nO=embedding_dim), with_padded(PyTorchLSTM(nI=embedding_dim,nO=hidden_dim, depth=n_layers)), reduce_last(), # This collapses the sequence dimension to 2D with_array2d(Sigmoid(nI=hidden_dim, nO=output_size)) ) model.initialize(X=train_x[:5], Y=train_y[:5])
2. Average Pool Over the Sequence
If you want to use context from the entire sequence instead of just the last step, use mean_pool() to calculate the average of all hidden states across the sequence (ignoring padding automatically):
from thinc.api import chain, PyTorchLSTM, Sigmoid, Embed, with_padded, with_array2d, mean_pool # Same parameter setup as before model = chain( Embed(nV=vocab_size, nO=embedding_dim), with_padded(PyTorchLSTM(nI=embedding_dim,nO=hidden_dim, depth=n_layers)), mean_pool(), # Average across sequence steps to get 2D output with_array2d(Sigmoid(nI=hidden_dim, nO=output_size)) ) model.initialize(X=train_x[:5], Y=train_y[:5])
Quick Check for Input Shape
Just to be thorough, double-check that your train_x is a 2D array with shape (number_of_samples, sequence_length). From your x[0] example, this looks correct—but if you ever accidentally add an extra dimension (like (samples, seq_len, 1)), that would also trigger a similar error.
内容的提问来源于stack exchange,提问作者fenix

