parrot15
parrot15

Reputation: 329

How to fix AttributeError: 'NoneType' object has no attribute 'original_name_scope' for my CNN?

I am trying to learn how to create a simple convolutional neural network, but I am getting an error:

AttributeError: 'NoneType' object has no attribute 'original_name_scope'

I don't know why this is happening. Earlier, I made a multilayer perceptron as my model with four layers (without the np.reshape part in the data preprocessing part of the code) instead of this CNN model and it worked fine. I would appreciate some help.

Here is my code:

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import random

# ****** load data ******
mnist_dataset = tf.keras.datasets.mnist
(train_images, train_labels), (test_images, test_labels) = mnist_dataset.load_data()

# ****** label list ******
class_names = ['Zero', 'One', 'Two', 'Three', 'Four',
               'Five', 'Six', 'Seven', 'Eight', 'Nine']

# ****** preprocess data ******
# scale RGB values from 0 to 1
train_images = train_images / 255.0
test_images = test_images / 255.0

# reshape data to fit model
train_images = train_images.reshape(-1, 28, 28, 1)
test_images = test_images.reshape(-1, 28, 28, 1)

# ****** build the model ******
model = tf.keras.Sequential()

# input layer
model.add(tf.keras.layers.Conv2D(64, kernel_size=(5, 5)))
model.add(tf.keras.layers.BatchNormalization())
model.add(tf.keras.layers.Activation(tf.nn.relu))

# hidden layer 1
model.add(tf.keras.layers.Conv2D(32, kernel_size=(5, 5)))
model.add(tf.keras.layers.BatchNormalization())
model.add(tf.keras.layers.Activation(tf.nn.relu))

# hidden layer 2
model.add(tf.layers.Flatten())
model.add(tf.keras.layers.Dense(100))
model.add(tf.keras.layers.BatchNormalization())
model.add(tf.keras.layers.Activation(tf.nn.relu))

# output layer
model.add(tf.keras.layers.Dense(10))
model.add(tf.keras.layers.BatchNormalization())
model.add(tf.keras.layers.Activation(tf.nn.softmax))

# ****** configure how model is updated, how model minimizes
# loss, and what to monitor ******
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

# ****** feed training data to the model ******
model.fit(train_images, train_labels, epochs=5)

# ****** compare how model performs on test dataset ******
test_loss, test_acc = model.evaluate(test_images, test_labels)
print(f'Test accuracy: {test_acc}')

# ****** make predictions about some images ******
predictions = model.predict(test_images)
print(f'shape of prediction data: {predictions.shape}')

Edit:

Here is the full traceback:

Traceback (most recent call last):
  File "/Users/MyName/Documents/PythonWorkspace/LearningTensorflow/test.py", line 62, in <module>
    model.fit(train_images, train_labels, epochs=5)
  File "/anaconda3/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 776, in fit
    shuffle=shuffle)
  File "/anaconda3/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 2289, in _standardize_user_data
    self._set_inputs(cast_inputs)
  File "/anaconda3/lib/python3.6/site-packages/tensorflow/python/training/checkpointable/base.py", line 442, in _method_wrapper
    method(self, *args, **kwargs)
  File "/anaconda3/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 2529, in _set_inputs
    outputs = self.call(inputs, training=training)
  File "/anaconda3/lib/python3.6/site-packages/tensorflow/python/keras/engine/sequential.py", line 233, in call
    inputs, training=training, mask=mask)
  File "/anaconda3/lib/python3.6/site-packages/tensorflow/python/keras/engine/sequential.py", line 253, in _call_and_compute_mask
    with ops.name_scope(layer._name_scope()):
  File "/anaconda3/lib/python3.6/site-packages/tensorflow/python/layers/base.py", line 284, in _name_scope
    return self._current_scope.original_name_scope
AttributeError: 'NoneType' object has no attribute 'original_name_scope'

Upvotes: 1

Views: 1805

Answers (1)

Thibault Bacqueyrisses
Thibault Bacqueyrisses

Reputation: 2331

You forgot a key word for your Flatten layer. It should be model.add(tf.keras.layers.Flatten())

instead of model.add(tf.layers.Flatten())

Upvotes: 1

Related Questions