Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

294
Views
Keras: Expected 3 dimensions, but got array with shape - dense model

I want to do multi label classification (20 distinct output labels), based on vectorized words using TfidfVectorizer. I have set of 39974 rows each one containing 2739 items (zeros or ones).

I would like to classify this data using Keras model which will contain 1 hidden layer (~20 nodes with activation='relu') and output layer equal 20 possible output values (with activation='softmax' to choose best fit).

Here's my code so far:

model = Sequential()
model.add(Dense(units=20, activation='relu', input_shape=tfidf_matrix.shape))
model.add(Dense(units=20, activation='softmax'))
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(tfidf_matrix, train_data['cuisine_id'], epochs=10)

But got error:

ValueError: Error when checking input: expected dense_1_input to have 3 dimensions, but got array with shape (39774, 2739)

How can I specify this NN to fit using this matrix?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The number of rows (number of training samples) is not the part of the input shape of the network because the training process feeds the network one sample per batch (or, more precisely, batch_size samples per batch).

So in your case, input shape of the network is (2739, ) and the right code should be like this:

model = Sequential()
# the shape of one training example is
input_shape = tfidf_matrix[0].shape
model.add(Dense(units=20, activation='relu', input_shape=input_shape))
model.add(Dense(units=20, activation='softmax'))
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', 
metrics=['accuracy'])
model.fit(tfidf_matrix, train_data['cuisine_id'], epochs=10)
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!