I was trying to run simple Machine Learning algorithm on Visua Studio Code. I downloaded Tensorflow 2 and installed it. Code I was trying to run was:
import tensorflow as tf
import numpy as np
from tensorflow import keras
model = tf.keras.Sequential([keras.layers.Dense(units=1, input_shape=[1])])
As I understood this shows how machine learning model works in general.
Then on line 4 it says
Exception has occurred: ImportError"
"cannot import name 'deserialize_keras_object' from partially initialized module 'keras.saving.legacy.serialization' (most likely due to a circular import) (C:\Users\Erdemdavaa\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\keras\saving\legacy\serialization.py)
File "D:\study\semester 6\Project laborotory(team)\ML_experiment.py", line 4, in <module>
model = tf.keras.Sequential([keras.layers.Dense(units=1, input_shape=[1])])
ImportError: cannot import name 'deserialize_keras_object' from partially initialized module 'keras.saving.legacy.serialization' (most likely due to a circular import) (C:\Users\Erdemdavaa\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\keras\saving\legacy\serialization.py)
What is this and how do I fix this?
I recently have a similar issue. It is it probably a compatibility issue between keras and tensorflow. A temporary solution is to use the tensorflow.python module, (although it is experimental, it should suffice a small machine learning project).
import tensorflow as tf
from tensorflow.python.keras import layers
from tensorflow.python.keras.models import Sequential
Sequential is imported so you can directly use it.
Had the same issue and at first I was unable to resolve it. What eventually worked for me was the following (Note this appears to be a predominant problem on machines with the M1/M2 chip):
I created a virtual environment folder using the Terminal where I will store all environments. Later you can pass it to VS Code:
$ pip install virtualenv
$ cd ~
$ mkdir .virtualenvs
$ cd .virtualenvs
Inside the newly created directory, you can create your environment and activate it:
$ virtualenv VENV_NAME
$ source VENV_NAME/bin/activate
In the VS Code preferences you can search for 'venv' and set the 'Python: Venv Path' option to ~/.virtualenvs (or whatever you decided to call your environment folder). Then I was able to select the newly created environment as a kernel. You might need to add it by searching for 'Python: Select Interpreter' after pressing (Cmd + Shift + P) - you only need this step if you are planning to use the environment in VS Code.
After these steps I simply installed tensorflow according to the apple developer page (https://developer.apple.com/metal/tensorflow-plugin/):
$ python -m pip install tensorflow
And tensorflow-metal which allows for training on Mac GPUs:
$ python -m pip install tensorflow-metal
This allowed me to run tensorflow without any issues. Hope this might resolve the issue for some.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With