I am trying to save a tensorflow model on Google Colab, but am running into an ImportError issue. I made a test sample code below to see if the issue was due to some other conflicts, but even in the following snippet I ran into the same issue on the line where I save the model.
import tensorflow as tf
from tensorflow.python.keras.callbacks import ReduceLROnPlateau
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense, Conv1D, MaxPooling1D, Flatten, Dropout
from tensorflow.python.keras.callbacks import ModelCheckpoint
import warnings
model = Sequential()
model.add(Dense(10, input_shape=(5,)))
model.save('model.h5')
ImportError: cannot import name '__version__' from 'tensorflow.python.keras' (/usr/local/lib/python3.10/dist-packages/tensorflow/python/keras/__init__.py)
For context, I am running tensorflow version 2.17.1.
I've tried implementing solutions from other forums but none of them seem to work. Some of the solutions I've looked at are below.
How to fix "cannot import name '__version__' from 'tensorflow.keras'"?
Since Tensorflow v2.6, we shouldn't import from tensorflow.python.keras:
Keras been split into a separate PIP package (keras), and its code has been moved to the GitHub repositorykeras-team/keras. The API endpoints for tf.keras stay unchanged, but are now backed by the keras PIP package. The existing code in tensorflow/python/keras is a staled copy and will be removed in future release (2.7). Please remove any imports to tensorflow.python.keras and replace them with public tf.keras API instead. https://newreleases.io/project/github/tensorflow/tensorflow/release/v2.6.0
Instead use:
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
There are warnings in Colab but it works fine. Another way if you want to remove the warnings:
from tensorflow import keras
from keras.api.models import Sequential
from keras.api.layers import Dense