Skip to content Skip to sidebar Skip to footer

Keras Attributeerror: 'list' Object Has No Attribute 'ndim'

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()

Solution 1:

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)

Solution 2:

When you import you should use tensorflow.keras instead of just keras like this:

from tensorflow.keras.modelsimportSequentialfrom tensorflow.keras.layersimportInput, Conv2D, MaxPool2D, Dense

because there is a bug related to the keras module.

Reference: here.

Solution 3:

I don't know the shape of your training data but I suspect that you have an error on your input_dim. Try changing it to input_dim=len(X_data) like this:

model  = Sequential()
model.add(Dense(5, input_dim=len(X_data), 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)

Post a Comment for "Keras Attributeerror: 'list' Object Has No Attribute 'ndim'"