Issue
So I recently wanted to experiment with Tensorflow and Keras again after some time. I have tried it in the past but I only scratched the surface before I got distracted and tried other things. Now, as I said, I wanted to try it out again so I just made sure I had the newest versions of Tensorflow and Keras using pip and then copied an example from the official Tensorflow website. This is the code:
# TensorFlow and tf.keras
import tensorflow as tf
# Helper libraries
import numpy as np
import matplotlib.pyplot as plt
fashion_mnist = tf.keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
train_images, test_images = train_images/255, test_images/255
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
model = tf.keras.Sequential([
tf.keras.layers.Input(shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(10)
])
model.compile(optimizer="adam",
loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics = ["accuracy"])
model.fit(train_images, train_labels, epochs=10)
Now when I run the code I get this error:
Traceback (most recent call last):
File "C:\Users\me\AppData\Local\Programs\Python\Python311\Tensorflow\example.py", line 21, in <module>
tf.keras.layers.Input(shape=(28, 28)),
File "C:\Users\me\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\layers\core\input_layer.py", line 143, in Input
layer = InputLayer(
File "C:\Users\me\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\layers\layer.py", line 216, in __new__
obj = super().__new__(cls, *args, **kwargs)
File "C:\Users\me\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\ops\operation.py", line 100, in __new__
flat_arg_values = tree.flatten(kwargs)
AttributeError: module 'tree' has no attribute 'flatten'
I am not new to programming or python either so I also tried installing different versions of Tensorflow and also using pip to upgrade the "tree" module. I also tried just replacing the function (yes a bad practice I know) using something like
tree.flatten = lambda kwargs: kwargs
but nothing has seemed to resolve the issue. I would be glad if someone knew what the problem is and could help me out.
Solution
I have executed your code in Google Colab and did not encounter any errors on that particular line. The issue you're facing is likely because of your library installations.
If you've separately installed both TensorFlow and Keras, there might be a conflict between the versions. I recommend installing only the TensorFlow library since Keras is integrated into it.
For straightforward image classification using a fully connected network, it's crucial to include a Flatten layer before the Dense layer.
Additionally, the common approach is to set the activation function of the last Dense layer to softmax for multi-class classification such that it outputs the probability instead of raw logits.
Here's the revised code snippet:
model = tf.keras.Sequential([
tf.keras.layers.Input(shape=(28, 28)),
tf.keras.layers.Flatten(), # Add this Flatten layer
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax') # Optional: Change the last activation to softmax
])
model.compile(
optimizer="adam",
loss=tf.keras.losses.SparseCategoricalCrossentropy(), # Optional: Remove the from_logits parameter if we set the final activation as softmax
metrics=["accuracy"]
)
I've created a Google Colab Notebook that you can explore here.
I hope this addresses your question thoroughly. Let me know if you need further clarification!
Answered By - Naufal Suryanto
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.