Sign Up

Have an account? Sign In Now

Sign In

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have a permission to ask a question, You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here
Sign InSign Up

ErrorCorner

ErrorCorner Logo ErrorCorner Logo

ErrorCorner Navigation

  • Home
  • Contact Us
  • About Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Contact Us
  • About Us
Home/ Questions/Q 512
Next
Answered
Kenil Vasani
Kenil Vasani

Kenil Vasani

  • 646 Questions
  • 567 Answers
  • 77 Best Answers
  • 26 Points
View Profile
  • 8
Kenil Vasani
Asked: December 14, 20202020-12-14T21:02:52+00:00 2020-12-14T21:02:52+00:00In: Python

Keras AttributeError: ‘list’ object has no attribute ‘ndim’

  • 8

I’m running a Keras neural network model in Jupyter Notebook (Python 3.6)

I get the following error

AttributeError: ‘list’ object has no attribute ‘ndim’

after calling the .fit() method from Keras.model

model  = Sequential()
model.add(Dense(5, input_dim=len(X_data[0]), activation='sigmoid' ))
model.add(Dense(1, activation = 'sigmoid'))
model.compile(loss='mean_squared_error', optimizer='adam', metrics=['acc'])
model.fit(X_data, y_data, epochs=20, batch_size=10)

I checked the requirements.txt file for Keras (in Anaconda3) and the numpy, scipy, and six module versions are all up to date.

What can explain this AttributeError?

The full error message is the following (seems to be somewhat related to Numpy):

————————————————————————— AttributeError Traceback (most recent call
last) in ()
3 model.add(Dense(1, activation = ‘sigmoid’))
4 model.compile(loss=’mean_squared_error’, optimizer=’adam’, metrics=[‘acc’])
—-> 5 model.fit(X_data, y_data, epochs=20, batch_size=10)

~\Anaconda3\lib\site-packages\keras\models.py in fit(self, x, y,
batch_size, epochs, verbose, callbacks, validation_split,
validation_data, shuffle, class_weight, sample_weight, initial_epoch,
steps_per_epoch, validation_steps, **kwargs)
963 initial_epoch=initial_epoch,
964 steps_per_epoch=steps_per_epoch,
–> 965 validation_steps=validation_steps)
966
967 def evaluate(self, x=None, y=None,

~\Anaconda3\lib\site-packages\keras\engine\training.py in fit(self, x,
y, batch_size, epochs, verbose, callbacks, validation_split,
validation_data, shuffle, class_weight, sample_weight, initial_epoch,
steps_per_epoch, validation_steps, **kwargs) 1591
class_weight=class_weight, 1592 check_batch_axis=False,
-> 1593 batch_size=batch_size) 1594 # Prepare validation data. 1595 do_validation = False

~\Anaconda3\lib\site-packages\keras\engine\training.py in
_standardize_user_data(self, x, y, sample_weight, class_weight, check_batch_axis, batch_size) 1424
self._feed_input_shapes, 1425
check_batch_axis=False,
-> 1426 exception_prefix=’input’) 1427 y = _standardize_input_data(y, self._feed_output_names,
1428 output_shapes,

~\Anaconda3\lib\site-packages\keras\engine\training.py in
_standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
68 elif isinstance(data, list):
69 data = [x.values if x.class.name == ‘DataFrame’ else x for x in data]
—> 70 data = [np.expand_dims(x, 1) if x is not None and x.ndim == 1 else x for x in data]
71 else:
72 data = data.values if data.class.name == ‘DataFrame’ else data

~\Anaconda3\lib\site-packages\keras\engine\training.py in
(.0)
68 elif isinstance(data, list):
69 data = [x.values if x.class.name == ‘DataFrame’ else x for x in data]
—> 70 data = [np.expand_dims(x, 1) if x is not None and x.ndim == 1 else x for x in data]
71 else:
72 data = data.values if data.class.name == ‘DataFrame’ else data

AttributeError: ‘list’ object has no attribute ‘ndim’

jupyter-notebookkerasmachine-learningpythontensorflow
  • 1 1 Answer
  • 9 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook

    1 Answer

    • Voted
    1. Kenil Vasani

      Kenil Vasani

      • 646 Questions
      • 567 Answers
      • 77 Best Answers
      • 26 Points
      View Profile
      Best Answer
      Kenil Vasani
      2020-12-14T21:02:23+00:00Added an answer on December 14, 2020 at 9:02 pm

      model.fit expects x and y to be numpy array. Seems like you pass a list, it tried to get shape of input by reading ndim attribute of numpy array and failed.

      You can simply transform it using np.array:

      import numpy as np
      ...
      model.fit(np.array(train_X),np.array(train_Y), epochs=20, batch_size=10)
      
      • 6
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp

    You must login to add an answer.

    Forgot Password?

    Sidebar

    Ask A Question
    • Popular
    • Kenil Vasani

      SyntaxError: invalid syntax to repo init in the AOSP code

      • 5 Answers
    • Kenil Vasani

      xlrd.biffh.XLRDError: Excel xlsx file; not supported

      • 3 Answers
    • Kenil Vasani

      Homebrew fails on MacOS Big Sur

      • 3 Answers
    • Kenil Vasani

      runtimeError: package fails to pass a sanity check for numpy ...

      • 3 Answers
    • Kenil Vasani

      ERROR: torch has an invalid wheel, .dist-info directory not found

      • 2 Answers

    Explore

    • Most Answered
    • Most Visited
    • Most Voted
    • Random

    © 2020-2021 ErrorCorner. All Rights Reserved
    by ErrorCorner.com