Keras AttributeError: 'list' object has no attribute 'ndim'

情到浓时终转凉″ 提交于 2019-12-06 18:29:50

问题


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'


回答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)



回答2:


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

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Input, Conv2D, MaxPool2D, Dense

because there is a bug related to the keras module.

Reference: here.




回答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)


来源:https://stackoverflow.com/questions/48493755/keras-attributeerror-list-object-has-no-attribute-ndim

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!