1

I am trying to learn Tensorflow and Deep Neural Networks. This error came up and i couldn't find an explanation. I am working on Pycharm and i tried on Anaconda command prompt also . Should i try on windows command prompt?

import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt
data = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = data.load_data()

class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
           'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']

 train_images = train_images/255
 test_images = test_images/255
 model = keras.Sequential([
keras.layers.Flatten(input_shape=(28, 28)), # First layer
keras.layers.Dense(128, activation='relu'), #Second layer
keras.layers.Dense(10, activation='softmax') # Third layer
])


 model.compile(optimizer='adam',
          loss='sparse_categorical_crossentropy',
          metrics=['accuracy'])
 model.fit(train_images, train_labels, epochs=5)

 # test_loss, test_acc = model.evaluate(test_images,  test_labels, verbose=2)

 # print('\nTest accuracy:', test_acc)

  prediction = model.predict(test_images)
 # print(class_names[np.argmax(prediction[1])])

 for i in range(5):
    plt.grid(False)
    plt.imshow(test_images[i], cmap=plt.cm.binary)
    plt.xlabel("Actual: ", class_names[test_labels[i]])
    plt.title("Prediction : ", class_names[np.argmax(prediction[i])])
    plt.show()

''' And I get this error '''

 Traceback (most recent call last):
   File "C:/Users/Kullanici/Desktop/MachineLearningProjects/neuralNetworkTutorial/TensorDeepNN.py", 
     line 40, in <module>
    plt.xlabel("Actual: ", class_names[test_labels[i]])
    File "C:\Users\Kullanici\Anaconda3\envs\tensor\lib\site-packages\matplotlib\pyplot.py", line 
    3063, in xlabel
    xlabel, fontdict=fontdict, labelpad=labelpad, **kwargs)
    File "C:\Users\Kullanici\Anaconda3\envs\tensor\lib\site-packages\matplotlib\axes\_axes.py", line 
    247, in set_xlabel
     return self.xaxis.set_label_text(xlabel, fontdict, **kwargs)
     File "C:\Users\Kullanici\Anaconda3\envs\tensor\lib\site-packages\matplotlib\axis.py", line 
     1598, in set_label_text
    self.label.update(fontdict)
    File "C:\Users\Kullanici\Anaconda3\envs\tensor\lib\site-packages\matplotlib\text.py", line 176, 
     in update
    bbox = kwargs.pop("bbox", sentinel)
   **AttributeError: 'str' object has no attribute 'pop'**

I couldn't find a solution.

3
  • bbox = kwargs.pop("bbox", sentinel) you're trying to call pop() on a String, but pop() is a List method. Running on Windows command prompt will not change the output. Commented Dec 11, 2019 at 22:19
  • thanks for your answer. Commented Dec 11, 2019 at 22:23
  • What don’t you understand from the error message? Commented Dec 12, 2019 at 1:38

1 Answer 1

3

In function, matplotlib.pyplot.xlabel(xlabel, fontdict=None, labelpad=None, **kwargs) the first argument should be string. However, you are passing

    plt.xlabel("Actual: ", class_names[test_labels[i]])
    plt.title("Prediction : ", class_names[np.argmax(prediction[i])])

which is not a string. You should call

    plt.xlabel("Actual: " + class_names[test_labels[i]])
    plt.title("Prediction : " + class_names[np.argmax(prediction[i])])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.