Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

This is my DataSet

import keras

print(keras.__version__)

mnist = keras.datasets.mnist

(x_train,y_train),(x_test,y_test) = mnist.load_data()

I have performed normalizing

x_train = keras.utils.normalize(x_train,axis=1)

x_test = keras.utils.normalize(x_test,axis=1)

And built a model

model = keras.models.Sequential()

model.add(keras.layers.Flatten(x_train))

model.add(keras.layers.Dense(128,activation= keras.nn.relu))

model.add(keras.layers.Dense(128,activation= keras.nn.relu))

model.add(keras.layers.Dense(10,activation= keras.nn.softmax))

model.compile(optimizer='adam',

              loss='sparse_categorical_crossentropy',

              metrics = ['accuracy']

)

model.fit(x_train,y_train,epochs=3)

I am getting the below error:

Using TensorFlow backend.

2.3.1

Traceback (most recent call last):

  File "/Users/aditya/Desktop/Desktop/dataScience/Practice/OpenCV/FaceDetect/Hackathon/classMnist.py", line 28, in <module>

    model.add(keras.layers.Flatten(x_train))

  File "/usr/local/lib/python3.7/site-packages/keras/layers/core.py", line 495, in __init__

    self.data_format = K.normalize_data_format(data_format)

  File "/usr/local/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py", line 311, in normalize_data_format

    data_format = value.lower()

AttributeError: 'numpy.ndarray' object has no attribute 'lower'

The problem is that the Keras can't flatten my  x_train dataset. Do you have nay idea why is this hapenning?

1 Answer

0 votes
by (36.8k points)

model.add(keras.layers.Flatten(x_train))

The Keras create the network that can't load data.

model.fit(x_train,y_train,epochs=3)

The data gets loaded.

So you should first edit the code:

model.add(keras.layers.Flatten())

And your codes have other error to:

# wrong

model.add(keras.layers.Dense(128,activation= keras.nn.relu))

# right

model.add(keras.layers.Dense(128,activation= keras.backend.relu))

If you want to know more about the Data Science then do check out the following Data Science which will help you in understanding Data Science from scratch 

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...