In a tutorial, we use Tensorflow in a Jupyter Notebook to build and train a neural network to classify images. While trying to plot images we got an error related to the Tensor type of element and its attributes.
This code works on Colab environment with no errors. While my local setup with Tensorflow 1, Python 3.7, is unable to run it.
# Normalize data
def normalize(images, labels):
images = tf.cast(images, tf.float32)
images /= 255
return images, labels
# The map function applies the normalize function to each element in the train enter code here and test datasets
train_dataset = train_dataset.map(normalize)
test_dataset = test_dataset.map(normalize)
# Take a single image, and remove the color dimension by reshaping
for image, label in test_dataset.take(1):
break
image = image.numpy().reshape((28,28))
plt.figure()
plt.imshow(image, cmap=plt.cm.binary)
plt.colorbar()
plt.grid(False)
plt.show()
It should plot an image but yields the following error instead:
AttributeError Traceback (most recent call last)
(ipython-i nput-10-89e82cad4b8f) in (module)
2 for image, label in test_dataset.take(1):
3 break
----> 4 image = image.numpy().reshape((28,28))
5 plt.figure()
AttributeError: 'Tensor' object has no attribute 'numpy'