Python Forum
I was trying to build a model using neural networks but It says layers not definied
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I was trying to build a model using neural networks but It says layers not definied
#1
ERROR MESSAGE IS DOWN

#IMPORTING THE LIBRARIES
import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt

#IMPORT THE DATASET
data = keras.datasets.cifar10

#SPLIT THE DATA AND LOAD IT
(train_images, train_labels), (test_images, test_labels) = data.load_data()

#CHECKING
train_images.shape,'/',train_labels.shape,'/',test_images.shape,'/',test_labels.shape

train_images = train_images / 255.0
test_images = test_images / 255.0

INDEX = 4
plt.imshow(train_images[INDEX], cmap=plt.cm.binary)
plt.show()

#STORE ALL FEATURES IN A LIST
names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']

#BUILDING THE MODEL
model = keras.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
#ERROR ERROR

Error:
NameError Traceback (most recent call last) <ipython-input-19-60526a5e37ed> in <module>() 1 #BUILDING THE MODEL 2 model = keras.Sequential() ----> 3 model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3))) 4 model.add(layers.MaxPooling2D((2, 2))) 5 model.add(layers.Conv2D(64, (3, 3), activation='relu')) NameError: name 'layers' is not defined
PLEASE HELP!

Please can someone help me
Reply
#2
I am no expert in neural networks but Python is right - there is no layers defined thus NameError. Shouldn’t you provide imported library name (keras.layers like in row #11 with data)?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
not related to your question, but what do you think this line do
#CHECKING
train_images.shape,'/',train_labels.shape,'/',test_images.shape,'/',test_labels.shape
I mean, there is comment CHECKING, but...
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
I have been trying to solve this problem for 3 hours now. I did everything I knew, Searched whole youtube and Stackoverflow and have tried your suggestions as well but nothing seems to work.
Reply
#5
(Jun-08-2020, 04:40 PM)MohammedSohail Wrote: I have been trying to solve this problem for 3 hours now. I did everything I knew, Searched whole youtube and Stackoverflow and have tried your suggestions as well but nothing seems to work.
why did you have layers in your code? what it is suppose to be? how do you write code you don't know why you have certain code?
did you try as suggested by @perfingo to replace layers with keras.layers?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
Here are the docs to Tensorflow/keras/layers for conv2d
https://www.tensorflow.org/api_docs/pyth...ers/Conv2D

Examples are given, but in your case I suspect that where you have layers, change to tf.keras.layers and you will be ok.
This works. Cute car.
#IMPORTING THE LIBRARIES
import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt
 
#IMPORT THE DATASET
data = keras.datasets.cifar10
 
#SPLIT THE DATA AND LOAD IT
(train_images, train_labels), (test_images, test_labels) = data.load_data()
 
#CHECKING
train_images.shape,'/',train_labels.shape,'/',test_images.shape,'/',test_labels.shape
 
train_images = train_images / 255.0
test_images = test_images / 255.0
 
INDEX = 4
plt.imshow(train_images[INDEX], cmap=plt.cm.binary)
plt.show()
 
#STORE ALL FEATURES IN A LIST
names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
 
#BUILDING THE MODEL
model = keras.Sequential()
model.add(tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))
model.add(tf.keras.layers.MaxPooling2D((2, 2)))
model.add(tf.keras.layers.Conv2D(64, (3, 3), activation='relu'))
model.add(tf.keras.layers.MaxPooling2D((2, 2)))
model.add(tf.keras.layers.Conv2D(64, (3, 3), activation='relu'))
Reply
#7
Thank you for all your answers. My problem has solved
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  model.fit and model.predict errors hatflyer 6 1,308 Nov-10-2023, 01:39 AM
Last Post: hatflyer
  FileNotFoundError: [Errno 2] No such file or directory: 'model/doc2vec.model/Articles Anldra12 10 5,771 Jun-11-2021, 04:48 PM
Last Post: snippsat
  use win32wnet to connect shared networks JoanAzpeitia 0 6,861 Dec-21-2017, 06:24 PM
Last Post: JoanAzpeitia

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020