Keras AttributeError: 'list' object has no attribute 'ndim'
Asked Answered
S

4

27

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'

Sandra answered 29/1, 2018 at 2:55 Comment(0)
A
45

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)
Angelia answered 29/1, 2018 at 4:35 Comment(1)
that was the problem. Fixed it! Funny though, since on another computer Keras could run even if the data was a double matrix instead of a numpy array. Thank you for your help!Sandra
P
6

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.

Penchant answered 6/2, 2019 at 10:39 Comment(1)
Is this for tensorflow 2.x?Etan
F
1

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)
Fen answered 29/1, 2018 at 8:19 Comment(1)
the X_data has 5 features and the y_data has 2 features. The output data might have been the cause, though switching to a numpy array solves the problem.Sandra
C
0

This is a basic layout I use. Hope it helps.

import tensorflow as tf
import numpy as np
from sklearn.model_selection import train_test_split

# random data
X = tf.range(-100, 100, 4)
y = X + 10

# addresses shape issues when dim is incorrect
X = tf.reshape(X, (-1, 1))
y = tf.reshape(y, (-1, 1))

# convert features and labels to numpy to work with .fit & train_test_split
X_np = np.array(X)
y_np = np.array(y)

# split data
X_train, X_test, y_train, y_test = train_test_split(X_np, y_np, test_size=0.2, random_state=42, shuffle=False)

# to get consistency in results
tf.random.set_seed(42)

# build the model
model = tf.keras.Sequential([
   tf.keras.layers.Dense(50, activation='linear'),
   tf.keras.layers.Dense(1)
])

# compile the model
model.compile(
   loss=tf.keras.losses.mae,
   optimizer=tf.keras.optimizers.Adam(learning_rate=0.01),
   metrics=['mae']
)

# fit the model
model.fit(X_train, y_train, epochs=100, verbose=False)

# evaluate the model
model.evaluate(X_train, y_train)
Catchup answered 4/4 at 4:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.