Python Forum

Full Version: I was trying to build a model using neural networks but It says layers not definied
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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)?
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...
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.
(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?
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'))
Thank you for all your answers. My problem has solved