Reputation: 581
I'm new to deep learning and Keras. When I used Keras to fit a LSTM model, I got the following error message: ValueError: Error when checking target: expected dense_2 to have shape (10,) but got array with shape (1,)
Here are my code for building LSTM:
def build(self, embedding_matrix, dim, num_class, vocab_size, maxlen):
model = Sequential()
model.add(Embedding(vocab_size, dim, weights = [embedding_matrix],
input_length = maxlen, trainable = False)) ## pre-trained model
model.add(LSTM(dim))
model.add(Dense(dim, activation = "relu"))
model.add(Dense(num_class, activation = "softmax"))
self.model = model
Before this post, I tried several solutions metioned in other SO posts. For example, use to_categorical
to transform labels, use Flatten
before the final layer. Sadly none of them worked.
Here are my log file for running the script:
Start to fit GLOVE with reported data
Applying GLOVE pre-trained model
Number of unique tokens: 308758
Pre-trained model finished
Applying keras text pre-processing
Finish to apply keras text pre-processing
Start to fit the model...
Build LSTM model...
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
embedding_1 (Embedding) (None, 36303, 300) 92627400
_________________________________________________________________
lstm_1 (LSTM) (None, 300) 721200
_________________________________________________________________
dense_1 (Dense) (None, 300) 90300
_________________________________________________________________
dense_2 (Dense) (None, 10) 3010
=================================================================
Total params: 93,441,910
Trainable params: 814,510
Non-trainable params: 92,627,400
_________________________________________________________________
None
Finish model building
It went well untill history = self.model.fit(train_padded, y_train, epochs = 10, batch_size = 128, validation_split = 0.2)
, then I got the above error.
I run out of solutions. Any help will be appriciate!
Edit:
About the y_train
, here is the code I use for building y_train
:
labels = dt["category"].values
num_class = len(np.unique(labels))
classes = np.unique(labels)
le = LabelEncoder()
y = le.fit_transform(labels)
y = to_categorical(y, num_class)
## split to training and test set
x_train, y_train, x_test, y_test = train_test_split(text, y, test_size = 0.33,
random_state = 42,
stratify = dt["category"].astype("str"))
Another update: here are the shapes.
The shape of y_train: (48334,)
The shape of x_train: (98132,)
The shape of y_test: (48334, 10)
The shape of x_test: (98132, 10)
Upvotes: 3
Views: 2058
Reputation: 3228
The problem is that you are getting your x_train, y_train, x_test, y_test
in the wrong order, so it is assigning things incorrectly. train_test_split
returns the following:
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33,random_state=42)
whereas you have x_train, y_train, x_test, y_test
train_test_split
docs
Upvotes: 1